JestClient

jest是一批操作es的http api接口,你可以像使用普法方法一下操作es,在springboot2.3.0之前,JestClient是支持自动注入的,而在2.3.0之后,你必须为JestClient写一个组件类,通过注入组件类来使用jest,这一点有些麻烦了。

版本对应

Jest VersionElasticsearch Version
>= 6.0.06
>= 5.0.05
>= 2.0.02
0.1.0 - 1.0.01
<= 0.0.6< 1

maven依赖

        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.7</version>
        </dependency>

初始化

/**
 * springboot2.3.0之后不支持自动注册,只能手动写注册配置文件.
 */
@Component
public class JestClientConfig {
    @Bean
    public io.searchbox.client.JestClient getJestCline() {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder("http://localhost:9200")
                .multiThreaded(true)
                .build());
        return factory.getObject();
    }
}

使用封装

package com.example.es_test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.*;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JestUtil {
    private static final Logger log = LoggerFactory.getLogger(JestUtil.class);
    private static JestClient client;
    private static final String CLUSTER_NAME = "myEs";
    private static  HttpClientConfig httpClientConfig;
    static {
        httpClientConfig = new HttpClientConfig.Builder("http://192.168.10.183:9200/")
                .multiThreaded(true).connTimeout(Integer.MAX_VALUE).readTimeout(Integer.MAX_VALUE).defaultMaxTotalConnectionPerRoute(2)
                .gson(new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        .create()).build();
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(httpClientConfig);
        client = factory.getObject();
    }


    /**
     * 创建索引并存入值
     * @param o 数据  index 索引名 id 主键  type数据库
     * @return  jestResult
     */
    public static <T> JestResult createIndex(T o, String index, String type, String id) {
        Index index1 = new Index.Builder(o).index(index).type(type).id(id).build();
        JestResult jestResult = null ;
        try {
            jestResult = client.execute(index1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jestResult;
    }



    /**
     * 批量入库
     * @param index
     * @param type
     * @param os
     * @param <T>
     */
    public static  <T> void bulkIndex(String index, String type , List<T> os) {
        List<Index> actions =new ArrayList<>();
        for (T o : os) {
            actions.add(new Index.Builder(o).build());
        }
        Bulk bulk = new Bulk.Builder()
                .defaultIndex(index)
                .defaultType(type)
                .addAction(actions).build();
        try {
            client.execute(bulk);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询全部
     * @param index :文档在哪存放
     * @return
     */
    public static  <T> List<SearchResult.Hit<T,Void>> searchAll(String index , T o){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex(index)
                .build();
        SearchResult result = null ;
        List<?> hits = null ;
        try {
            result = client.execute(search);
            log.info("本次查询共查到:"+result+"个关键字!");
            hits = result.getHits(o.getClass());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (List<SearchResult.Hit<T, Void>>) hits ;
    }

    /**
     * 搜索
     * @param keyWord :搜索关键字
     * @return
     */
    public static <T> List<SearchResult.Hit<T,Void>> createSearch(String keyWord , String type , T o , String... fields){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.queryStringQuery(keyWord));
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        for(String field : fields){
            highlightBuilder.field(field);//高亮field
        }
        highlightBuilder.preTags("<em>").postTags("</em>");//高亮标签
        highlightBuilder.fragmentSize(200);//高亮内容长度
        searchSourceBuilder.highlighter(highlightBuilder);
        Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(type).build();
        SearchResult result = null ;
        List<?> hits = null ;
        try {
            result = client.execute(search);
            hits = result.getHits(o.getClass());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return (List<SearchResult.Hit<T, Void>>) hits ;
    }

    /**
     * 获取Document
     * @param index :文档在哪存放
     * @param type : 文档表示的对象类别
     * @param id :文档唯一标识
     * @return
     */
    public static  <T> JestResult getDocument( String index , String type , String id){
        Get get = new Get.Builder(index, id).type(type).build();
        JestResult result = null ;
        try {
            result = client.execute(get);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 按照id删除
     * @param index
     * @param type
     * @param id
     * @return
     */
    public static JestResult deleteDocument(String index, String type, String id) {
        Delete delete = new Delete.Builder(id).index(index).type(type).build();
        JestResult result = null ;
        try {
            result = client.execute(delete);
            log.info("deleteDocument == " + result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 检索结果删除
     * @param index
     * @param type
     * @param params
     * @return
     */
    public static JestResult deleteDocumentByQuery(String index, String type, String params) {
        DeleteByQuery db = new DeleteByQuery.Builder(params)
                .addIndex(index)
                .addType(type)
                .build();

        JestResult result = null ;
        try {
            result = client.execute(db);
            log.info("deleteDocument == " + result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 更新Document
     * @param index
     * @param type
     * @param id
     * @throws Exception
     */
    public static void updateDocument(String index,String type,String id,Object o) {

        Gson gson=new Gson();
        Map map=new HashMap<>();
        map.put("doc",o);
        String script = gson.toJson(map).toString();
        Update update = new Update.Builder(script).index(index).type(type).id(id).build();
        JestResult result = null;
        try {
            result = client.execute(update);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result.getJsonString());
    }

    /**
     * 解析 SearchResult
     * @param result
     * @param clazz
     * @param <T>
     * @return
     */
    public static  <T> List<T>  parseSearchResultList( SearchResult result, Class<T> clazz){
        List<T> os= result.getSourceAsObjectList(clazz, false);
        return os;
    }
    public static  <T> List<T>  parseJestResultList( JestResult result, Class<T> clazz){
        List<T> os= result.getSourceAsObjectList(clazz, false);
        return os;
    }
    public static  <T> T parseSearchResult( SearchResult result, Class<T> clazz){
        return result.getSourceAsObject(clazz, false);
    }

    public static  <T> T parseJestResult( JestResult result, Class<T> clazz){
        return result.getSourceAsObject(clazz, false);
    }
}

参考

elasticsearch-jest-example/JestExample.java at master · ameizi/elasticsearch-jest-example · GitHub

springboot data elasticsearch

版本对应

 查询es5样例

maven

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <elasticsearch.version>5.6.4</elasticsearch.version>
</dependency>

application.properties

spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=192.168.10.183:9300
spring.data.elasticsearch.repositories.enabled=true

bean准备

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "sdes_blog", type = "article")
public class Article {
    @Id
    @Field(type = FieldType.Long, store = true)
    private Long id;
    @Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
    private String title;
    @Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
    private String content;

    @Field(type = FieldType.Keyword, store = true )
    private String site;
}
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.elasticsearch.annotations.Document;
//indexName代表所以名称,type代表表名称
@Document(indexName = "wantu_notice_info", type = "doc")
public class Notice {

    //id
    @JsonProperty("auto_id")
    private Long id;

    //标题
    @JsonProperty("title")
    private String title;

    //公告标签
    @JsonProperty("exchange_mc")
    private String exchangeMc;

    //公告发布时间
    @JsonProperty("create_time")
    private String originCreateTime;

    //公告阅读数量
    @JsonProperty("read_count")
    private Integer readCount;

}

Repository准备

import com.example.spring_data_es.bean.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

@Component
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
}
import com.example.spring_data_es.bean.Notice;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

@Component
public interface NoticeRepository extends ElasticsearchRepository<Notice, Long> {

} 

查询样例

import com.example.spring_data_es.bean.Notice;
import com.example.spring_data_es.repo.NoticeRepository;
import org.assertj.core.util.Lists;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {

    @Autowired
    private NoticeRepository nticeRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    public void indexTest(){
        System.out.println("----------------");
        for (int i = 0; i < 20; i++) {
            Notice article = new Notice();
            article.setId((long)(i+1));
            article.setReadCount(123);
            article.setTitle("springboot整合elasticsearch,这个是新版本 2018年");
            nticeRepository.save(article);
        }

    }


    @Test
    public void pageTest(){
        //按标题进行搜索
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "2018");

        //PageRequest的对象构造函数有多个,page是页数,初始值是0,size是查询结果的条数,后两个参数参考Sort对象的构造方法
        Pageable pageable = PageRequest.of( 1 , 3 , Sort.Direction.DESC, "auto_id" );

        //如果实体和数据的名称对应就会自动封装,pageable分页参数
        Iterable<Notice> listIt =  nticeRepository.search(queryBuilder,pageable);

        //Iterable转list
        List<Notice> list= Lists.newArrayList(listIt);
        for (Notice notice : list) {
            System.out.println(notice.getId());
        }
    }

    @Test
    public void navieTest(){
        QueryBuilder queryBuilder= QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("title","2018"));

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .build();
        List<Notice> notices = elasticsearchTemplate.queryForList(searchQuery, Notice.class);
        for (Notice notice : notices) {
            System.out.println(notice);
        }
    }

}

Java High Level Rest Client

maven依赖

<dependency> 
   <groupId>org.elasticsearch</groupId> 
    <artifactId>elasticsearch</artifactId> 
    <version>6.5.3</version> 
</dependency> 
<!-- 高级api里有对文档,索引等的操作api,所以这里引入的是高级的api --> 
<dependency>
    <groupId>org.elasticsearch.client</groupId> 
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.5.3</version> 
</dependency>
 

初始化

	/**
     * RestHighLevelClient 使用 java low level rest client builder 来构建
     *
     * 高级别的客户端中构建了一个低级别的client
     *
     * @return
     */
    public static RestHighLevelClient getRestClient() {
        RestHighLevelClient restClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.1.2", 9200, "http")));

        return restClient;
    }

使用文档

Delete Alias API | Java REST Client [7.17] | Elastic

SpringBoot 2.2.5 整合ElasticSearch,使用RestHighLevelClient调用各种Api - 简书

TransportClient

优缺点

优点:
使用Transport 接口进行通信,能够使用ES集群中的一些特性,性能最好。
缺点:
JAR包版本需与ES集群版本一致,ES集群升级,客户端也跟着升级到相同版本。
ES 7.0 之后要逐步去掉

maven

<!-- ES -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.6.4</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.6.4</version>
</dependency>

初始化

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
 
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public enum EsClientEnum {
    INSTANCE;
    private TransportClient client;
    private EsClientEnum(){
        Prop prop = PropKit.use(new File("./conf/config.properties"),"gbk");
        String es_cluster_name = prop.get("es_cluster_name");
        String es_ip = prop.get("es_ip");
        Integer es_port = prop.getInt("es_port");
        System.out.println("es_cluster_name = " + es_cluster_name);
 
         //设置集群名称
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", es_cluster_name)
                      .put("client.transport.sniff", false) //启动嗅探功能,自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
//                      .put("client.transport.ignore_cluster_name", true)//忽略集群名字验证, 打开后集群名字不对也能连接上
//                     .put("client.transport.nodes_sampler_interval", 5)//报错
//                     .put("client.transport.ping_timeout", 5) //报错, ping等待时间
                    .build();// 集群名
            //创建client
            client=  new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(es_ip), es_port));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
 
    public TransportClient getClient()  {
        return client;
    }
}

使用

TransportClient使用总结_csdncjh的博客-CSDN博客

来源

Springboot集成elasticsearch的jest - 知乎

Elasticsearch的几种Java客户端_binlife的博客-CSDN博客_elasticsearch java客户端

Spring Data Elasticsearch - Reference Documentation

Elasticsearch RestHighLevelClient 已标记为被弃用 它的替代方案 Elasticsearch Java API Client 的基础教程及迁移方案_无枫丶的博客-CSDN博客

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐