Es 学习记录

2024-01-09 09:54:48
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

文章来源:https://blog.csdn.net/weixin_42891450/article/details/135414835
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。