3

I have elasticsearch entity in spring as below :

@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;
}

but when I see mapping using curl curl -s -XGET 'http://localhost:9200/cities_in/cities/_mapping?pretty=1' I get output as:

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

and when hit I query I get below error:

{
 "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
 }

Why is location type not geo_point? How to manage geopoint with spring?

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Neha Pateliya
  • 81
  • 2
  • 11

2 Answers2

6

If you use Spring, make sure thath you use GeoPoint from spring

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

Thibaut
  • 2,596
  • 2
  • 24
  • 24
5

The problem is that FieldType.Object fields get processed before @GeoPointField by Spring Data's MapperBuilder.

So you simply need to remove the @Field(type = FieldType.Object) annotation and just declare your location field like this:

@GeoPointField
private GeoPoint location;
Val
  • 207,596
  • 13
  • 358
  • 360
  • Still its working as spring is not creating field type "geo_point". If i am adding mapping using curl and then adding/fetching data from spring its working. May be spring not supporting field type "geo_point" – Neha Pateliya Jun 19 '16 at 10:56
  • What version of Spring Data are you using? Have you deleted the index prior to restarting your Spring app? – Val Jun 19 '16 at 15:54
  • 1
    Thanks a lot, its working now. I was using GeoPoint class "org.elasticsearch.common.geo.GeoPoint" instead of "org.springframework.data.elasticsearch.core.geo.GeoPoint". – Neha Pateliya Jun 21 '16 at 05:15
  • @Val can you tell in what case I must use FieldType.Object? Example, I have an Object Client with name and id attributes, I want to store this in elasticsearch just they way it is (with those 2 attributes), but when I run my app I get an error: 'MapperParsingException[object mapping for [cliente] tried to parse field [cliente] as object, but found a concrete value]' – Francisco Souza Jan 29 '20 at 14:56
  • @franzisk This is because your field `cliente` already exists in the mapping and has another type. You cannot change the type once created, you need to create a new index with the correct type for `cliente` and then reindex your data – Val Jan 29 '20 at 16:43