Es 学习记录

java 复制代码
import com.demo1.demo1.util.JsonUtils;

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.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * @author haiyang.shu
 * @date 2023/6/29 2:07 PM
 */
public class EsDemo {

    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200)));

        // 创建索引
//        CreateIndexRequest indexRequest = new CreateIndexRequest("order");
//        CreateIndexResponse createIndexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT);
//
//        System.out.println(createIndexResponse.isAcknowledged());

        SearchRequest request = new SearchRequest();
        request.indices("mp_lepus_test");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.from(0);
        builder.size(10);


        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        queryBuilder.must(QueryBuilders.termsQuery("order_no", "121313131","124123121"));


        queryBuilder.must(QueryBuilders.rangeQuery("create_time")
                .gte(1660863600000L)
                .lte(1661382000000L));


        builder.query(queryBuilder);




        request.source(builder);

        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] hitsHits = hits.getHits();
        for (SearchHit hit : hitsHits) {
            String sourceAsString = hit.getSourceAsString();
            MpLepusTestBean mpLepusTestBean = JsonUtils.fromJson(sourceAsString, MpLepusTestBean.class);
            System.out.println(1111);
        }
        // 关闭客户端连接
        client.close();
    }
}

RestHighLevelClient

相关推荐
路在脚下@10 分钟前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien13 分钟前
Spring Boot常用注解
java·spring boot·后端
量子-Alex37 分钟前
【多模态聚类】用于无标记视频自监督学习的多模态聚类网络
学习·音视频·聚类
吉大一菜鸡42 分钟前
FPGA学习(基于小梅哥Xilinx FPGA)学习笔记
笔记·学习·fpga开发
苹果醋31 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader2 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭2 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪2 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0072 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl