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);
    }

}
相关推荐
执键行天涯2 小时前
【工具使用】VSCode如何将本地项目关联到远程的仓库 (vscode本地新项目与远程仓库建立链接)
ide·vscode·elasticsearch
再拼一次吧5 小时前
Elasticsearch
大数据·elasticsearch·搜索引擎
、十一、9 小时前
Linux中ES的安装
linux·运维·elasticsearch
苏少朋14 小时前
Es概念理解 ▎Es索引库操作 ▎Es文档操作
大数据·elasticsearch·搜索引擎
小小工匠14 小时前
ElasticSearch - Bucket Selector使用指南
elasticsearch·bucket sort
cab514 小时前
聊一聊Elasticsearch的一些基本信息
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客14 小时前
GraphQL 与 Elasticsearch 相遇:使用 Hasura DDN 构建可扩展、支持 AI 的应用程序
大数据·后端·elasticsearch·搜索引擎·全文检索·graphql·1024程序员节
攒了一袋星辰14 小时前
分布式搜索引擎elasticsearch操作文档操作介绍
分布式·elasticsearch·搜索引擎
武子康1 天前
大数据-186 Elasticsearch - ELK 家族 Logstash Input插件 JDBC & syslog
java·大数据·elk·elasticsearch·搜索引擎·全文检索
CNAHYZ1 天前
Elastic Stack - FileBeat 入门浅体验
java·大数据·elasticsearch·es·filebeat