如何解决Elasticsearch查询两个过滤器
我是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","_id": "chak-01","node_reference": "980066547976678","_id": "chak-01"
}
},"_id": "dummy_elastic_update_station","_source": {
"dummystnupddate": "Thu Dec 03 2015 07:00:01 GMT+0000","level": "1","icon": "newicons/dataxicons/blue.png","tag_location": "unknown"
}
},"_id": "5f3121d6b4c93c1d20bbbb38","loc": [
{
"lat": "0","lon": "0"
}
],"kml_path": "","created": "Mon Aug 10 16:00:47 IST 2020","latitude": "0","station_id": "5f3121d6b4c93c1d20bbbb38","longtitude": "0","tag_owner": "","description": "","zoneType": "test","display_name": "testname","supply_zone": "testname","outflow": null,"tag_sector": "dmameter","name": "testname","inflow": null,"tag_location": "NA","lastmod": "Mon Aug 10 16:00:47 IST 2020","status": "ACTIVE"
}
},"_id": "5f312253b4c93c1d20bbbb39","created": "Mon Aug 10 16:02:51 IST 2020","station_id": "5f312253b4c93c1d20bbbb39","zoneType": "oms","display_name": "506020200236117-O1","supply_zone": "506020200236117-O1","name": "506020200236117-O1","lastmod": "Mon Aug 10 16:02:51 IST 2020","_id": "5f32357b3ccb8f51e003587e","created": "Tue Aug 11 11:36:51 IST 2020","station_id": "5f32357b3ccb8f51e003587e","zoneType": "village","display_name": "testvillage1","supply_zone": "testvillage1","name": "testvillage1","lastmod": "Tue Aug 11 11:36:51 IST 2020","_id": "5ee0861c5f15030001b2dfd9","created": "Wed Jun 10 07:05:00 UTC 2020","station_id": "5ee0861c5f15030001b2dfd9","display_name": "bhamarhar","supply_zone": "bhamarhar","name": "bhamarhar","lastmod": "Wed Jun 10 07:05:00 UTC 2020","_id": "5f2ad03bba21eb28684451e3","created": "Wed Aug 05 20:58:59 IST 2020","station_id": "5f2ad03bba21eb28684451e3","display_name": "zone-1","supply_zone": "zone-1","name": "zone-1","lastmod": "Wed Aug 05 20:58:59 IST 2020","_type": "sensor_info","_id": "chak_01_btry","_source": {
"parent": "chak_01","sensortype_units": "volt","device_id": "OMS_MP_LRR_001","level": 2,"sensortype_actual": "btry","highrate": false,"tag_datasource": "xylem","sensortype_display": "btry - chak_01","type": "sensor","sensortype_backend": "btry"
}
},"_id": "chak-01/pressure","_source": {
"parent": "chak-01","sensortype_units": "bar","sensortype_actual": "pressure","sensortype_display": "pressure - chak-01","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
查询旁边。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。