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

相关推荐
计算机毕设指导613 分钟前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
Gu Gu Study14 分钟前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言
Chris _data17 分钟前
二叉树oj题解析
java·数据结构
牙牙70522 分钟前
Centos7安装Jenkins脚本一键部署
java·servlet·jenkins
paopaokaka_luck30 分钟前
[371]基于springboot的高校实习管理系统
java·spring boot·后端
-一杯为品-38 分钟前
【51单片机】程序实验5&6.独立按键-矩阵按键
c语言·笔记·学习·51单片机·硬件工程
以后不吃煲仔饭42 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师43 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
The_Ticker1 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
java1234_小锋1 小时前
Elasticsearch中的节点(比如共20个),其中的10个选了一个master,另外10个选了另一个master,怎么办?
大数据·elasticsearch·jenkins