注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)和分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。
ES源代码中对idsQuery的描述如下所示:
/**
* Constructs a query that will match only specific ids within types.
*
* @param types The mapping/doc type
*/
public static IdsQueryBuilder idsQuery(@Nullable String... types) {
return new IdsQueryBuilder(types);
}
即 指定type和id进行查询。
示例代码如下所示:
/**
* @author Geloin
*/
package com.gsoft.gsearch.util;
import junit.framework.Assert;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Test;
import com.gsoft.gsearch.BaseTest;
import com.gsoft.gsearch.entity.Person;
/**
* @author Geloin
*
*/
public class IdsQueryTest extends BaseTest {
@Test
public void idsQuery() {
try {
String id1 = "udisjkdfd";
String id2 = "ewdsfdsfe";
BulkRequestBuilder builder = client.prepareBulk();
// 创建索引
Person p = new Person();
p.setAge(20);
p.setId(id1);
p.setIsStudent(true);
p.setName("张三的故事" + Math.random());
p.setSex("男");
String source = ElasticSearchUtil.BeanToJson(p);
IndexRequest request = client.prepareIndex(index, type, p.getId())
.setSource(source).request();
builder.add(request);
Person p2 = new Person();
p2.setAge(20);
p2.setId(id2);
p2.setIsStudent(true);
p2.setName("小李四讲的李四的故事" + Math.random());
p2.setSex("男");
String source2 = ElasticSearchUtil.BeanToJson(p2);
IndexRequest request2 = client
.prepareIndex(index, type, p2.getId()).setSource(source2)
.request();
builder.add(request2);
BulkResponse response = builder.execute().actionGet();
if (response.hasFailures()) {
Assert.fail("创建索引失败!");
}
// 检索
QueryBuilder qb = QueryBuilders.idsQuery(type).ids(id1, id2);
SearchResponse sr = client.prepareSearch(index).setTypes(type)
.setQuery(qb).setFrom(0).setSize(12).execute().actionGet();
SearchHits hits = sr.getHits();
if (null != hits && hits.totalHits() > 0) {
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
Person newPerson = mapper.readValue(json, Person.class);
System.out.println("id\t\t" + newPerson.getId());
System.out.println("name\t\t" + newPerson.getName());
System.out.println("sex\t\t" + newPerson.getSex());
System.out.println("age\t\t" + newPerson.getAge());
System.out.println("isStudent\t\t"
+ newPerson.getIsStudent());
}
} else {
log.error("没有查询到任何内容!");
return;
}
// 查询总数量
long count = client.prepareCount(index).setTypes(type).setQuery(qb)
.execute().actionGet().count();
log.info("符合条件的总数量为:" + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != client) {
client.close();
}
if (null != node) {
node.close();
}
}
}
}
请注意:必须在生成idsQuery后为其设定id ,设定id的方法有以下两种:
/**
* Adds ids to the filter.
*/
public IdsQueryBuilder addIds(String... ids) {
values.addAll(Arrays.asList(ids));
return this;
}
/**
* Adds ids to the filter.
*/
public IdsQueryBuilder ids(String... ids) {
return addIds(ids);
}