分布式搜索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下载最新版的 ...
随机推荐
- VS代码模板
Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\Csharp\Code\2052\Class
- JVM系列三:JVM参数设置、分析(转载)
不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java ...
- qml去标题栏
只要加入"flags: Qt.Window | Qt.FramelessWindowHint "属性就可实现去标题栏. 注意:在使用这个属性的时候要先导入QtQuick.Windo ...
- javascript 文本框中,判断回车键触发事件 兼容IE&FireFox
1.onkeypress&onkeydown区别 onkeypress 事件在用户按下并放开任何字母数字键时发生.但是系统按钮(例如:箭头键.功能键)无法得到识别. onkeydown 事件在 ...
- HTML5 Video(视频)
HTML5 Video(视频) 很多站点都会使用到视频. HTML5 提供了展示视频的标准. 检测您的浏览器是否支持 HTML5 视频: 检测 Web站点上的视频 直到现在,仍然不存在一项旨在网页上显 ...
- asp.net get server control id from javascript
var WhateverValue = document.getElementById('<%= saveValue.ClientID %>').value
- VI命令删除文件所有内容
>vi test.log >gg #光标移到第一行 >:.,$d
- php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员
本人最近做了微信支付开发,是第一次接触.其中走了很多弯路,遇到的问题也很多.为了让和我一样的新人不再遇到类似的问题,我把我的开发步骤和问题写出来,以供参考. 开发时间是2016/8/2,所以微信支付的 ...
- window.print打印指定div实例代码
window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢,请看下面的例子 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. 复制代码代码如下: <h ...
- nrm —— 快速切换 NPM 源 (附带测速功能)
以前我们介绍过cnpmjs.org和最近推出的淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,然后各个镜像各自都可能有少数包暂时没有同步 ...