注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)和分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。
ES源代码中对matchPhrasePrefixQuery的描述如下所示:
/**
* Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.
*
* @param name The field name.
* @param text The query text (to be analyzed).
*/
public static MatchQueryBuilder matchPhrasePrefixQuery(String name, Object text) {
return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE_PREFIX);
}
如果你 调用matchPhrasePrefixQuery时,text为中文,那么,很大可能是一种状况:你会发现,matchPhraseQuery和matchPhrasePrefixQuery没有任何差别。而当text为英文时,差别就显现出来了:matchPhraseQuery的text是一个英文单词,而matchPhrasePrefixQuery的text则无这一约束,你可以从一个英文单词中抽几个连接在一起的字母进行查询。
示例代码如下所示:
/**
* @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.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 MatchPhrasePrefixQueryTest extends BaseTest {
@Test
public void matchPhrasePrefixQuery() {
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("Zhangsan wang");
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", "wa");
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("使用matchPhraseQuery(\"name\", \"wa\")没有查询到任何结果!");
} 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());
}
}
System.out.println("===================================================");
// 检索
QueryBuilder qb1 = QueryBuilders.matchPhrasePrefixQuery("name", "wa");
SearchResponse searchResponse1 = client.prepareSearch(index)
.setTypes(type).setQuery(qb1).setFrom(0).setSize(20)
.execute().actionGet();
SearchHits hits1 = searchResponse1.getHits();
if (null == hits1 || hits1.totalHits() == 0) {
log.error("使用matchPhrasePrefixQuery(\"name\", \"wa\")没有查询到任何结果!");
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());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != client) {
client.close();
}
if (null != node) {
node.close();
}
}
}
}
你会发现,使用matchP
hraseQuery并未查询出结果,而matchPhrasePrefixQuery查询出的,则是我们需要的结果。