• ElasticSearch7.x Bool查询Java API


    import org.apache.lucene.search.TotalHits;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.TermQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Map;
    
    /**
     * @author zhangxiaobo
     * @version 1.0
     * @description TODO
     * @date 2020/4/19 21:16
     **/
    public class TestES {
    
        private RestHighLevelClient client = EsClient.getEsClient();
    
        @Test
        public void testConnectionEs(){
            System.out.println(client);
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Test
        public void testBoolQuery(){
    
            // 搜索请求对象
            SearchRequest movies = new SearchRequest("movies");
            // 搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    
            /**
             * GET movies/_search
             * {
             *   "query": {
             *     "bool": {
             *       "must": [
             *         {
             *           "match_phrase": {
             *             "genre": "Horror"
             *           }
             *         },
             *         {
             *           "term": {
             *             "title": {
             *               "value": "wolf"
             *             }
             *           }
             *         }
             *       ]
             *     }
             *   }
             * }
             */
            //定义一个MutiMatchQueryBuilder
            MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("genre", "Horror");
            //定义一个termQuery
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "wolf");
    
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(matchPhraseQueryBuilder);
            boolQueryBuilder.must(termQueryBuilder);
    
            searchSourceBuilder.query(boolQueryBuilder);
    
            //设置源字段过滤,第一个参数结果集包括哪些字段,第二字段结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"title","genre","year"},new String[]{});
    
            //向搜索请求对象设置搜索源
            movies.source(searchSourceBuilder);
    
            //执行搜索,向ES发起http请求
            SearchResponse response = null;
            try {
                response = client.search(movies, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            // 搜索结果
            SearchHits hits = response.getHits();
            // 匹配到的总计录
            TotalHits totalHits = hits.getTotalHits();
            // 得到文档
            SearchHit[] searchHits = hits.getHits();
    
            System.out.println("总数:" + totalHits.value);
    
            for (SearchHit searchHit : searchHits) {
                //文档id
                String id = searchHit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
    
                String title = (String) sourceAsMap.get("title");
                ArrayList<String> genre = (ArrayList<String>) sourceAsMap.get("genre");
                String genres = String.join("|", genre);
                Integer year = (Integer) sourceAsMap.get("year");
    
                System.out.println("title: " + title + "; year: " + year + "; genre: " + genres);
    
    
            }
        }
    }

    ElasticSearch client 获取请看上一篇:https://www.cnblogs.com/zxbdboke/p/12763696.html

  • 相关阅读:
    Linux基础知识--目录操作
    Linux基础知识--命令的基础知识
    Linux基础知识--磁盘分区和目录结构
    PO设计模式
    SSL连接形式发送邮件
    已附件的形式发送测试报告
    Python+selenium+HTMLTestRunner邮件形式发送测试报告
    Python随笔13
    Python随笔12
    Python随笔11
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/12763769.html
Copyright © 2020-2023  润新知