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;
}
由於
請提供有關您的應用程序的更多信息(代碼示例,配置...) –