分布式搜索Elasticsearch——QueryBuilders.matchAllQuery

        源代码解释如下:

    /**
     * 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);
	}

}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值