分布式搜索Elasticsearch——MultiSearch

        MultiSearch是ElasticSearch提供的针对多个查询请求进行一次查询的接口,该接口虽然能解决同时执行多个不同的查询,但存在以下问题:

        1. 无法对最终结果进行分页,除非人工分页;

        2. 有可能多个SearchRequest查询出来的结果中,存在重复的结果,但MultiSearch并不负责去重。

        org.elasticsearch.index.query.QueryBuilders提供了一些可同时执行多种不同Query的接口,你可自行研究。

        MultiSearch示例如下所示:

/**
 * @author Geloin
 */
package com.gsoft.gsearch.util;

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.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
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 MultiSearchTest extends BaseTest {
	@Test
	public void multiSearchQuery() {
		try {

			// 创建索引
			BulkRequestBuilder builder = client.prepareBulk();

			for (int i = 0; i < 2; i++) {
				Person p = new Person();
				p.setId(UUID.randomUUID().toString());
				p.setAge(20);
				p.setIsStudent(false);
				p.setSex("男");
				p.setName("张三的车");

				String source = ElasticSearchUtil.BeanToJson(p);

				IndexRequest request = client.prepareIndex().setIndex(index)
						.setType(type).setId(p.getId()).setSource(source)
						.request();

				builder.add(request);
			}
			for (int i = 0; i < 2; i++) {
				Person p = new Person();
				p.setId(UUID.randomUUID().toString());
				p.setAge(20);
				p.setIsStudent(false);
				p.setSex("男");
				p.setName("李四的货");

				String source = ElasticSearchUtil.BeanToJson(p);

				IndexRequest request = client.prepareIndex().setIndex(index)
						.setType(type).setId(p.getId()).setSource(source)
						.request();

				builder.add(request);
			}

			BulkResponse bResponse = builder.execute().actionGet();
			if (bResponse.hasFailures()) {
				Assert.fail("创建索引出错!");
			}

			// 检索
			QueryBuilder qb1 = QueryBuilders.matchQuery("name", "张三");
			QueryBuilder qb2 = QueryBuilders.matchQuery("name", "李四");
			QueryBuilder qb3 = QueryBuilders.matchQuery("name", "车");
			QueryBuilder qb4 = QueryBuilders.matchQuery("name", "货");

			SearchRequestBuilder searchRB1 = client.prepareSearch(index)
					.setTypes(type).setQuery(qb1).setFrom(0).setSize(8);
			SearchRequestBuilder searchRB2 = client.prepareSearch(index)
					.setTypes(type).setQuery(qb2).setFrom(0).setSize(9);
			SearchRequestBuilder searchRB3 = client.prepareSearch(index)
					.setTypes(type).setQuery(qb3).setFrom(0).setSize(11);
			SearchRequestBuilder searchRB4 = client.prepareSearch(index)
					.setTypes(type).setQuery(qb4).setFrom(0).setSize(12);

			MultiSearchResponse response = client.prepareMultiSearch()
					.add(searchRB1)
					.add(searchRB2)
					.add(searchRB3)
					.add(searchRB4)
					.execute().actionGet();
			
			for (MultiSearchResponse.Item item : response.responses()) {
				SearchResponse searchResponse = item.response();
				SearchHits hits = searchResponse.getHits();
				if (null == hits || hits.totalHits() == 0) {
					log.error("使用matchPhraseQuery(\"name\", \"wa\")没有查询到任何结果!");
				} else {
					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());
					}
				}
				System.out.println("============================================");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != client) {
				client.close();
			}
			if (null != node) {
				node.close();
			}
		}
	}
}


你可能感兴趣的:(分布式搜索Elasticsearch——MultiSearch)