ElasticSearch按地理位置使用value或null值过滤

tyu7yeag  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(71)

我在ElasticSearch中添加了一些业务。一些是地理位置(经度,纬度坐标),一些是网上业务(没有坐标)。
我想做的是创建一个查询,在其中过滤给定地理位置和半径的企业。我想包括这些在线企业(没有地理坐标)。
你有什么办法吗?
我试过了:

GET /organizations/_search
{
  "query": {
    "bool" : {
      "must_not": {
        "exists": {
          "field": "geocoords"
        }
      },
      "filter" : {
        "geo_distance" : {
          "distance" : "200km",
          "geocoords" : {
            "lon": -73.57,
            "lat": 45.45
          }
        }
      }
    }
  }
}

字符串
但是我没有得到任何结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}


这是我的数据:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "organizations",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "is_active" : true,
          "name": "Compagny Inc.",
          "geocoords" : {
            "lon" : -73.5761003,
            "lat" : 45.4560316
          }
        }
      }
    ]
  }
}


有什么建议吗?谢谢。

wkftcu5l

wkftcu5l1#

当前查询是互斥的--您首先过滤出有效的坐标,然后执行径向搜索……
相反,你可能需要一个逻辑OR --要么在半径内,要么根本没有坐标:

GET organizations/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": {
              "geo_distance": {
                "distance": "200km",
                "geocoords": {
                  "lon": -73.57,
                  "lat": 45.45
                }
              }
            }
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "geocoords"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

字符串

相关问题