ElasticSearch按地理位置过滤,有值或空值。

问题描述 投票:0回答:1

我正在向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
          }
        }
      }
    ]
  }
}

有什么提示或建议吗?谢谢你。

elasticsearch geolocation
1个回答
1
投票

当前的查询是互斥的--你首先过滤出有效的坐标,然后执行径向搜索......。

相反,你可能需要一个逻辑的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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.