17、ES实战:实现term查询、terms查询

term查询
trem 查询只可指定查询一个字段对应单个词条

public class EsTermQuery{
   
     
    public void updateIndex(TransportClient client){
   
     
       //查询一个字段对应单个词条(term)
       QueryBuilder qb = QueryBuilders.termQuery("interests", "changge");

       SearchResponse sr = client.prepareSearch("lib3")
                                 .setQuery(qb)
                                 .setSize(3)
                                 .get();

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
           //文档中每个字段数据
           Map<String, Object> map = hit.getSourceAsMap();
           for(String key:map.keySet()){
   
     
               System.out.println(key+"="+map.get(key))
           }
       }                
    }
}

terms查询
trems 查询可指定查询一个字段对应多个词条

public class EsTermsQuery{
   
     
    public void updateIndex(TransportClient client){
   
     
       //查询一个字段对应多个词条(terms)
       QueryBuilder qb = QueryBuilders.termsQuery("interests", "changge", "lvyou");

       SearchResponse sr = client.prepareSearch("lib3")
                                 .setQuery(qb)
                                 .setSize(3)
                                 .get();

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
           //文档中每个字段数据
           Map<String, Object> map = hit.getSourceAsMap();
           for(String key:map.keySet()){
   
     
               System.out.println(key+"="+map.get(key))
           }
       }                
    }
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: