Java Elasticsearch返回整数,但映射中的字段显式为长
我使用的是Elasticsearch v5.2.2,当通过Java API执行match all查询时,当我从返回的点击中循环映射源对象时,映射的ID字段将作为整数返回。因此,我的代码(预期为Long并将返回的对象值强制转换为Long)抛出了ClassCastException。知道为什么Elasticsearch忽略我的映射并返回一个整数吗 我可以确认,使用CURL和restapi,我看到我的ID字段映射为Long类型 映射:Java Elasticsearch返回整数,但映射中的字段显式为长,java,
elasticsearch,Java,
elasticsearch,我使用的是Elasticsearch v5.2.2,当通过Java API执行match all查询时,当我从返回的点击中循环映射源对象时,映射的ID字段将作为整数返回。因此,我的代码(预期为Long并将返回的对象值强制转换为Long)抛出了ClassCastException。知道为什么Elasticsearch忽略我的映射并返回一个整数吗 我可以确认,使用CURL和restapi,我看到我的ID字段映射为Long类型 映射: { "reference" : {
{
"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;
}
公共列表findCityByType(字符串值){
SearchResponse=client.prepareSearch(“参考”).setTypes(“城市”)
.setQuery(QueryBuilders.matchPhrasePrefixQuery(“type”,value)).get();
SearchHits SearchHits=response.getHits();
SearchHit[]hits=searchHits.getHits();
List cities=new ArrayList();
for(SearchHit:hits){
城市=新城();
Map source=hit.getSource();
//必须强制转换为整数,不长。为什么ES返回整数?
postcode.setId((整数)source.get(“id”);
postcode.setType((字符串)source.get(“type”);
}
回归城市;
}
谢谢如果长字段包含较小的值,如1,java api将返回整数。请提供有关您的应用程序的更多信息(代码示例、配置等)。我遇到了相同的问题,您找到答案了吗?