• ElasticSearch(七):Java操作elasticsearch基于smartcn中文分词查询


     1 package com.gxy.ESChap01;
     2 
     3 import java.net.InetAddress;
     4 
     5 import org.elasticsearch.action.search.SearchRequestBuilder;
     6 import org.elasticsearch.action.search.SearchResponse;
     7 import org.elasticsearch.client.transport.TransportClient;
     8 import org.elasticsearch.common.settings.Settings;
     9 import org.elasticsearch.common.transport.InetSocketTransportAddress;
    10 import org.elasticsearch.index.query.QueryBuilder;
    11 import org.elasticsearch.index.query.QueryBuilders;
    12 import org.elasticsearch.search.SearchHit;
    13 import org.elasticsearch.search.SearchHits;
    14 import org.elasticsearch.transport.client.PreBuiltTransportClient;
    15 import org.junit.After;
    16 import org.junit.Before;
    17 import org.junit.Test;
    18 /**
    19  * 使用smartcn分词查询
    20  * @author 郭祥跃
    21  *
    22  */
    23 public class ESQuerySmartcn {
    24     private static String host="192.168.56.3"; // 服务器地址
    25     private static int port=9300; // 端口
    26     
    27     public static final String CLUSTER_NAME = "my-application"; //集群名称
    28     
    29     private TransportClient client=null;
    30     
    31     private static final String ANALYZER="smartcn";
    32     
    33     private static Settings settings= Settings.builder()
    34             .put("cluster.name",CLUSTER_NAME)
    35             .put("client.transport.sniff", true)
    36             .build();
    37     
    38     //获取客户端
    39     @SuppressWarnings({ "resource", "unchecked" })
    40     @Before
    41     public void getClient() throws Exception {
    42         try {
    43             client = new PreBuiltTransportClient(settings)
    44                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
    45         } catch (Exception e) {
    46             // TODO Auto-generated catch block
    47             e.printStackTrace();
    48         }
    49     }
    50     
    51     //关闭客户端
    52     @After
    53     public void close() {
    54         if(client!=null) {
    55             client.close();
    56         }
    57     }
    58     /**
    59      * 条件分词查询
    60      */
    61     @Test
    62     public void search() {
    63         SearchRequestBuilder srb=client.prepareSearch("book").setTypes("kehuan");
    64         SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title", "三体").analyzer(ANALYZER))
    65                 .setFetchSource(new String[] {"title","price"},null)
    66                 .execute()
    67                 .actionGet();
    68         SearchHits hits=sr.getHits();
    69         for(SearchHit hit:hits){
    70             System.out.println(hit.getSourceAsString());
    71         }
    72     }
    73     /**
    74      * 多字段条件分词查询
    75      */
    76     @Test
    77     public void search2() {
    78         SearchRequestBuilder srb=client.prepareSearch("book").setTypes("kehuan");
    79         SearchResponse sr=srb.setQuery(QueryBuilders.multiMatchQuery("美国宇宙红岸", "title","content")
    80                 .analyzer(ANALYZER))
    81                 .setFetchSource(new String[] {"title","price"},null)
    82                 .execute()
    83                 .actionGet();
    84         SearchHits hits=sr.getHits();
    85         for(SearchHit hit:hits){
    86             System.out.println(hit.getSourceAsString());
    87         }
    88     }
    89 }

  • 相关阅读:
    Abp Zero 演示(链接)
    阿里中台战略是个伪命题(转)
    AlphaFlow智能BPM专家的博客
    绵绵用力方能久久为功 --《工程建设企业管理信息化实用案例精选》前言 -- 鲁贵卿
    业务梳理优化(政府、企业)---- 收集网上资料链接
    .NET for Apache® Spark™ 开源大数据分析工具
    Net Core 3.0 及 AspNet 3.0
    统一身份访问管理平台 (收集)-- Identity Access management platform
    SciSharp .Net 平台的人工智能,Net 如何调用 Python
    Identity Server4 及 其它 OpenId 服务器 学习
  • 原文地址:https://www.cnblogs.com/guoxiangyue/p/9640640.html
Copyright © 2020-2023  润新知