当涉及到Elasticsearch的入门实例时,以下是一个详细的示例,展示了如何使用Java高级REST客户端与Elasticsearch进行交互。
- 准备工作:
- 安装Elasticsearch:请按照Elasticsearch官方文档中的说明安装并启动Elasticsearch。
- 添加依赖项:在您的项目的构建文件(例如pom.xml)中,添加Elasticsearch的Java高级REST客户端的依赖项。例如,在Maven项目中,您可以添加以下依赖项:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>
- 创建索引:
- 创建一个名为"my_index"的索引:
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class ElasticsearchDemo {
public static void main(String[] args) {
try (RestHighLevelClient client = ElasticsearchClientFactory.createClient()) {
String indexName = "my_index";
// 检查索引是否已存在
boolean indexExists = client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
if (!indexExists) {
// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 设置索引的设置
request.settings(Settings.builder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1));
// 设置索引的映射
String mapping = "{\"properties\":{\"title\":{\"type\":\"text\"},\"content\":{\"type\":\"text\"}}}";
request.mapping(mapping, XContentType.JSON);
// 执行创建索引请求
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
// 判断索引是否创建成功
if (response.isAcknowledged()) {
System.out.println("Index created: " + indexName);
} else {
System.out.println("Failed to create index: " + indexName);
}
} else {
System.out.println("Index already exists: " + indexName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 添加文档:
- 向索引中添加一个文档:
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class ElasticsearchDemo {
public static void main(String[] args) {
try (RestHighLevelClient client = ElasticsearchClientFactory.createClient()) {
String indexName = "my_index";
String documentId = "1";
// 检查索引是否存在
boolean indexExists = client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
if (indexExists) {
// 创建索引请求
IndexRequest request = new IndexRequest(indexName)
.id(documentId)
.source
("{\"title\":\"Elasticsearch Demo\",\"content\":\"This is a demo content\"}", XContentType.JSON);
// 执行索引请求
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
// 打印响应信息
System.out.println("Index created with ID: " + response.getId());
} else {
System.out.println("Index does not exist: " + indexName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 搜索文档:
- 从索引中搜索文档:
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class ElasticsearchDemo {
public static void main(String[] args) {
try (RestHighLevelClient client = ElasticsearchClientFactory.createClient()) {
String indexName = "my_index";
// 检查索引是否存在
boolean indexExists = client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
if (indexExists) {
// 创建搜索请求
SearchRequest request = new SearchRequest(indexName);
// 构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("content", "demo"));
sourceBuilder.timeout(TimeValue.timeValueSeconds(10));
request.source(sourceBuilder);
// 执行搜索请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 处理搜索结果
for (SearchHit hit : response.getHits().getHits()) {
System.out.println("Document ID: " + hit.getId());
System.out.println("Document Source: " + hit.getSourceAsString());
System.out.println("--------------------------------------------------");
}
} else {
System.out.println("Index does not exist: " + indexName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上示例代码中的操作是一个简化的示例,您可以根据您的实际需求进行相应的操作,如创建索引、添加文档、搜索文档等。
在编译和运行代码之前,请确保已添加了Elasticsearch的Java高级REST客户端的依赖项,并根据您的实际情况修改代码中的Elasticsearch服务器的主机、端口和索引名称。
希望以上示例能帮助您入门使用Elasticsearch。如需进一步了解和使用Elasticsearch,请参考官方文档和示例代码。
标签:indexName,使用,client,索引,实例,ElasticSearch,elasticsearch,org,import From: https://www.cnblogs.com/lukairui/p/17444076.html