ETJava Beta | Java    注册   登录
  • elasticsearch 中文分词 - 查询中文分词

    发表于 2024-04-09 22:23:44     阅读(216)     博客类别:Elasticsearch

    elasticsearch 中文分词 - 查询中文分词

     

    查询时需要指定索引名称,索引类型 同时还要指定分词器 smartcn

     

    package com.et.index;
    
    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.TransportAddress;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    import org.elasticsearch.search.sort.SortOrder;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    /**
     * @Author: ETJAVA
     * @CreateTime: 2024-04-09  09:18
     * @Description: TODO 查询索引
     * @Version: 1.0
     */
    public class SearchIndex2 {
    
        private TransportClient client;
    
        private static String host="192.168.199.126";
        private static int port=9300; // 程序连接的端口
    
        // 配置settings 集群相关
        private static Settings.Builder settings = Settings.builder().put("cluster.name","my-application");
    
    
        @Before
        public void init() throws UnknownHostException {
            client = new PreBuiltTransportClient(settings.build())
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
    
        }
    
        @After
        public void close(){
            if(client!=null){
                client.close();
            }
        }
    
        /**
         * 单字段分词查询 - 中文分词
         matchQuery
         */
        @Test
        public void search(){
            // prepareSearch 可以同时查询多个索引名称,使用逗号隔开
            SearchRequestBuilder srb=client.prepareSearch("film2").setTypes("dongzuo");
            // 设置查询所有 actionGet  analyzer("smartcn")指定分词器
            SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title","战星球").analyzer("smartcn"))
                    .execute().actionGet();
            // 获取返回结果
            SearchHits hits=sr.getHits();
            for(SearchHit hit:hits){
                System.out.println(hit.getSourceAsString());
            }
        }
    
        /*
        * 多字段查询 - 中文分词
        multiMatchQuery
        * */
        @Test
        public void search2(){
            // prepareSearch 可以同时查询多个索引名称,使用逗号隔开
            SearchRequestBuilder srb=client.prepareSearch("film2").setTypes("dongzuo");
            // 设置查询所有 actionGet  analyzer("smartcn")指定分词器
            // multiMatchQuery 指定多字段查询 多个字段使用逗号隔开
            SearchResponse sr=srb.setQuery(QueryBuilders.multiMatchQuery("生化星球", "title","content").analyzer("smartcn"))
                    // 指定要查询的列
                    .setFetchSource(new String[]{"title","price"}, null)
                    .execute()
                    .actionGet();
            // 获取返回结果
            SearchHits hits=sr.getHits();
            for(SearchHit hit:hits){
                System.out.println(hit.getSourceAsString());
            }
        }
    }