Fizmeister
Fizmeister

Reputation: 109

Elasticsearch returning Integer but field in my mapping is explicitly a Long

I am using Elasticsearch v5.2.2 and when doing a match all query via the Java API, when I loop over the Map source object from the returned hits, my ID fields which are mapped as Long are being returned as Integer. As a result, my code, which expects a Long and casts the returned Object value to a long, throws a ClassCastException. Any idea why Elasticsearch is ignoring my mapping and returning an Integer?

I can confirm that using CURL and the rest API, I see my ID field mapped as type Long.

Mapping:

    {
      "reference" : {
        "mappings" : {
          "city" : {
            "_all" : {
              "enabled" : false
            },
            "properties" : {
              "id" : {
                "type" : "long"
              },
              "location" : {
                "type" : "geo_point"
              },
              "type" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          }
        }
      }
    }

code which returns an Integer rather than a Long:

      public List<City> findCityByType(String value) {
        SearchResponse response = client.prepareSearch("reference").setTypes("city")
            .setQuery(QueryBuilders.matchPhrasePrefixQuery("type", value)).get();
        SearchHits searchHits = response.getHits();
        SearchHit[] hits = searchHits.getHits();
        List<City> cities = new ArrayList<>();
        for (SearchHit hit : hits) {
          City city = new City();
          Map<String, Object> source = hit.getSource();

          //Have to cast to Integer, not Long. Why does ES return an Integer?
          postcode.setId((Integer)source.get("id"));
          postcode.setType((String) source.get("type"));
        }
        return cities;
      }

Thanks

Upvotes: 2

Views: 1383

Answers (1)

Jimmy Zhang
Jimmy Zhang

Reputation: 967

If the long field contains lesser values like 1, the java api returns integer.

Upvotes: 1

Related Questions