【问题标题】:ElasticSearch geo_distance filter is not returning any hitsElasticSearch geo_distance 过滤器没有返回任何命中
【发布时间】:2015-01-24 01:07:54
【问题描述】:

我正在使用 ElasticSearch (1.4.0) 执行搜索。

我已经成功设置了 ElasticSearch,除了 geo_location 过滤器之外,一切都很好。

每当我使用 geo_location 过滤器时,都不会返回任何结果。 请注意,ElasticSearch 不会返回任何错误。即使我确保在我的搜索点附近有实体,也没有返回任何命中。

这是我用来测试 geo_distance 过滤器的 PHP 参数数组的简化版本:

    $searchParams['index'] = 'myname';
    $searchParams['type']  = 'myentity';

    $searchParams['body']['query']['filtered']['filter']['geo_distance'] = 
                    [
                        'distance' => '50km',
                        'address.geopoint' => $lat . "," $lng
                    ];

这是 curl 请求正文,它也不返回任何命中:

{
    "query" : {
        "filtered" : {
            "filter" : {
                "geo_distance" : {
                    "distance" : "50km",
                    "address.geopoint" : "51.43,-2.54"
                }
            }
        }
    }
}

这是我的映射:

{  
   "myname":{  
      "mappings":{  
         "myentity":{  
            "properties":{  
               "address":{  
                  "type":"nested",
                  "properties":{  
                     "country":{  
                        "type":"string"
                     },
                     "geopoint":{  
                        "type":"geo_point",
                        "lat_lon":true
                     },
                     "house":{  
                        "type":"string"
                     },
                     "postcode":{  
                        "type":"string"
                     },
                     "street":{  
                        "type":"string"
                     },
                     "town":{  
                        "type":"string"
                     }
                  }
               },
               "categories":{  
                  "type":"string"
               },
               "tags":{  
                  "type":"string"
               }
            }
         }
      }
   }
}

这是我希望得到的文件:

[_source] => Array (
    [categories] => Array (
    )
    [tags] => Array (
    )
    [address] => Array (
        [street] => Bristol Road
        [geopoint] => 51.4307381,-2.5417914
        [town] => Bristol
        [house] => 1
        [postcode] => BS4 5NL
        [country] => UK
    )
)

任何帮助将不胜感激。

【问题讨论】:

  • 我建议在你的 php 客户端中尝试一下。只需使用curlmarvel。如果问题仍然存在,请使用您的查询、您认为应该返回的数据、映射和 ES 版本更新帖子。
  • 我尝试过使用 curl,但还是一样。 PHP 客户端和 curl 都给出相同的结果。我用更多数据更新了我的问题。任何帮助将不胜感激。

标签: php elasticsearch


【解决方案1】:

根据doc

格式为[lon, lat],注意,这里lon/lat的顺序是为了符合GeoJSON。

所以我觉得你应该试试:

address.coordinates = [$lng, $lat]


编辑:编辑后回答

自从你发布了你的地图,我现在看到了你真正的问题。当您将 nested type 用于 address 对象时,您需要一个类似于以下内容的 nested query

{
    "query": {
        "nested": {
            "path": "address",
            "query": {
                "filtered": {
                    "filter": {
                        "geo_distance": {
                            "distance": "50km",
                            "geopoint": "51.43,-2.54"
                        }
                    }
                }
            }
        }
    }
}

要么就是这样,要么停止使用嵌套类型(如果我没记错的话,它只是从您的映射中删除 "type":"nested",)。对于这个特定的用例并不是真正需要的,您的原件可以使用。

【讨论】:

  • 我已经尝试过了,但仍然没有。虽然你是对的,但这是我的代码中的另一个问题,但这只会返回非常糟糕的结果,因为 lat 和 lng 会被颠倒。我现在修复了它,但同样的问题仍然存在 - 使用 geo_distance 过滤器时没有命中。检查我的问题。我添加了更多信息、预期数据和映射。
  • @AndriusBartulis 再次检查我的答案,我刚刚编辑了它:)
  • 1.- filtered query可以在query属性下包含你需要的查询
  • 2.- 查看this 以了解如何获取距离。
  • @AndriusBartulis 如果答案解决了您的问题,请接受它:)
猜你喜欢
  • 1970-01-01
  • 2016-11-24
  • 2011-12-25
  • 1970-01-01
  • 2021-06-16
  • 2021-06-05
  • 2020-07-14
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多