首页 > 其他分享 >es中几种客户端的理解

es中几种客户端的理解

时间:2023-03-25 17:11:06浏览次数:34  
标签:return searchSourceBuilder org 几种 query new import es 客户端

  在项目中,jestClient还有在使用,RestHighLevelClient没有怎么被使用。现在对其做一个对比,方便技术使用上方便切换。

 

一:JestClient

1.说明

  JestClient是一款基于HTTP实现的ES客户端,在RestHighLevelClient出现之前,它填补了ES缺少HTTP REST接口客户端的空白。JestClient具有一定的ES服务端版本兼容性,支持数据的同步和异步操作,同时,它也提供了索引时和搜索结果的POJO映射机制,即OR-Mapping功能。但是目前主键被RestHighLevelClient取代

 

2.配置客户端

package com.jun.fastpro.config;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * jestClient的配置
 */
@Data
@Configuration
@ConfigurationProperties("elasticsearch")
public class JestClientElasticConfig {

    /**
     * 基础服务ES地址
     */
    private List<String> basicUrls;

    /**
     * 中心ES地址
     */
    private List<String> centerUrls;

    @Bean("basicJestClient")
    public JestClient basicJestClient() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder(basicUrls)
                .multiThreaded(true)
                .build());
        return factory.getObject();
    }

    @Bean("centerJestClient")
    public JestClient centerJestClient() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder(centerUrls)
                .multiThreaded(true)
                .build());
        return factory.getObject();
    }
}

 

3.使用

    /**
     * 查询
     */
    public <T> List<T> getByParams(JestClient jestClient, EsQuery<T> query) {
        try {
            String index = query.getIndex();
            if (StringUtils.isBlank(index)) {
                throw new RuntimeException("索引不能为空");
            }
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder bool = QueryBuilders.boolQuery();
            Map<String, Object> params = query.getParams();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String name = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof List || value instanceof Set) {
                    bool.filter(QueryBuilders.termsQuery(name, (Collection<?>) value));
                } else {
                    bool.filter(QueryBuilders.termQuery(entry.getKey(), entry.getValue()));
                }
            }
            searchSourceBuilder.query(bool);
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(1000);
            log.info("query json:{}", searchSourceBuilder.toString());
            Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(index).addType("_doc").build();
            SearchResult result = jestClient.execute(search);
            return result.getSourceAsObjectList(query.getClazz(), true);
        } catch (Exception e) {
            log.error("查询es异常", e);
            return null;
        }
    }

 

二:HighLevelClient

1.配置

package com.jun.fastpro.config;


import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * restHighLevelClient配置
 */
@Data
@Configuration
@ConfigurationProperties("highlevel")
public class HighLevelConfig {

    private List<String> uris;

    //配置高级客户端
    @Bean(name = "highClient")
    public RestHighLevelClient initSimpleClient() {
        HttpHost[] httpHosts = uris.stream().map(url -> {
            String[] hostParts = url.split(":");
            String host = hostParts[0];
            int port = Integer.parseInt(hostParts[1]);
            return new HttpHost(host, port, HttpHost.DEFAULT_SCHEME_NAME);
        }).toArray(HttpHost[]::new);

        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }

}

 

2.使用

    /**
     * 公共的查询
     */
    private SearchResponse commonQuery(String index, QueryBuilder queryBuilder, int from, int size) {
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        searchSourceBuilder.from(from);
        searchSourceBuilder.size(size);
        searchRequest.source(searchSourceBuilder);
        try {
            return highClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

标签:return,searchSourceBuilder,org,几种,query,new,import,es,客户端
From: https://www.cnblogs.com/juncaoit/p/17255116.html

相关文章