【问题标题】:ElasticSearch filter by geo location with value or null valueElasticSearch 按具有值或空值的地理位置过滤
【发布时间】:2020-05-12 06:20:37
【问题描述】:

我正在将业务添加到 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】:

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

    相反,您可能需要一个逻辑 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"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多