【问题标题】:Elasticsearch "failed to find geo_point field [location]" when the mapping is there for that field当该字段存在映射时,Elasticsearch“无法找到 geo_point 字段 [位置]”
【发布时间】:2018-11-06 09:04:57
【问题描述】:

我有一个具有以下映射的索引:

{
  "mappings":{
    "my_stuff_type":{
      "properties":{
        "location": {
          "type": "geo_point",
          "null_value": -1
        }
      }
    }
  }
}

我必须使用属性null_value,因为我的一些文档没有关于其位置(纬度/经度)的信息,但我仍然想按位置搜索距离,参见。这里:https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html

在检查索引映射详细信息时,我可以验证地理映射是否存在:

curl -XGET http://localhost:9200/my_stuff_index/_mapping | jq '.my_stuff_index.mappings.my_stuff_type.properties.location'
{
  "properties": {
    "lat": {
      "type": "float"
    },
    "lon": {
      "type": "float"
    }
  }
}

但是,当尝试使用地理距离过滤器(参见https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance.html)在该索引上搜索文档时,我看到了:

curl -XPOST http://localhost:9200/my_stuff_index/_search -d'
{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "location": {
            "lat": <PUT_LATITUDE_FLOAT_HERE>,
            "lon": <PUT_LONGITUDE_FLOAT_HERE>
          },
          "distance": "200m"
        }
      }
    }
  }
}' | jq

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to find geo_point field [location]",
        "index_uuid": "mO94yEsHQseQDFPkHjM6tA",
        "index": "my_stuff_index"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my_stuff_index",
        "node": "MDueSn31TS2z0Lamo64zbw",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to find geo_point field [location]",
          "index_uuid": "mO94yEsHQseQDFPkHjM6tA",
          "index": "my_stuff_index"
        }
      }
    ],
    "caused_by": {
      "type": "query_shard_exception",
      "reason": "failed to find geo_point field [location]",
      "index_uuid": "mO94yEsHQseQDFPkHjM6tA",
      "index": "my_stuff_index"
    }
  },
  "status": 400
}

我认为null_value 属性应该允许我插入没有location 归档的文档,同时我应该能够在同一个“可选”字段上使用过滤器进行搜索。

为什么我无法过滤那个“可选”字段?我怎么能这样做?

编辑:

要使用 python 重现此问题,请运行以下代码 sn-p,然后从命令行执行 curl/jq 操作。

python 代码依赖于此:pip install elasticsearch==5.4.0.

from elasticsearch import Elasticsearch
from elasticsearch import helpers

my_docs = [
    {"xyz": "foo", "location": {"lat": 0.0, "lon": 0.0}},
    {"xyz": "bar", "location": {"lat": 50.0, "lon": 50.0}}
]

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

index_mapping = '''
{
  "mappings":{
    "my_stuff_type":{
      "properties":{
        "location": {
          "type": "geo_point",
          "null_value": -1.0
        }
      }
    }
  }
}'''

es.indices.create(index='my_stuff_index', ignore=400, body=index_mapping)

helpers.bulk(es, my_docs, index='my_stuff_index', doc_type='my_stuff_type')

【问题讨论】:

  • 您返回的映射清楚地表明您的位置字段不是geo_point,而是两个浮点数。您需要使用适当的映射重新索引您的数据,即您在问题中显示的第一个。
  • @Val 我提供了一个 python 代码 sn-p 来重现 geo_point 的问题,为什么会这样?谢谢

标签: python elasticsearch elasticsearch-5


【解决方案1】:

正如@Val 所说,您应该更改映射。如果以这种方式定义位置字段:

    "location": {
      "type": "geo_point"
    }

您可以将 lanlon 索引为两个不同的子字段 - 无需在映射中声明它们,如我所示 - 如文档中所述 - look here

【讨论】:

  • 为了更清楚:不需要映射属性null_value。还要确保索引中不包含具有意外地理信息的文档(例如错误格式,例如纬度/经度的字符串,甚至None 或空值,例如来自以前的pandas 数据帧),即清理文档使用 python 代码,例如:isinstance(my_dirty_doc["latitude"], numbers.Number) == True 或删除字典中的键,例如 my_dirty_doc.pop("latitude", None)
猜你喜欢
  • 1970-01-01
  • 2012-10-26
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多