分布式搜索Elasticsearch——QueryBuilders.idsQuery

来源:互联网 发布:mac系统怎样翻墙 编辑:程序博客网 时间:2024/05/19 15:22

   注:该文项目基础为分布式搜索 :Elasticsearch-项目过程(一)和Elasticsearch-项目过程(二),项目骨架可至这里下载。

        ES源代码中对idsQuery的描述如下所示:

[java] view plain copy
  1. /** 
  2.  * Constructs a query that will match only specific ids within types. 
  3.  * 
  4.  * @param types The mapping/doc type 
  5.  */  
  6. public static IdsQueryBuilder idsQuery(@Nullable String... types) {  
  7.     return new IdsQueryBuilder(types);  
  8. }  

        即指定type和id进行查询。

        示例代码如下所示:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.gsearch.util;  
  5.   
  6. import junit.framework.Assert;  
  7.   
  8. import org.elasticsearch.action.bulk.BulkRequestBuilder;  
  9. import org.elasticsearch.action.bulk.BulkResponse;  
  10. import org.elasticsearch.action.index.IndexRequest;  
  11. import org.elasticsearch.action.search.SearchResponse;  
  12. import org.elasticsearch.index.query.QueryBuilder;  
  13. import org.elasticsearch.index.query.QueryBuilders;  
  14. import org.elasticsearch.search.SearchHit;  
  15. import org.elasticsearch.search.SearchHits;  
  16. import org.junit.Test;  
  17.   
  18. import com.gsoft.gsearch.BaseTest;  
  19. import com.gsoft.gsearch.entity.Person;  
  20.   
  21. /** 
  22.  * @author Geloin 
  23.  * 
  24.  */  
  25. public class IdsQueryTest extends BaseTest {  
  26.     @Test  
  27.     public void idsQuery() {  
  28.         try {  
  29.               
  30.             String id1 = "udisjkdfd";  
  31.             String id2 = "ewdsfdsfe";  
  32.   
  33.             BulkRequestBuilder builder = client.prepareBulk();  
  34.             // 创建索引  
  35.             Person p = new Person();  
  36.             p.setAge(20);  
  37.             p.setId(id1);  
  38.             p.setIsStudent(true);  
  39.             p.setName("张三的故事" + Math.random());  
  40.             p.setSex("男");  
  41.   
  42.             String source = ElasticSearchUtil.BeanToJson(p);  
  43.   
  44.             IndexRequest request = client.prepareIndex(index, type, p.getId())  
  45.                     .setSource(source).request();  
  46.   
  47.             builder.add(request);  
  48.   
  49.             Person p2 = new Person();  
  50.             p2.setAge(20);  
  51.             p2.setId(id2);  
  52.             p2.setIsStudent(true);  
  53.             p2.setName("小李四讲的李四的故事" + Math.random());  
  54.             p2.setSex("男");  
  55.   
  56.             String source2 = ElasticSearchUtil.BeanToJson(p2);  
  57.   
  58.             IndexRequest request2 = client  
  59.                     .prepareIndex(index, type, p2.getId()).setSource(source2)  
  60.                     .request();  
  61.   
  62.             builder.add(request2);  
  63.   
  64.             BulkResponse response = builder.execute().actionGet();  
  65.             if (response.hasFailures()) {  
  66.                 Assert.fail("创建索引失败!");  
  67.             }  
  68.   
  69.             // 检索  
  70.             QueryBuilder qb = QueryBuilders.idsQuery(type).ids(id1, id2);  
  71.   
  72.             SearchResponse sr = client.prepareSearch(index).setTypes(type)  
  73.                     .setQuery(qb).setFrom(0).setSize(12).execute().actionGet();  
  74.   
  75.             SearchHits hits = sr.getHits();  
  76.   
  77.             if (null != hits && hits.totalHits() > 0) {  
  78.                 for (SearchHit hit : hits) {  
  79.                     String json = hit.getSourceAsString();  
  80.   
  81.                     Person newPerson = mapper.readValue(json, Person.class);  
  82.                     System.out.println("id\t\t" + newPerson.getId());  
  83.                     System.out.println("name\t\t" + newPerson.getName());  
  84.                     System.out.println("sex\t\t" + newPerson.getSex());  
  85.                     System.out.println("age\t\t" + newPerson.getAge());  
  86.                     System.out.println("isStudent\t\t"  
  87.                             + newPerson.getIsStudent());  
  88.                 }  
  89.             } else {  
  90.                 log.error("没有查询到任何内容!");  
  91.                 return;  
  92.             }  
  93.   
  94.             // 查询总数量  
  95.             long count = client.prepareCount(index).setTypes(type).setQuery(qb)  
  96.                     .execute().actionGet().count();  
  97.   
  98.             log.info("符合条件的总数量为:" + count);  
  99.   
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.         } finally {  
  103.             if (null != client) {  
  104.                 client.close();  
  105.             }  
  106.             if (null != node) {  
  107.                 node.close();  
  108.             }  
  109.         }  
  110.     }  
  111. }  

        请注意:必须在生成idsQuery后为其设定id,设定id的方法有以下两种:

[java] view plain copy
  1. /** 
  2.  * Adds ids to the filter. 
  3.  */  
  4. public IdsQueryBuilder addIds(String... ids) {  
  5.     values.addAll(Arrays.asList(ids));  
  6.     return this;  
  7. }  
  8.   
  9. /** 
  10.  * Adds ids to the filter. 
  11.  */  
  12. public IdsQueryBuilder ids(String... ids) {  
  13.     return addIds(ids);  
  14. }  
0 0