027 elasticsearch查询数据-Java原生客户端

文章目录

1步骤
1.创建一个client对象
2.使用client的search方法实现查询操作
3.需要SearchRequest参数
4.request对象中需要有索引的名称
5.request对象中需要包含请求体
6.请求体中包含查询条件,分页条件、过滤条件、高亮条件等
7.执行查询
8.从查询结果中取数据
2返回结果
total().value属性:命中的所有记录数
默认是有分页的,默认返回前10条数据
文档是包含在searchHits数组中
3查询条件
封装到SearchRequest对象中,其中查询条件需要封装到SearchSourceBuilder中
其中包含:
查询条件:QueryBuilders工具类创建
分页信息:from、size
过滤条件:filter实现过滤,过滤条件可以有多个

java 复制代码
package com.xd.cubemall.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.stream.Stream;

public class IndexSearchTest {
    private RestHighLevelClient client;

    @BeforeEach
    public void init() {
        //创建一个client对象
        client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("1.1.1.1",9200),
                new HttpHost("2.2.2.2",9200),
                new HttpHost("3.3.3.3",9200)
        ));
    }

    @Test
    public void searchIndex1() throws Exception {
        //http://1.1.1.1:9200/hello1/_search
        //{
        //"query":{"match_all":{}}
        //}
        SearchRequest request = new SearchRequest("hello1")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())
                );
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //从response对象中取结果
        SearchHits hits = response.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("总记录数:" + total);
        SearchHit[] searchHits = hits.getHits();
        Stream.of(searchHits).forEach(e-> System.out.println(e.getSourceAsString()));

    }
}
java 复制代码
package com.xd.cubemall.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.stream.Stream;

public class IndexSearchTest {
    private RestHighLevelClient client;

    @BeforeEach
    public void init() {
        //创建一个client对象
        client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("1.1.1.1",9200),
                new HttpHost("2.2.2.2",9200),
                new HttpHost("3.3.3.3",9200)
        ));
    }

    @Test
    public void searchIndex1() throws Exception {
        //http://1.1.1.1:9200/hello1/_search
        //{
        //"query":{"match_all":{}}
        //}
        SearchRequest request = new SearchRequest("hello1")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())
                );
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //从response对象中取结果
        SearchHits hits = response.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("总记录数:" + total);
        SearchHit[] searchHits = hits.getHits();
        Stream.of(searchHits).forEach(e-> System.out.println(e.getSourceAsString()));

    }


    @Test
    public void termQuery() throws Exception {
        SearchRequest request = new SearchRequest("hello1")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.termQuery("title","品牌"))
                );

        printResult(request);


    }

    private void printResult(SearchRequest request) throws Exception {
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //从response对象中取结果
        //从response对象中取结果
        SearchHits hits = response.getHits();
        long total = hits.getTotalHits().value;
        System.out.println("总记录数:" + total);
        SearchHit[] searchHits = hits.getHits();
        Stream.of(searchHits).forEach(e-> {
            System.out.println(e.getSourceAsString());
            System.out.println(e.getHighlightFields());
        });
    }


    @Test
    public void searchPage() throws Exception {
        SearchRequest request = new SearchRequest("hello1")
                .source(new SearchSourceBuilder()
                        //查询条件
                        .query(QueryBuilders.matchQuery("title","明星开饭店频翻车奔跑吧"))
                        //过滤
                        .postFilter(QueryBuilders.termQuery("title","明星"))
                        .postFilter(QueryBuilders.termQuery("content","围城"))
                        //高亮显示
                        .highlighter(new HighlightBuilder()
                                .field("title")
                                .field("content")
                                .preTags("<em>")
                                .postTags("</em>")
                        )
                        //分页条件
                        .from(0)
                        .size(30)
                );
        printResult(request);
    }

}
相关推荐
it噩梦5 小时前
es 中 terms set 使用
大数据·elasticsearch
喝醉酒的小白7 小时前
Elasticsearch 配置文件
大数据·elasticsearch·搜索引擎
missay_nine11 小时前
Elasticsearch
大数据·elasticsearch·搜索引擎
it噩梦12 小时前
深度分析 es multi_match 中most_fields、best_fields、cross_fields区别
java·elasticsearch
喝醉酒的小白13 小时前
ES 集群 A 和 ES 集群 B 数据流通
大数据·elasticsearch·搜索引擎
炭烤玛卡巴卡13 小时前
初学elasticsearch
大数据·学习·elasticsearch·搜索引擎
it噩梦13 小时前
es 中使用update 、create 、index的区别
大数据·elasticsearch
Mitch31115 小时前
【漏洞复现】CVE-2015-3337 Arbitrary File Reading
elasticsearch·网络安全·docker·漏洞复现
Mitch31115 小时前
【漏洞复现】CVE-2015-5531 Arbitrary File Reading
web安全·elasticsearch·网络安全·docker·漏洞复现
喝醉酒的小白17 小时前
Elasticsearch(ES)监控、巡检及异常指标处理指南
大数据·elasticsearch·搜索引擎