您的位置:首页 > 其它

elasticsearch 在查询的时候如何返回指定的字段值?

2016-11-06 19:57 726 查看
指定返回字段,查询方式,

SearchResponse response = client.prepareSearch("sb").setTypes("sb")
.setQuery(query).setFrom(0).setSize(500)
.setExplain(false)
.addFields(new String[]{"cphm1","jcdid","cplx1","tpid1","tgsj","cdid"})
.execute().actionGet();```

**结果获取方式:**
//指定返回字段时的结果获取方式------begin---------

Map<String, Object> map = new HashMap<String, Object>();
List<Map> listresult = new ArrayList<Map>();
for(final SearchHit hit:response.getHits()){
final Iterator<SearchHitField> iterator = hit.iterator();
while(iterator.hasNext()){
final SearchHitField hitfield = iterator.next();
map.put(hitfield.getName(),hitfield.getValue());
System.out.print(hitfield.getName()+"=="+hitfield.getValue()+"-----");
}
listresult.add(map);
System.out.println();
}
for(final Map m:listresult){
// System.out.println(m.get("jcdid")+"--"+m.get("cphm1")+"--"+m.get("tpid1")+"--"+m.get("tgsj"));
}

**普通查询方式**

SearchResponse response = client.prepareSearch("sb").setTypes("sb")
.setQuery(query).setFrom(0).setSize(500)
.setExplain(false)
.execute().actionGet();

结果获取方式:


SearchHits hits = response.getHits();

for (int i = 0; i < hits.getHits().length; i++) {

System.out.print(“主键值:”+hits.getAt(i).getId()+”—>”);

System.out.print(hits.getAt(i).getSource().get(“cphm1”) + “—”);

System.out.print(hits.getAt(i).getSource().get(“cplx1”) + “—”);

System.out.print(hits.getAt(i).getSource().get(“jcdid”) + “—”);

“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐