【问题标题】:Using geo_distance filter doesn't return any hits using ElasticSearch使用 geo_distance 过滤器不会返回任何使用 ElasticSearch 的命中
【发布时间】:2016-11-24 00:08:59
【问题描述】:

我无法让我的过滤器查询与 geo_distance 一起正常工作。它似乎返回 0 次点击。但是,如果我不尝试查找地理位置,我的所有其他查询都有效。

我使用的是 2.3.1 版的 ElasticSearch

{
  "name" : "Mar-Vell",
  "cluster_name" : "elastic-logs",
  "version" : {
    "number" : "2.3.1",
    "build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
    "build_timestamp" : "2016-04-04T12:25:05Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

通过使用 json 发出请求,我已将我的位置键映射到“geo_point”类型,如下所示:

curl -XPUT 'http://10.0.7.181:9200/connections/profile/_mapping' -d '
{

      "profile" : {
         "properties" : {
            "location" : {
               "type" : "geo_point"
            }
         }
      }
}

它会返回这个。我假设我的更改已生效。

{"acknowledged":true}

这是我的数据示例

  {
    "_index": "connections",
    "_type": "profile",
    "_id": "92",
    "_score": 1,
    "_source": {
      "profile": {
        "location": {
          "lon": -111.8909815,
          "lat": 40.7607818
        },
        "age": 44,
        "school": {
          "undergraduate": {
            "universityId": 1814,
            "active": true,
            "programId": 9
          },
          "graduate": {
            "universityId": 1814,
            "active": true,
            "programId": 7
          }
        },
        "bio": "Everything is awesome! ????"
      },
      "id": 0,
      "active": false,
      "optIn": true
    }
  }

我要发送到 ElasticSearch 的查询。没什么疯狂的。

{
   "filter" : {
      "geo_distance" : {
         "distance" : "1000mi",
         "distance_type": "plane",
         "location" : {
          "lon": -111.8391029,
          "lat": 40.7607818
         }
      }
   },
   "query" : {
      "match_all" : {}
   }
}

我尝试将距离更改为 1 英里、100 英里和 1000 英里。它应该返回 100 英里但没有显示的东西。我也尝试过使用不同的单位测量来看看它是否会做任何事情。同样的交易。

坐标位于盐湖城。并且经纬度的顺序应该是对的。我不确定我还应该尝试什么。

【问题讨论】:

    标签: elasticsearch geolocation


    【解决方案1】:

    您的位置字段的名称是location,但在您的查询中,您使用geoPoint 作为字段名称。试试这个:

    {
       "filter" : {
          "geo_distance" : {
             "distance" : "1000mi",
             "distance_type": "plane",
             "location" : {
              "lon": -111.8391029,
              "lat": 40.7607818
             }
          }
       },
       "query" : {
          "match_all" : {}
       }
    }
    

    更新:好的,这是我看到的明显错误,所以我没有进一步查看。这是我的一个项目中的一个有效查询(带有您的值):

    {
      "query": {
        "bool": {
          "must": {
            "match_all": []
          },
          "filter": {
            "geo_distance": {
              "distance": "1000mi",
              "distance_type": "plane",
              "location": {
                "lat": "-111.8391029",
                "lon": "40.7607818"
              }
            }
          }
        }
      }
    }
    

    应该可以的。

    【讨论】:

    • 对不起,我复制并粘贴了错误的代码。我对示例搜索查询进行了更改。我正在试验一个新属性,我想通过创建一个名为“geoPoint”的不同属性,其类型为“geo_point”,其值相同,它会有所作为。我之前曾尝试按“位置”搜索,但没有成功。
    • 好的,我现在用我的一个项目的有效查询更新了我的答案。
    【解决方案2】:

    @Pelmered 感谢您帮助我进行过滤器查询。我发现我的映射不正确。现在一切都按预期工作。因此,请确保您正确映射它。

    curl -XPUT 'http://10.0.7.181:9200/connections' -d '
    
    {
       "mappings":{
          "profile":{
             "properties":{
                "location":{
                   "type":"geo_point"
                }
             }
          }
       }
    }'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2020-07-14
      • 1970-01-01
      • 2021-06-05
      • 2021-06-16
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多