1. 程式人生 > >分散式搜尋Elasticsearch——QueryBuilders.matchPhraseQuery

分散式搜尋Elasticsearch——QueryBuilders.matchPhraseQuery

        ES源代碼中對matchPhraseQuery的描述如下所示:

    /**
     * Creates a text query with type "PHRASE" for the provided field name and text.
     *
     * @param name The field name.
     * @param text The query text (to be analyzed).
     */
    public static MatchQueryBuilder matchPhraseQuery(String name, Object text) {
        return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE);
    }

        先看示例:
/**
 * @author Geloin
 */
package com.geloin.esample.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.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.geloin.esample.BaseTest;
import com.geloin.esample.entity.Person;

/**
 * @author Geloin
 * 
 */
public class MatchPhraseQueryTest extends BaseTest {

	@Test
	public void matchPhraseQuery() {
		try {
			// 建立索引
			BulkRequestBuilder builder = client.prepareBulk();

			for (int i = 0; i < 5; 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 qb = QueryBuilders.matchPhraseQuery("name", "小別克老");
			SearchResponse searchResponse = client.prepareSearch(index)
					.setTypes(type).setQuery(qb).setFrom(0).setSize(12)
					.execute().actionGet();

			SearchHits hits = searchResponse.getHits();
			if (null == hits || hits.totalHits() == 0) {
				log.error("使用\"小別克老\"沒有查詢到任何結果!");
			} else {
				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());
				}
			}
			
			// 檢索
			QueryBuilder qb1 = QueryBuilders.matchPhraseQuery("name", "小別克聽");
			SearchResponse searchResponse1 = client.prepareSearch(index)
					.setTypes(type).setQuery(qb1).setFrom(0).setSize(12)
					.execute().actionGet();
			
			SearchHits hits1 = searchResponse1.getHits();
			if (null == hits1 || hits1.totalHits() == 0) {
				log.error("使用\"小別克聽\"沒有查詢到任何結果!");
				return;
			} else {
				for (SearchHit hit : hits1) {
					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());
				}
			}

			Thread.sleep(1000000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

        你會發現,使用“小別克老”沒有查詢出任何結果,而使用“小別克聽”則查詢出了我們需要的結果,這便是matchPhraseQuery和matchQuery等的區別,在使用matchQuery等時,即使你傳入的是“小別克老”,在執行查詢時,“小別克老”會被分詞器分詞,例如paoding解析成“小別/別克/老”,而使用matchPhraseQuery時,“小別克老”並不會被分詞器分詞,而是直接以一個短語的形式查詢,而如果你在建立索引所使用的field的value中沒有這麼一個短語(順序無差,且連線在一起),那麼將查詢不出任何結果。