Es 学习记录

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

你可能感兴趣的:(elasticsearch,学习,java)