Java 如何连接 ElasticSearch
在Java中连接Elasticsearch可以使用Elasticsearch提供的官方Java客户端。Elasticsearch官方提供的Java客户端有多种,其中最常用的是RestHighLevelClient
。下面是使用RestHighLevelClient
连接Elasticsearch的详细步骤。
1. 添加依赖
首先,在你的项目中添加Elasticsearch客户端的依赖。如果你使用的是Maven,你可以在pom.xml
中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version> <!-- 版本根据你使用的Elasticsearch版本选择 -->
</dependency>
</dependencies>
如果你使用的是Gradle,你可以在build.gradle
中添加以下依赖:
dependencies {
implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.10.2'
}
2. 创建客户端
接下来,在你的Java代码中创建一个连接到Elasticsearch的客户端。
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ElasticsearchClient {
private static final String HOST = "localhost";
private static final int PORT = 9200;
private static final String SCHEME = "http";
public static RestHighLevelClient createClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(HOST, PORT, SCHEME)
)
);
return client;
}
public static void closeClient(RestHighLevelClient client) {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 执行基本操作
下面是一些基本操作的示例,包括索引文档、获取文档和删除文档。
索引文档
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
public class IndexDocument {
public static void main(String[] args) {
RestHighLevelClient client = ElasticsearchClient.createClient();
IndexRequest request = new IndexRequest("my_index")
.id("1")
.source("{\"name\":\"John Doe\",\"age\":30,\"city\":\"New York\"}", XContentType.JSON);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());
} catch (IOException e) {
e.printStackTrace();
} finally {
ElasticsearchClient.closeClient(client);
}
}
}
获取文档
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
public class GetDocument {
public static void main(String[] args) {
RestHighLevelClient client = ElasticsearchClient.createClient();
GetRequest getRequest = new GetRequest("my_index", "1");
try {
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
if (getResponse.isExists()) {
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
} else {
System.out.println("Document not found");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
ElasticsearchClient.closeClient(client);
}
}
}
删除文档
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
public class DeleteDocument {
public static void main(String[] args) {
RestHighLevelClient client = ElasticsearchClient.createClient();
DeleteRequest deleteRequest = new DeleteRequest("my_index", "1");
try {
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.getResult());
} catch (IOException e) {
e.printStackTrace();
} finally {
ElasticsearchClient.closeClient(client);
}
}
}
4. 处理响应
上面的示例展示了如何创建客户端并执行基本的CRUD操作。在实际应用中,你可能需要处理更多的响应和异常,例如检查响应状态,处理特定的异常等。确保根据实际需求编写相应的处理逻辑。
通过以上步骤,你可以在Java中连接并操作Elasticsearch,进行文档的索引、查询、更新和删除等操作。