gpt4 book ai didi

elasticsearch - Elasticsearch查询两个过滤器

转载 作者:行者123 更新时间:2023-12-03 00:45:13 26 4
gpt4 key购买 nike

我是Elasticsearch的新手,我有一个用例,需要为以下2个条件获取数据

  • zoneType : [test,oms]
  • {"geo_bounding_box":{"location":{"top_left":{"lat":"1.3545001078734353","lon":"103.87945999358624"},"bottom_right":{"lat":"1.3435168247600437","lon":"103.89390100692282"}}}

  • 我的查询始终返回下面的全部数据,但是我希望仅针对以上条件返回我的数据
    {
    "took": 0,
    "timed_out": false,
    "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
    },
    "hits": {
    "total": 11,
    "max_score": 1,
    "hits": [
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "chak_01",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lng": 78.460938,
    "lat": 25.665325
    }
    ],
    "level": 1,
    "station_id": "01",
    "tag_owner": "xylem",
    "tag_network_name": "chak_network",
    "supply_zone": "mantena",
    "display_name": "Chak 01",
    "tag_sector": "sensorstation",
    "meta_info": {
    "site": {
    "site_name": "site Name",
    "site_id": "04"
    },
    "district": {
    "district_name": "district Name",
    "district_id": "03"
    },
    "state": {
    "state_name": "state Name",
    "state_id": "05"
    },
    "village": {
    "village_id": "01",
    "village_name": "village Name"
    },
    "tehsil": {
    "tehsil_id": "02",
    "tehsil_name": "tehsil Name"
    }
    },
    "tag_category": "sensorstation",
    "node_reference": "chak-01",
    "_id": "chak_01"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "chak-01",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lng": 78.460938,
    "lat": 25.665325
    }
    ],
    "level": 1,
    "station_id": "01",
    "tag_owner": "xylem",
    "tag_network_name": "chak_network",
    "supply_zone": "mantena",
    "display_name": "Chak 01",
    "tag_sector": "sensorstation",
    "meta_info": {
    "site": {
    "site_name": "site Name",
    "site_id": "04"
    },
    "district": {
    "district_name": "district Name",
    "district_id": "03"
    },
    "state": {
    "state_name": "state Name",
    "state_id": "05"
    },
    "village": {
    "village_id": "01",
    "village_name": "village Name"
    },
    "tehsil": {
    "tehsil_id": "02",
    "tehsil_name": "tehsil Name"
    }
    },
    "tag_category": "sensorstation",
    "node_reference": "980066547976678",
    "_id": "chak-01"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "dummy_elastic_update_station",
    "_score": 1,
    "_source": {
    "dummystnupddate": "Thu Dec 03 2015 07:00:01 GMT+0000",
    "level": "1",
    "icon": "newicons/dataxicons/blue.png",
    "_id": "dummy_elastic_update_station",
    "tag_location": "unknown"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "5f3121d6b4c93c1d20bbbb38",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lat": "0",
    "lon": "0"
    }
    ],
    "level": 1,
    "kml_path": "",
    "created": "Mon Aug 10 16:00:47 IST 2020",
    "latitude": "0",
    "station_id": "5f3121d6b4c93c1d20bbbb38",
    "longtitude": "0",
    "tag_owner": "",
    "description": "",
    "zoneType": "test",
    "tag_network_name": "chak_network",
    "display_name": "testname",
    "supply_zone": "testname",
    "outflow": null,
    "tag_sector": "dmameter",
    "name": "testname",
    "tag_category": "sensorstation",
    "inflow": null,
    "_id": "5f3121d6b4c93c1d20bbbb38",
    "tag_location": "NA",
    "lastmod": "Mon Aug 10 16:00:47 IST 2020",
    "status": "ACTIVE"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "5f312253b4c93c1d20bbbb39",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lat": "0",
    "lon": "0"
    }
    ],
    "level": 1,
    "kml_path": "",
    "created": "Mon Aug 10 16:02:51 IST 2020",
    "latitude": "0",
    "station_id": "5f312253b4c93c1d20bbbb39",
    "longtitude": "0",
    "tag_owner": "",
    "description": "",
    "zoneType": "oms",
    "tag_network_name": "chak_network",
    "display_name": "506020200236117-O1",
    "supply_zone": "506020200236117-O1",
    "outflow": null,
    "tag_sector": "dmameter",
    "name": "506020200236117-O1",
    "tag_category": "sensorstation",
    "inflow": null,
    "_id": "5f312253b4c93c1d20bbbb39",
    "tag_location": "NA",
    "lastmod": "Mon Aug 10 16:02:51 IST 2020",
    "status": "ACTIVE"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "5f32357b3ccb8f51e003587e",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lat": "0",
    "lon": "0"
    }
    ],
    "level": 1,
    "kml_path": "",
    "created": "Tue Aug 11 11:36:51 IST 2020",
    "latitude": "0",
    "station_id": "5f32357b3ccb8f51e003587e",
    "longtitude": "0",
    "tag_owner": "",
    "description": "",
    "zoneType": "village",
    "display_name": "testvillage1",
    "supply_zone": "testvillage1",
    "outflow": null,
    "tag_sector": "dmameter",
    "name": "testvillage1",
    "tag_category": "sensorstation",
    "inflow": null,
    "_id": "5f32357b3ccb8f51e003587e",
    "tag_location": "NA",
    "lastmod": "Tue Aug 11 11:36:51 IST 2020",
    "status": "ACTIVE"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "5ee0861c5f15030001b2dfd9",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lat": "0",
    "lon": "0"
    }
    ],
    "level": 1,
    "kml_path": "",
    "created": "Wed Jun 10 07:05:00 UTC 2020",
    "latitude": "0",
    "station_id": "5ee0861c5f15030001b2dfd9",
    "longtitude": "0",
    "tag_owner": "",
    "description": "",
    "tag_network_name": "chak_network",
    "display_name": "bhamarhar",
    "supply_zone": "bhamarhar",
    "outflow": null,
    "tag_sector": "dmameter",
    "name": "bhamarhar",
    "tag_category": "sensorstation",
    "inflow": null,
    "_id": "5ee0861c5f15030001b2dfd9",
    "tag_location": "NA",
    "lastmod": "Wed Jun 10 07:05:00 UTC 2020",
    "status": "ACTIVE"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "station_info",
    "_id": "5f2ad03bba21eb28684451e3",
    "_score": 1,
    "_source": {
    "tag_datatype": "sensor",
    "loc": [
    {
    "lat": "0",
    "lon": "0"
    }
    ],
    "level": 1,
    "kml_path": "",
    "created": "Wed Aug 05 20:58:59 IST 2020",
    "latitude": "0",
    "station_id": "5f2ad03bba21eb28684451e3",
    "longtitude": "0",
    "tag_owner": "",
    "description": "",
    "tag_network_name": "chak_network",
    "display_name": "zone-1",
    "supply_zone": "zone-1",
    "outflow": null,
    "tag_sector": "dmameter",
    "name": "zone-1",
    "tag_category": "sensorstation",
    "inflow": null,
    "_id": "5f2ad03bba21eb28684451e3",
    "tag_location": "NA",
    "lastmod": "Wed Aug 05 20:58:59 IST 2020",
    "status": "ACTIVE"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "sensor_info",
    "_id": "chak_01_btry",
    "_score": 1,
    "_source": {
    "parent": "chak_01",
    "sensortype_units": "volt",
    "device_id": "OMS_MP_LRR_001",
    "level": 2,
    "sensortype_actual": "btry",
    "highrate": false,
    "tag_datasource": "xylem",
    "_id": "chak_01_btry",
    "sensortype_display": "btry - chak_01",
    "type": "sensor",
    "sensortype_backend": "btry"
    }
    },
    {
    "_index": "dataintelindex_man",
    "_type": "sensor_info",
    "_id": "chak-01/pressure",
    "_score": 1,
    "_source": {
    "parent": "chak-01",
    "sensortype_units": "bar",
    "device_id": "OMS_MP_LRR_001",
    "level": 2,
    "sensortype_actual": "pressure",
    "highrate": false,
    "tag_datasource": "xylem",
    "_id": "chak-01/pressure",
    "sensortype_display": "pressure - chak-01",
    "type": "sensor",
    "sensortype_backend": "pressure"
    }
    }
    ]
    }
    }
    我形成的查询如下:
    {
    "query":{
    "geo_bounding_box":{
    "location":{
    "top_left":{
    "lat":"1.3545001078734353",
    "lon":"103.87945999358624"
    },
    "bottom_right":{
    "lat":"1.3435168247600437",
    "lon":"103.89390100692282"
    }
    }
    }
    },
    {
    "zoneType":[
    "oms",
    "test"
    ]
    },
    "size":100000
    }
    请帮助我验证查询。

    最佳答案

    存在多个问题:

  • JSON无效
  • zoneType放置在对象的根目录下,它应该在查询


  • 为了使它起作用,所有查询/过滤器必须放置在根 query对象中,并且为了组合多个过滤器,在这种特定情况下,您需要使用 compound queries-bool查询,请在文档中详细了解。
    因此,这将是适合您的实际查询:
    {
    "query": {
    "bool": {
    "filter": [
    {
    "geo_bounding_box": {
    "location": {
    "top_left": {
    "lat": "1.3545001078734353",
    "lon": "103.87945999358624"
    },
    "bottom_right": {
    "lat": "1.3435168247600437",
    "lon": "103.89390100692282"
    }
    }
    }
    },
    {
    "terms": {
    "zoneType": [
    "oms",
    "test"
    ]
    }
    }
    ]
    }
    },
    "size": 100000
    }
    请参阅我已将 zoneType移至 query->bool->filter[]查询旁边的 geo_bounding_box下。

    关于elasticsearch - Elasticsearch查询两个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63357790/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com