一、操作ES 的方式大题有这么三种

1. TransportClinet客户端
2. REST Client
3. SpringDateES

二、第一种 TransportClinet客户端

首先要讲一下,如果是新打的ES 集群的话,ES版本在7.0以上,或者以后有可能升级版本的话,这个方式已经不被推荐了。这个是官方文档上显示的。并且到ES8的版本,直接废除。

Deprecated in 7.0.0.
The TransportClient is deprecated in favour of the Java High Level REST Clientand will be removed in Elasticsearch 8.0. The migration guide describes all the steps needed to migrate.

This section describes the Java API that Elasticsearch provides. All Elasticsearch operations are executed using a Client object. All operations are completely asynchronous in nature (either accepts a listener, or returns a future).

Additionally, operations on a client may be accumulated and executed in Bulk.

Note, all the APIs are exposed through the Java API (actually, the Java API is used internally to execute them).

如果ES 使用的7.0之前的版本的话,并且不会轻易升ES版本,那么就可以使用。

这里我贴一个案例(不自己手敲了,直接链接别人的案例):案例

三、REST Client

这个是ES 官方推荐的一种方式。下边是官方文档、

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.6/java-rest-high.html

REST Client 有一个高版本,和一个低版本。主要是用来适应不同的 ES版本的。

-  Java Low Level REST Client
-  Java High Level REST Client

这里主要介绍高版本的客户端,看官方文档的介绍:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.6/java-rest-high-compatibility.html

Java高级REST客户端需要Java 1.8,并依赖于Elasticsearch核心项目。客户端版本与为其开发客户端的Elasticsearch版本相同。它接受与相同的请求参数,TransportClient 并返回相同的响应对象。 如果需要将应用程序从其迁移到新的REST客户端,请参阅《迁移指南》TransportClient。确保高级客户端能够与在相同主要版本和较大或相等的次要版本上运行的任何Elasticsearch节点进行通信。它不需要与与其通信的Elasticsearch节点处于相同的次要版本,因为它是前向兼容的,这意味着它支持与比其开发的版本更高的Elasticsearch通信。6.0客户端可以与任何6.x Elasticsearch节点进行通信,而6.1客户端可以与6.1、6.2和任何更高版本的6.x版本进行通信,但是与先前的Elasticsearch节点进行通信时可能会出现不兼容问题如果6.1客户端支持6.0节点不知道的某些API的新请求正文字段,则版本介于6.1和6.0之间。 对这个感兴趣的,就看官方文档吧,我上边已经都贴出来了。我最最推荐的还是下边的方式。

四、SpringDateES

这种方式,是我认为最简单方便操作的方式。可能有我个人的情感在里边,我之前持久层经常使用SpringDateJPA,在我看到SpringDateEs 的时候,一下子就被深深的吸引了。从入门的案例来讲,相比较其它的方式,这个非常简单,从入门案例就可以看出来。


另外,对SpringDateES 做入门的学习,我推荐这篇文章:https://www.jianshu.com/p/930c803a4ebd

最后,对SpringDateES 做全面的学习,请看官方文档:官方文档

五、这里在提一下 使用 ES 的一些坑