在弹性搜索中索引地理空间导致错误?

时间:2013-02-24 01:48:55

标签: search lucene elasticsearch

{ title: 'abcccc',
  price: 3300,
  price_per: 'task',
  location: { lat: -33.8756, lon: 151.204 },
  description: 'asdfasdf' 
 }

以上是我要索引的JSON。但是,当我索引它时,错误是:

{"error":"MapperParsingException[Failed to parse [location]]; nested: ElasticSearchIllegalArgumentException[unknown property [lat]]; ","status":400}

如果我删除“位置”字段,一切正常。

如何索引地理位置?我阅读了教程,我仍然感到困惑它是如何工作的。它应该像这样工作,对吧......?

2 个答案:

答案 0 :(得分:2)

您收到此错误消息,因为字段位置未正确映射。在某些时候,您可能尝试在此字段中索引字符串,现在它已映射为字符串。 Elasticsearch无法自动检测字段是否包含geo_point。必须在映射中明确指定它。否则,Elasticsearch将此字段映射为字符串,数字或对象,具体取决于您在第一个索引记录中使用的geo_point表示的类型。将字段添加到映射后,将无法再更改其类型。因此,为了解决这种情况,您需要删除此类型的映射并再次创建。以下是为geo_point字段指定映射的示例:

curl -XDELETE "localhost:9200/geo-test/"
echo
# Set proper mapping. Elasticsearch cannot automatically detect that something is a geo_point:
curl -XPUT "localhost:9200/geo-test" -d '{
    "settings": {
        "index": {
            "number_of_replicas" : 0,
            "number_of_shards": 1
        }
    },
    "mappings": {
        "doc": {
            "properties": {
                "location" : {
                    "type" : "geo_point"
                }
            }
        }
    }
}'
echo
# Put some test data in Sydney
curl -XPUT "localhost:9200/geo-test/doc/1" -d '{ 
    "title": "abcccc",
    "price": 3300,
    "price_per": "task",
    "location": { "lat": -33.8756, "lon": 151.204 },
    "description": "asdfasdf"
 }'
curl -XPOST "localhost:9200/geo-test/_refresh"
echo
# Search, and calculate distance to Brisbane 
curl -XPOST "localhost:9200/geo-test/doc/_search?pretty=true" -d '{
    "query": {
        "match_all": {}
    },
    "script_fields": {
        "distance": {
            "script": "doc['\''location'\''].arcDistanceInKm(-27.470,153.021)"
        }
    },
    "fields": ["title", "location"]
}
'
echo

答案 1 :(得分:-1)

由于您未指定解析方式,因此:

Parsing through JSON in JSON.NET with unknown property names

可能会带来一些亮点。