千家信息网

本地实现es查询

发表于:2024-11-19 作者:千家信息网编辑
千家信息网最后更新 2024年11月19日,本篇内容介绍了"本地实现es查询"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!import cn.j
千家信息网最后更新 2024年11月19日本地实现es查询

本篇内容介绍了"本地实现es查询"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

import cn.jpush.statistics.elasticsearch.ReportDO;import cn.jpush.statistics.util.JsonUtil;import com.google.common.collect.Lists;import lombok.extern.slf4j.Slf4j;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.search.SearchRequestBuilder;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.TransportAddress;import org.elasticsearch.index.query.*;import org.elasticsearch.search.SearchHit;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.joda.time.DateTime;import org.joda.time.DateTimeFieldType;import org.joda.time.LocalDate;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Date;import java.util.List;import static org.elasticsearch.index.query.QueryBuilders.matchQuery;@Slf4jpublic class EsClient {    public static void main(String[] args) throws UnknownHostException {            esSearch();    }    private static void esSearch() throws UnknownHostException {        //指定集群        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff",false).build();        // 创建访问es的客户端        TransportClient client = new PreBuiltTransportClient(settings)                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));//        TermQueryBuilder query = QueryBuilders.termQuery("channel", "GDT");        BoolQueryBuilder query = QueryBuilders.boolQuery();        List must = query.must();        must.add(QueryBuilders.termsQuery("channel", "GDT".toLowerCase()));        DateTime start = new DateTime(2017,1,1,0,0,0);        DateTime end = new DateTime(2020,9,1,0,0,0);        must.add(QueryBuilders.termQuery( "advertiser_id", 12));        must.add(QueryBuilders.termQuery( "product_id", 98));        RangeQueryBuilder rangeQueryBuilder = QueryBuilders                .rangeQuery("query_time")                .gt(start.getMillis())                .lte(end.getMillis())                .format("epoch_millis");        must.add(rangeQueryBuilder);//        must.add(QueryBuilders.termsQuery("advertiser_id", Lists.newArrayList(243)));        SearchRequestBuilder builder = client.prepareSearch("ad-report-v3-20*")                .setTypes("report")                .setQuery(query);        log.info("elastic.list.query.string: \n\n{}\n\n",builder.toString());        SearchResponse response = builder//                .setSize(3)                .get();        for (SearchHit hit : response.getHits()) {            String sourceAsString = hit.getSourceAsString();            ReportDO reportDO = JsonUtil.parseObject(sourceAsString, ReportDO.class);            System.out.println(JsonUtil.toJsonString(reportDO));        }        client.close();    }}

"本地实现es查询"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0