Rakesh J R
Rakesh J R

Reputation: 31

Elasticsearch query for two filters

I am new to Elasticsearch and i have a use case where i need to fetch data for the below 2 conditions

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

My Query always returns the whole data which is below , but i want my data to be returned only for the above conditions

{
  "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"
        }
      }
    ]
  }
}

The query which i have formed is as below :

{
  "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
}

Please help me validate the query.

Upvotes: 0

Views: 47

Answers (1)

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

There are multiple issues:

  1. JSON is invalid
  2. zoneType is placed at the root of the object, it should be under query

In order for this to work, all queries/filters must be placed within root query object and in order to combine multiple filters, you'll need to use compound queries, in this specific case - bool query, read about it more in the documentation.

So this would be the actual query that should work for you:

{
  "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
}

See I've moved zoneType under query->bool->filter[], next to geo_bounding_box query.

Upvotes: 2

Related Questions