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

指定返回字段,查询方式,

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”) + “—”);

“`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值