我一直在 ES 2.3 中使用此代码,并且运行良好。我正在查询 Elasticsearch 中已加载的数据。我无法在 ES 5.3 中执行此操作。我只是想从加载到 ES 的 300 个字段中获取某些字段的值。我想要的结果不是 JSON,而是“|”划定的。有没有办法对 ES 5.3 做类似的事情?
for( SearchHit hit : response.getHits())
{
String s0 = hit.field("first_name").getValue().toString();
result.add(s0);
String s1 = hit.field("last_name").getValue().toString();
result.add(s1);
String s2 ="";
if(hit.fields().containsKey("middle_name"))
{
String s2 = hit.field("middle_name").getValue().toString();
result.add(s2);
}
finalresult = s0 + "|" + s1 + "|" + s2;
}
预先感谢您的帮助
最佳答案
for( SearchHit hit : response.getHits())
{
Map<String, Object> map = hit.getSource();
String s0 = map.get("first_name").toString();
result.add(s0);
String s1 = map.get("last_name").toString();
result.add(s1);
String s2 ="";
if(map.containsKey("middle_name"))
{
String s2 = map.get("middle_name").toString();
result.add(s2);
}
finalresult = s0 + "|" + s1 + "|" + s2;
}
关于java - ElasticSearch 5.3 SearchHit hit.field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43623422/