18、ES实战:实现range、prefix、wildcard、fuzzy、type、ids查询

range查询
范围查询使用rangeQuery

public class EsRangeQuery throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //范围查询(range),
       //rangeQuery("birthdaty").from("1990-01-01").to("2000-12-31")等价于
       //rangeQuery("birthdaty").gt("1990-01-01").lt("2000-12-31")
       QueryBuilder qb = QueryBuilders.rangeQuery("birthdaty")
					       .from("1990-01-01")
						   .to("2000-12-31")
						   .format("yyyy-MM-dd");

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

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

prefix查询
前缀查询使用prefixQuery

public class EsPrefixQuery throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //前缀查询(prefix)--- 查询name中前缀为zhao的文档
       QueryBuilder qb = QueryBuilders.prefixQuery("name", "zhao");

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

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

wildcard查询
通配符查询使用wildcardQuery,可以用于类比实现模糊查询

public class EsWildcardQuery throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //通配符查询(wildcard)
       QueryBuilder qb = QueryBuilders.wildcardQuery("name", "zhao*");

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

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

fuzzy查询
模糊查询使用fuzzyQuery

public class EsFuzzyQueryl throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //模糊查询(fuzzy)
       QueryBuilder qb = QueryBuilders.fuzzyQuery("interests", "chagge");

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

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

type查询
类型查询查询使用typeQuery,_type的查询

public class EsTypeQuery throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //类型查询(type)
       QueryBuilder qb = QueryBuilders.typeQuery("blog");

       SearchResponse sr = client.prepareSearch("index1")
                                 .setQuery(qb)
	                             .get();

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

ids查询
id查询查询使用idsQuery,_id的查询

public class EsTypeQuery throws UnknownHostException{
   
     
    public void updateIndex(TransportClient client){
   
     
       //id查询查询(ids)
       QueryBuilder qb = QueryBuilders.idsQuery().addIds("1","3");

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

       SearchHits hits = sr.getHits();

       for(Searchhit hit:hits){
   
     
           //整个文档输出
           System.out.println(hit.getSourceAsString);
       }                
    }
}

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