GeoPoint with Spring Data ElasticSearch is giving error : QueryParsingException[field [location] is not a geo_point field]

GeoPoint with Spring Data ElasticSearch is giving error : QueryParsingException[field [location] is not a geo_point field]

我在 spring 中有弹性搜索实体,如下所示:

@Document(indexName = "cities_in", type = "cities")
public class CityDocument {

@Id
private String id;

@Field(type = FieldType.String)
private String city;

@Field(type = FieldType.Object)
@GeoPointField
private GeoPoint location;
}

但是当我看到使用 curl curl -s -XGET 'http://localhost:9200/cities_in/cities/_mapping?pretty=1' 的映射时,我得到的输出为:

  {
   "cities_in" : {
   "mappings" : {
    "cities" : {
      "properties" : {
        "city" : {
          "type" : "string"
        },
        "id" : {
         "type" : "string"
      },
      "location" : {
        "properties" : {
          "geohash" : {
            "type" : "string"
          },
          "lat" : {
            "type" : "double"
          },
          "lon" : {
            "type" : "double"
          }
        }
        }
        }
     }
   }
 }
}

当我查询时,出现以下错误:

{
 "error" : {
"root_cause" : [ {
  "type" : "query_parsing_exception",
  "reason" : "No query registered for [geo_point]",
  "index" : "cities_in",
  "line" : 1,
  "col" : 33
} ],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [ {
  "shard" : 0,
  "index" : "cities_in",
  "node" : "4jGBH3m4SkqNnBwC196hTw",
  "reason" : {
    "type" : "query_parsing_exception",
    "reason" : "No query registered for [geo_point]",
    "index" : "cities_in",
    "line" : 1,
    "col" : 33
  }
} ]
  },
  "status" : 400
 }

为什么位置类型不是 geo_point? 如何使用 spring 管理地理点?

问题是 FieldType.Object 字段在 @GeoPointField 之前被 Spring 数据的 MapperBuilder.

处理

所以你只需要删除 @Field(type = FieldType.Object) 注释并像这样声明你的 location 字段:

@GeoPointField
private GeoPoint location;

如果您使用 Spring,请确保您使用来自 spring

的 GeoPoint

org.springframework.data.elasticsearch.core.geo.GeoPoint