elasticsearch 在查询的时候如何返回指定的字段值?
2016-11-06 19:57
726 查看
指定返回字段,查询方式,
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”) + “—”);
“`
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”) + “—”);
“`
相关文章推荐
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- Hibernate 查询多个字段返回的list如何转为自定义对象,以及转换异常
- Nhibernate总结(一)查询返回指定字段
- 使用mybatis,在添加数据的时候返回主键,当查询count和属性字段怎么接收返回数据
- SQLite-Python中如何返回、查询中文字段
- spring-data-mongodb查询结果返回指定字段
- SQL Server 2000通用分页查询存储过程(可指定返回字段,查询条件)
- Thinkphp框架下对某个字段查询数据的时候进行唯一过滤,返回唯一不同的值
- MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
- Laravel Eloquent ORM 时如何查询表中指定的字段
- 博客园现代化建设—[Entity Framework]在LINQ查询中指定返回的字段
- 通过id查询用户,但是只返回指定的字段
- 博客园现代化建设—[Entity Framework]在LINQ查询中指定返回的字段
- 记录:java执行mysql语句查询字段类型:timestamp返回页面显示会多出个 .0,自己如何处理的
- 如何返回指定行数之间的查询结果?
- 查询数据库时如何把数据按照字段名和字段值的数据返回
- spring-data-mongodb查询结果返回指定字段
- mysql中如何将查询结果的多个记录中的指定字段放到一个二维数组中,以及在in子句中使用数组
- hibernate中的query查询原生sql语句,如何获得返回记录中的每一个字段的值
- MyBatis从数据库取值的时候,当某一字段为null时,如何在返回前端的时候,也包含有这个字段