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

posted on 2020-04-23 21:43  0x153_小波  阅读(1646)  评论(0编辑  收藏  举报