重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“本地实现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; @Slf4j public 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(); Listmust = 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查询”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!