【发布时间】: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