我们将使用 RestHighLevelClient 来演示如何进行索引创建、文档插入、文档查询和文档删除等操作。
1. 引入依赖
首先,确保你的项目中已经引入了 Elasticsearch 的依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version> <!-- 请根据实际情况选择合适的版本 -->
</dependency>
2. 创建 RestHighLevelClient
import org.elasticsearch.client.RestHighLevelClient;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
public class ElasticsearchClientExample {
private static final String ES_HOST = "localhost";
private static final int ES_PORT = 9200;
private static final String ES_SCHEME = "http";
public static RestHighLevelClient createClient() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost(ES_HOST, ES_PORT, ES_SCHEME)
)
);
}
}
3. 创建索引
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
public class IndexOperations {
public static void createIndex(RestHighLevelClient client, String indexName) throws Exception {
CreateIndexRequest request = new CreateIndexRequest(indexName);
request.source("{\n" +
" \"settings\": {\n" +
" \"number_of_shards\": 1,\n" +
" \"number_of_replicas\": 0\n" +
" },\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"title\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"content\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("Index created: " + response.isAcknowledged());
}
}
4. 插入文档
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
public class DocumentOperations {
public static void indexDocument(RestHighLevelClient client, String indexName, String id, String title, String content) throws Exception {
IndexRequest request = new IndexRequest(indexName).id(id)
.source(XContentType.JSON, "title", title, "content", content);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("Document indexed: " + response.getResult());
}
}
5. 查询文档
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class SearchOperations {
public static void searchDocuments(RestHighLevelClient client, String indexName, String query) throws Exception {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("content", query));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("Total hits: " + searchResponse.getHits().getTotalHits().value);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
}
6. 删除文档
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
public class DeleteOperations {
public static void deleteDocument(RestHighLevelClient client, String indexName, String id) throws Exception {
DeleteRequest request = new DeleteRequest(indexName, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println("Document deleted: " + response.getResult());
}
}
7. 主方法
public class Main {
public static void main(String[] args) {
RestHighLevelClient client = ElasticsearchClientExample.createClient();
try {
// 创建索引
IndexOperations.createIndex(client, "my_index");
// 插入文档
DocumentOperations.indexDocument(client, "my_index", "1", "First Document", "This is the content of the first document.");
DocumentOperations.indexDocument(client, "my_index", "2", "Second Document", "This is the content of the second document.");
// 查询文档
SearchOperations.searchDocuments(client, "my_index", "first");
// 删除文档
DeleteOperations.deleteDocument(client, "my_index", "1");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上代码展示了如何使用 RestHighLevelClient 进行基本的 Elasticsearch 操作,包括创建索引、插入文档、查询文档和删除文档。希望这些示例对你有所帮助!