分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery
注:该文项目基础为分布式搜索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\", \"<span style="font-size:14px;">wa</span>\")没有查询到任何结果!");
- } 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();
- }
- }
- }
- }
你会发现,使用matchPhraseQuery并未查询出结果,而matchPhrasePrefixQuery查询出的,则是我们需要的结果。
http://blog.csdn.net/geloin/article/details/8939387
分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery的更多相关文章
- 分布式搜索ElasticSearch构建集群与简单搜索实例应用
分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...
- 分布式搜索ElasticSearch单机与服务器环境搭建
从上方插件官网中下载适合的dist包,然后解压.进入bin目录,可以看到一堆sh脚本.在bin目录下创建一个test.sh: bin=/home/csonezp/Dev/elasticsearch-j ...
- 分布式搜索elasticsearch 基本概念
ElasticSearch官网:http://www.elasticsearch.org/ 先上一张elasticsearch的整体框架图: ElasticSearch是基于Lucene开发的分布式搜 ...
- 分布式搜索elasticsearch几个概念解析
原文链接:http://blog.csdn.net/july_2/article/details/24367177 介绍下es的几个概念:cluster 代表一个集群,集群中有多个节点,其中有 ...
- 分布式搜索elasticsearch配置文件详解
elasticsearch的config文件夹里面有两个配置文件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来 ...
- (转)分布式搜索Elasticsearch——配置
配置文件位于%ES_HOME%/config/elasticsearch.yml文件中,用Editplus打开它,你便可以进行配置. 所有的配置都可以使用环境变量,例如: node.r ...
- 分布式搜索Elasticsearch增、删、改、查操作深入详解
引言: 对于刚接触ES的童鞋,经常搞不明白ES的各个概念的含义.尤其对“索引”二字更是与关系型数据库混淆的不行.本文通过对比关系型数据库,将ES中常见的增.删.改.查操作进行图文呈现.能加深你对ES的 ...
- 分布式搜索elasticsearch 文献检索索引 入门
1.首先,例如,下面的数据被提交给ES该指数 {"number":32768,"singer":"杨坤","size": ...
- 分布式搜索elasticsearch 环境搭建
1.elasticsearch安装 elasticsearch的安装超级easy,解压即用(要事先安装好java环境). 到官网 http://www.elasticsearch.org下载最新版的 ...
随机推荐
- C++多线程技术windows常用方法
随着计算机CPU计算能力快速提高,计算机的处理性能和并行性能力也大大提升.那么,一味使用运行时标准库的C++语言也应该开始支持多线程技术.今天,我为大家带来了C++在windows平台下的常用多线程方 ...
- CVTE面试总结
刚面完了CVTE的一面,感觉完败,现总结. 一:准备不充分 这个跟时间仓促也有关系.昨天下午收到通知,晚上九点多回来填写给定格式的简历,题目也比较多.由于时间较仓促,那些问题的答案没有过多斟酌.但糟糕 ...
- 最新模仿ios版微信应用源码
这个是刚刚从那个IOS教程网http://ios.662p.com分享来的,也是一个很不错的应用源码,仿微信基本功能.基于XMPP服务器的即时通信以及交友客户端. ----第一期代码的功能如下---- ...
- ubuntu配置多个DNS
Ubuntu设置了静态IP地址,设置DNS,打开/etc/resolv.conf cat /etc/resolv.conf# Dynamic resolv.conf(5) file for glibc ...
- Angular之【form提交问题】
前端页面是这样: <form class="form-horizontal" role="form" name="LoginForm" ...
- php5.3不支持 ereg、ereg_replace等函数问题,如提示:Deprecated: Function ereg() is deprecated
在php5.3中,正则函数ereg_replace已经废弃,而dedecms还继续用.有两个方案可以解决以上问题: 1.把php版本换到v5.3下. 2.继续使用v5.3,修改php.ini文件 ;e ...
- foxtable使用笔记
1.设置关联表窗口的列的顺序:Tables("award.people").SetColVisibleWidth("name|100|sex|100|job|100|ag ...
- javac mac 终端乱码
java和javac在简体中文的Mac OSX的终端(Terminal.app)环境下,默认是以GBK编码的中文输出各种诸如语法错误,数组访问越界之类的信息.但是,Mac的终端的默认编码是UTF-8, ...
- 2015-4-2的阿里巴巴笔试题:乱序的序列保序输出(bit数组实现hash)
分布式系统中的RPC请求经常出现乱序的情况.写一个算法来将一个乱序的序列保序输出.例如,假设起始序号是1,对于(1, 2, 5, 8, 10, 4, 3, 6, 9, 7)这个序列,输出是:123, ...
- HashMap加入数据后,会自动根据首字母排序
1.Map<String, ArrayList<XX>> entityHashMap = new HashMap<>(); 然后增加一些数据,会发现根据String ...