【问题标题】:Geo Distance Filter with ElasticSearch always returns 0 results使用 ElasticSearch 的地理距离过滤器始终返回 0 个结果
【发布时间】:2014-03-06 10:55:17
【问题描述】:

我将Geo Distance FilterElasticSearch 一起使用,无论我搜索什么距离,elasticsearch 0.90.11 都会返回零结果。

这是我所做的:首先,使用地理映射删除/创建一个新索引:

curl -XDELETE 'http://localhost:9200/photos'

curl -XPOST 'http://localhost:9200/photos' -d '
{
  "mappings": {
    "pin" : {
        "properties" : {
            "location" : {
                "type" : "geo_point"
            }
        }
    }
  }
}
'

然后,添加一个文档:

curl -XPOST 'http://localhost:9200/photos/photo?pretty=1' -d '
{
   "pin" : {
      "location" : {
         "lat" : 46.8,
         "lon" : -71.2
      }
   },
   "file" : "IMG_2115.JPG"
}
'

然后搜索:

curl -XGET 'http://localhost:9200/photos/_search?pretty=1&size=20' -d '
{
   "query" : {
      "match_all" : {}
   },
   "filter" : {
      "geo_distance" : {
         "distance" : "10km",
         "pin.location" : {
            "lat" : "46.8",
            "lon" : "-71.2"
         }
      }
   }
}
'

但搜索结果为零:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

有人知道为什么在半径范围内找不到文档吗?作为一个有趣的附注,上面引用的文档中描述的同时具有“查询”和“过滤器”作为子字段的“过滤”语法似乎根本不再起作用,现在似乎需要“查询”和“过滤器”在 json 查询的顶层。

任何帮助表示赞赏...

【问题讨论】:

    标签: elasticsearch geo


    【解决方案1】:

    原来官方手册页不准确,需要映射

    [Sun Feb  9 11:01:55 2014] # Request to: http://localhost:9200
    curl -XPUT 'http://localhost:9200/photos?pretty=1' -d '
    {
       "mappings" : {
          "photo" : {
             "properties" : {
                "Location" : {
                   "type" : "geo_point"
                }
             }
          }
       }
    }
    '
    

    然后需要像这样添加带有 GPS 数据的记录:

    [Sun Feb  9 11:01:56 2014] # Request to: http://localhost:9200
    curl -XPOST 'http://localhost:9200/photos/photo?pretty=1' -d '
    {
       "file" : "/home/mschilli/iphone/IMG_2115.JPG",
       "Location" : [
          46.8,
          -71.2
       ]
    }
    '
    

    然后是查询

    [Sun Feb  9 11:05:00 2014] # Request to: http://localhost:9200
    curl -XGET 'http://localhost:9200/photos/_search?pretty=1&size=100' -d '
    {
       "filter" : {
          "geo_distance" : {
             "distance" : "1km",
             "Location" : [
                36.986,
                -121.443333333333
             ]
          }
       },
       "query" : {
          "match_all" : {}
       }
    }
    '
    

    将显示所需的结果,正确过滤掉所选 GPS 距离之外的结果。

    【讨论】:

    • +1 谢谢!我已经尝试了 2 天。
    • 我提交了pull request 来更新误导性文档。
    【解决方案2】:

    我有类似的问题,花了很多时间来解决它。所以也许它对某人有帮助。 问题可能是,如果您想按距离搜索嵌套字段属性,例如:

    something: properties: name: type: text city: type: nested properties: location: type: geo_point

    您需要在主查询中添加嵌套查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多