ElasticSearch学习(二)客户端TransportClient简单检索

    xiaoxiao2022-07-13  175

    1、添加依赖

    <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.5.2</version> </dependency>

    2、application.properties配置文件

    # es 配置 spring.es.host=172.16.8.221 spring.es.transport.port=9300 spring.es.port=9200 spring.es.cluster.name=application spring.es.index.auto.create=true

    3、添加连接配置类

    package com.scistor.elasticsearch; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author lc * @description es 配置 * @date 2019/3/26 */ @Configuration public class TransportClientConfig { // id地址 @Value("${spring.es.host}") private String host; // 端口号 @Value("${spring.es.transport.port}") private int transportPort; // 集群名字 @Value("${spring.es.cluster.name}") private String clusterName; @Value("${spring.es.index.auto.create}") private boolean indexAutoCreate; @Bean public TransportClient getClient() throws UnknownHostException { TransportAddress node = new InetSocketTransportAddress( InetAddress.getByName(host), transportPort //ip ); Settings setttings = Settings.builder() .put("client.transport.sniff", indexAutoCreate) .put("cluster.name", clusterName).build(); TransportClient client = new PreBuiltTransportClient(setttings); client.addTransportAddress(node); return client; } }

    4、简单检索

    @Autowired private TransportClient transportClient; // 时间范围的设定 RangeQueryBuilder rangequerybuilder = QueryBuilders.rangeQuery(createTime).from(startTime).to(endTime); // 根据事件名查询 TermQueryBuilder termQuery = QueryBuilders.termQuery("c_event_id.keyword", eventId); // 查询条件封装 BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); boolBuilder.must(rangequerybuilder); boolBuilder.must(termQuery); // 请求参数封装 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.sort("c_time", SortOrder.DESC); // 按时间倒序排序 sourceBuilder.query(boolBuilder); // 如果不指定查询量范围,ES默认只返回10条,这两个参数可以根据分页情况设定 // sourceBuilder.from(0); // sourceBuilder.size(9); // 请求查询 SearchRequest searchRequest = new SearchRequest("index_org_info"); searchRequest.types("type001"); searchRequest.source(sourceBuilder); SearchResponse response = transportClient.search(searchRequest).get(); // 获取数据 SearchHits hits = response.getHits(); Map aptIdMap = jedisCluster.hgetAll(redis_aptid_key); for (SearchHit searchHit : hits.getHits()) { String json = searchHit.getSourceAsString(); }

     

    最新回复(0)