• 分布式搜索Elasticsearch——QueryBuilders.matchPhrasePrefixQuery


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

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

    [java] view plain copy
     
    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则无这一约束,你可以从一个英文单词中抽几个连接在一起的字母进行查询。

            示例代码如下所示:

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

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

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

  • 相关阅读:
    绝对路径和相对路径
    基本的文件操作
    Python2和3字符编码区别
    java开发两年了,连个java代理模式都摸不透,你怎么跳槽涨薪?
    【建议收藏】阿里P7总结的Spring注解笔记,把组件注册讲的明明白白
    面试官:你说你精通SpringBoot,你给我说一下类的自动装配吧
    面试BAT问的最多的27道MyBatis 面试题(含答案和思维导图总结)
    Springboot 框架整理,建议做开发的都看看,整理的比较详细!
    直面秋招!非科班生背水一战,最终拿下阿里等大厂offer!
    写的太细了!Spring MVC拦截器的应用,建议收藏再看!
  • 原文地址:https://www.cnblogs.com/softidea/p/5981751.html
Copyright © 2020-2023  润新知