ElasticSearch學習(二)客戶端TransportClient簡單檢索

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();
            }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章