源代码解释如下:
/**
* A query that match on all documents.
*/
public static MatchAllQueryBuilder matchAllQuery() {
return new MatchAllQueryBuilder();
}
即用于匹配所有Document的
Query,以下是matchAllQuery的示例及常用场景的代码:
/**
* @author Geloin
*/
package com.gsoft.gcrsearch.util;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gsoft.gcrsearch.entity.Person;
/**
* @author Geloin
*
*/
public class QueryBuildersTest {
private static Client client;
private static ObjectMapper mapper;
@BeforeClass
public static void beforeClass() {
System.out.println("==============执行beforeClass()");
NodeBuilder builder = NodeBuilder.nodeBuilder();
String clusterName = PropertyManager.getContextProperty("cluster.name");
builder.clusterName(clusterName);
Node node = builder.node();
client = node.client();
mapper = new ObjectMapper();
}
@Before
public void beforeMethod() throws Exception {
System.out.println("==============执行beforeMethod()");
List<Person> persons = new ArrayList<Person>();
List<IndexRequest> requests = new ArrayList<IndexRequest>();
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setAge(20 + i);
person.setId(UUID.randomUUID().toString());
person.setIsStudent(true);
person.setName("小别克听老别克讲别克的故事" + i);
person.setSex("男");
persons.add(person);
String index = "user"; // 相当于数据库名
String type = "tb_person" + i; // 相当于表名
String json = mapper.writeValueAsString(person);
IndexRequest request = client
.prepareIndex(index, type, person.getId()).setSource(json)
.request();
requests.add(request);
}
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (IndexRequest request : requests) {
bulkRequest.add(request);
}
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
Assert.fail("批量创建索引错误!");
}
}
/**
* 常用场景一:匹配所有项
*
* @author Geloin
* @throws Exception
*/
@Test
public void matchAllQuery() throws Exception {
QueryBuilder builder = QueryBuilders.matchAllQuery();
SearchResponse response = client
.prepareSearch("user")
.setTypes("tb_person0", "tb_person1", "tb_person2",
"tb_person3", "tb_person4")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(builder) // Query
.setFilter(FilterBuilders.rangeFilter("age").from(20).to(22)) // Filter
.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
Person newPerson = mapper.readValue(json, Person.class);
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());
}
}
/**
* 常用场景二:查询某些(个)Index、某些(个)Type下的记录总量
* @author Geloin
* @throws Exception
*/
@Test
public void matchAllQueryForCount() throws Exception {
QueryBuilder builder = QueryBuilders.matchAllQuery();
long count = client.prepareCount("user")
.setQuery(builder)
.setTypes("tb_person0", "tb_person1", "tb_person2",
"tb_person3", "tb_person4").execute().actionGet()
.count();
Assert.assertTrue(count >= 3);
}
}