注:该文项目基础为分布式搜索Elasticsearch——项目过程(一)分布式搜索Elasticsearch——项目过程(二),项目骨架可至这里下载。

ES源代码中对matchPhrasePrefixQuery的描述如下所示:

  1. /**
  2. * Creates a match query with type "PHRASE_PREFIX" for the provided field name and text.
  3. *
  4. * @param name The field name.
  5. * @param text The query text (to be analyzed).
  6. */
  7. public static MatchQueryBuilder matchPhrasePrefixQuery(String name, Object text) {
  8. return new MatchQueryBuilder(name, text).type(MatchQueryBuilder.Type.PHRASE_PREFIX);
  9. }

如果你调用matchPhrasePrefixQuery时,text为中文,那么,很大可能是一种状况:你会发现,matchPhraseQuery和matchPhrasePrefixQuery没有任何差别。而当text为英文时,差别就显现出来了:matchPhraseQuery的text是一个英文单词,而matchPhrasePrefixQuery的text则无这一约束,你可以从一个英文单词中抽几个连接在一起的字母进行查询。

示例代码如下所示:

  1. /**
  2. * @author Geloin
  3. */
  4. package com.gsoft.gsearch.util;
  5. import java.util.UUID;
  6. import junit.framework.Assert;
  7. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  8. import org.elasticsearch.action.bulk.BulkResponse;
  9. import org.elasticsearch.action.index.IndexRequest;
  10. import org.elasticsearch.action.search.SearchResponse;
  11. import org.elasticsearch.index.query.QueryBuilder;
  12. import org.elasticsearch.index.query.QueryBuilders;
  13. import org.elasticsearch.search.SearchHit;
  14. import org.elasticsearch.search.SearchHits;
  15. import org.junit.Test;
  16. import com.gsoft.gsearch.BaseTest;
  17. import com.gsoft.gsearch.entity.Person;
  18. /**
  19. * 以短语形式查询,查询时关键字不会被分词,而是直接以一个字符串的形式查询
  20. *
  21. * @author Geloin
  22. *
  23. */
  24. public class MatchPhrasePrefixQueryTest extends BaseTest {
  25. @Test
  26. public void matchPhrasePrefixQuery() {
  27. try {
  28. // 创建索引
  29. BulkRequestBuilder builder = client.prepareBulk();
  30. for (int i = 0; i < 2; i++) {
  31. Person p = new Person();
  32. p.setId(UUID.randomUUID().toString());
  33. p.setAge(20);
  34. p.setIsStudent(false);
  35. p.setSex("男");
  36. p.setName("Zhangsan wang");
  37. String source = ElasticSearchUtil.BeanToJson(p);
  38. IndexRequest request = client.prepareIndex().setIndex(index)
  39. .setType(type).setId(p.getId()).setSource(source)
  40. .request();
  41. builder.add(request);
  42. }
  43. BulkResponse bResponse = builder.execute().actionGet();
  44. if (bResponse.hasFailures()) {
  45. Assert.fail("创建索引出错!");
  46. }
  47. // 检索
  48. QueryBuilder qb = QueryBuilders.matchPhraseQuery("name", "wa");
  49. SearchResponse searchResponse = client.prepareSearch(index)
  50. .setTypes(type).setQuery(qb).setFrom(0).setSize(12)
  51. .execute().actionGet();
  52. SearchHits hits = searchResponse.getHits();
  53. if (null == hits || hits.totalHits() == 0) {
  54. log.error("使用matchPhraseQuery(\"name\", \"<span style="font-size:14px;">wa</span>\")没有查询到任何结果!");
  55. } else {
  56. for (SearchHit hit : hits) {
  57. String json = hit.getSourceAsString();
  58. Person newPerson = mapper.readValue(json, Person.class);
  59. System.out.println("name\t\t" + newPerson.getName());
  60. System.out.println("sex\t\t" + newPerson.getSex());
  61. System.out.println("age\t\t" + newPerson.getAge());
  62. System.out.println("isStudent\t\t"
  63. + newPerson.getIsStudent());
  64. }
  65. }
  66. System.out.println("===================================================");
  67. // 检索
  68. QueryBuilder qb1 = QueryBuilders.matchPhrasePrefixQuery("name", "wa");
  69. SearchResponse searchResponse1 = client.prepareSearch(index)
  70. .setTypes(type).setQuery(qb1).setFrom(0).setSize(20)
  71. .execute().actionGet();
  72. SearchHits hits1 = searchResponse1.getHits();
  73. if (null == hits1 || hits1.totalHits() == 0) {
  74. log.error("使用matchPhrasePrefixQuery(\"name\", \"wa\")没有查询到任何结果!");
  75. return;
  76. } else {
  77. for (SearchHit hit : hits1) {
  78. String json = hit.getSourceAsString();
  79. Person newPerson = mapper.readValue(json, Person.class);
  80. System.out.println("name\t\t" + newPerson.getName());
  81. System.out.println("sex\t\t" + newPerson.getSex());
  82. System.out.println("age\t\t" + newPerson.getAge());
  83. System.out.println("isStudent\t\t"
  84. + newPerson.getIsStudent());
  85. }
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally {
  90. if (null != client) {
  91. client.close();
  92. }
  93. if (null != node) {
  94. node.close();
  95. }
  96. }
  97. }
  98. }

你会发现,使用matchPhraseQuery并未查询出结果,而matchPhrasePrefixQuery查询出的,则是我们需要的结果。

http://blog.csdn.net/geloin/article/details/8939387

分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery的更多相关文章

  1. 分布式搜索ElasticSearch构建集群与简单搜索实例应用

    分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...

  2. 分布式搜索ElasticSearch单机与服务器环境搭建

    从上方插件官网中下载适合的dist包,然后解压.进入bin目录,可以看到一堆sh脚本.在bin目录下创建一个test.sh: bin=/home/csonezp/Dev/elasticsearch-j ...

  3. 分布式搜索elasticsearch 基本概念

    ElasticSearch官网:http://www.elasticsearch.org/ 先上一张elasticsearch的整体框架图: ElasticSearch是基于Lucene开发的分布式搜 ...

  4. 分布式搜索elasticsearch几个概念解析

    原文链接:http://blog.csdn.net/july_2/article/details/24367177 介绍下es的几个概念:cluster     代表一个集群,集群中有多个节点,其中有 ...

  5. 分布式搜索elasticsearch配置文件详解

    elasticsearch的config文件夹里面有两个配置文件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来 ...

  6. (转)分布式搜索Elasticsearch——配置

    配置文件位于%ES_HOME%/config/elasticsearch.yml文件中,用Editplus打开它,你便可以进行配置.         所有的配置都可以使用环境变量,例如: node.r ...

  7. 分布式搜索Elasticsearch增、删、改、查操作深入详解

    引言: 对于刚接触ES的童鞋,经常搞不明白ES的各个概念的含义.尤其对“索引”二字更是与关系型数据库混淆的不行.本文通过对比关系型数据库,将ES中常见的增.删.改.查操作进行图文呈现.能加深你对ES的 ...

  8. 分布式搜索elasticsearch 文献检索索引 入门

    1.首先,例如,下面的数据被提交给ES该指数 {"number":32768,"singer":"杨坤","size": ...

  9. 分布式搜索elasticsearch 环境搭建

    1.elasticsearch安装 elasticsearch的安装超级easy,解压即用(要事先安装好java环境). 到官网 http://www.elasticsearch.org下载最新版的 ...

随机推荐

  1. 查看Aix系统配置命令

    prtconf#topas http://baike.baidu.com/link?url=QruEnlfCqyoqQ565LicyKxIGMQYSkVesj6j9GzHWwzpDOagXtuprhT ...

  2. 滑动选择日期(基于sui-mobile的移动端)

    $(page).on('touchmove','#touchMoveTime',function (event) { touchMove(event); }); scrollBarInit(); // ...

  3. =====关于swing的一些收集-swing大收集======

    一篇经典的 介绍netbeans中swing 应用程序框架的文章 http://blog.csdn.net/tangwing/article/details/5745075 Swing外观框架 Bea ...

  4. 用css3写出来的进度条

    夜深了,废话不多说,先上代码: <style> * { box-sizing: border-box } .wrapper { width: 350px; margin: 200px au ...

  5. Spring MVC - log4j 配置

    http://blog.csdn.net/yhqbsand/article/details/8764388

  6. 从零学起PHP

    数据库连接conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root", ...

  7. 遍历字典时用与不用iter的区别

    遍历字典时用与不用iter的区别 遍历字典的时候一般会用这三个方法:keys(),values(),items() 同时,它们各自都有升级版的方法:iterkeys(),itervalues(),it ...

  8. SQL动态更新表字段 传入字段可能为空

    小技巧: 项目组有修改产品的基本信息字段 但有时候传入的字段可能为空 也可能不为空  动态修改表中字段. USE [BetaProductMarket_DB] GO )) BEGIN DROP PRO ...

  9. IOS 学习参考

    IOS 开发 http://code4app.com/ios/%E5%AE%9E%E6%97%B6%E6%9B%B4%E6%96%B0%E7%9A%84%E6%9B%B2%E7%BA%BF%E5%9B ...

  10. GUIText的淡入淡出

    单击按键“A”(随意改变),可以控制GUIText马上显示出来,然后淡出:按住按键“A”,可以使GUIText淡入,如果抬起按键则淡出. FadeInOut.cs using UnityEngine; ...