2017-05-04 18 views
0

我使用的是Elasticsearch v5.2.2,當通過Java API匹配所有查詢時,當我從返回的匹配中遍歷Map源對象時,我的ID字段它們被映射爲Long作爲整數返回。因此,我的代碼需要一個Long並將返回的Object值轉換爲long,並引發ClassCastException。任何想法爲什麼Elasticsearch會忽略我的映射並返回一個Integer?Elasticsearch返回整數,但我的映射中的字段明確是一個長

我可以確認使用CURL和其他API,我看到我的ID字段映射爲類型Long。

映射:

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

代碼返回一個整數,而不是長:

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

由於

+0

請提供有關您的應用程序的更多信息(代碼示例,配置...) –

回答

1

如果長字段包含象1更小的值,Java API的返回整數。

相關問題