elasticsearch之RestClient查询文档

快速入门

我们通过match_all来演示下基本的API,先看请求DSL的组织:

elasticsearch之RestClient查询文档_第1张图片elasticsearch之RestClient查询文档_第2张图片

结果解析

elasticsearch之RestClient查询文档_第3张图片 RestAPI中其中构建DSL是通过HighLevelRestClient中的resource()来实现的,其中包含了查询、排序、分页、高亮等所有功能:

RestAPI中其中构建查询条件的核心部分是由一个名为QueryBuilders的工具类提供的,其中包含了各种查询方法:

elasticsearch之RestClient查询文档_第4张图片

总结——基本步骤

创建SearchRequest对象 准备Request.source(),也就是DSL。

QueryBuilders来构建查询条件 传入Request.source() 的 query() 方法

发送请求,得到结果 解析结果(参考JSON结果,从外到内,逐层解析) 

match查询

elasticsearch之RestClient查询文档_第5张图片

精确查询

elasticsearch之RestClient查询文档_第6张图片

复合查询

elasticsearch之RestClient查询文档_第7张图片

附源代码

@SpringBootTest
public class HotelSearchTest {

    private RestHighLevelClient client;
    @BeforeEach
    void setUp() {
        this.client=new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://8.130.89.67:9200")
        ));
    }

//    TODO match_all
    @Test
    public void testMatchAll() throws IOException {
        // 1.准备Request
        SearchRequest request=new SearchRequest("hotel");
        // 2.组织DSL参数
        request.source().query(QueryBuilders.matchAllQuery());
        // 3.发送请求,得到响应结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        responseJson(response);
    }
//    TODO match
    @Test
    public void testMatch() throws IOException {
        // 1.准备Request
        SearchRequest request=new SearchRequest("hotel");
        // 2.组织DSL参数
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        // 3.发送请求,得到响应结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        responseJson(response);
    }
//    todo 布尔查询
    @Test
    public void testBoolQuery() throws IOException {
        // 1.准备Request
        SearchRequest request=new SearchRequest("hotel");
        //获取DSL参数
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("city","杭州"));
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("prise").gt(250));
        // 2.组织DSL参数
        request.source().query(boolQueryBuilder);
        // 3.发送请求,得到响应结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        responseJson(response);
    }

//    todo 解析结果
    private void responseJson(SearchResponse response) {
        //解析响应结果
        SearchHits hits = response.getHits();
        //查询的结果数组
        SearchHit[] hitsHits = hits.getHits();
        //查询的总条数
        val length = hitsHits.length;
        System.err.println("总共有" + length + "条数据");
        for (SearchHit hit : hitsHits) {
            //得到source
            String asString = hit.getSourceAsString();
            System.out.println(asString);
        }
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

排序、分页

索结果的排序和分页是与query同级的参数,对应的API如下

//    todo 分页、排序
    @Test
    public  void  testPageAndOrder() throws IOException {
        SearchRequest request=new SearchRequest("hotel");
        request.source().query(QueryBuilders.matchAllQuery());
        request.source().from(0).size(10);
        request.source().sort("price", SortOrder.ASC);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        responseJson(response);
    }

高亮

高亮API包括请求DSL构建和结果解析两部分。高亮的结果处理相对比较麻烦

//    TODO 高亮显示
    @Test
    public void testHighLight() throws IOException {
        SearchRequest request=new SearchRequest("hotel");
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        request.source().highlighter(new HighlightBuilder()
        .field("name").requireFieldMatch(false));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        responseLightJson(response);
    }

    //TODO 高亮结果解析
    private void responseLightJson(SearchResponse response) {
        //解析响应结果
        SearchHits hits = response.getHits();
        //查询的结果数组
        SearchHit[] hitsHits = hits.getHits();
        //查询的总条数
        val length = hitsHits.length;
        System.err.println("总共有" + length + "条数据");
        for (SearchHit hit : hitsHits) {
            HotelDoc hotelDoc = JSON.parseObject(hit.getSourceAsString(), HotelDoc.class);
            Map highlightFieldMap=hit.getHighlightFields();
            if (!CollectionUtils.isEmpty(highlightFieldMap)){
                HighlightField name = highlightFieldMap.get("name");
                if (name!=null){
                    String s = name.getFragments()[0].string();
                    hotelDoc.setName(s);
                }
            }
            System.out.println(hotelDoc);
        }
    }

你可能感兴趣的:(elasticsearch,大数据,搜索引擎)