분산 검색 Elasticsearch-QueryBuilders.matchAllQuery

4476 단어
소스 코드는 다음과 같이 설명됩니다.
4
    /**
     * 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);
	}

}

좋은 웹페이지 즐겨찾기