elasticsearch 类document
时间: 2023-08-24 07:06:44 浏览: 41
Elasticsearch 是一个开源的分布式搜索和分析引擎,它使用倒排索引来实现高效的全文搜索。在 Elasticsearch 中,文档(Document)是最基本的数据单元。每个文档都有一个唯一的标识(ID),并且由多个字段组成。
一个文档可以是任何形式的结构化数据,通常以 JSON 格式表示。每个字段都有一个字段名和相应的值。字段可以是简单的值(如字符串、数字、布尔值等),也可以是复杂的结构(如嵌套对象、数组等)。
在 Elasticsearch 中,文档是存储、索引和搜索的基本单位。你可以将文档插入到索引中,然后通过各种查询来搜索和检索这些文档。每个文档都可以独立地进行索引和更新,而且可以根据需要进行分片和复制,以支持高吞吐量和可扩展性。
总结来说,Elasticsearch 的文档是用于存储和检索数据的基本单位,它具有灵活的数据结构和强大的搜索功能。
相关问题
elasticsearch java类document
在 Elasticsearch 的 Java 客户端中,有一个名为 `org.elasticsearch.client.document.DocumentClient` 的类,用于执行与文档相关的操作。这个类提供了一系列方法,可以用来索引、获取、更新和删除文档。下面是一个简单的示例:
```java
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class DocumentExample {
private RestHighLevelClient client;
public DocumentExample(RestHighLevelClient client) {
this.client = client;
}
public void indexDocument(String index, String id, String json) throws IOException {
IndexRequest request = new IndexRequest(index)
.id(id)
.source(json, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void getDocument(String index, String id) throws IOException {
GetRequest request = new GetRequest(index, id);
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void updateDocument(String index, String id, String json) throws IOException {
UpdateRequest request = new UpdateRequest(index, id)
.doc(json, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response);
}
public void deleteDocument(String index, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response);
}
}
```
以上示例展示了如何使用 `DocumentClient` 类来执行索引、获取、更新和删除文档的操作。你可以根据自己的需求调用这些方法,并根据需要处理相应的响应。请注意,需要先创建一个 `RestHighLevelClient` 实例,并将其传递给 `DocumentExample` 的构造函数中,以便与 Elasticsearch 集群进行交互。
elasticsearch通用工具类
以下是一个基本的 Elasticsearch 工具类,可以用于与 Elasticsearch 交互:
```java
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ElasticsearchUtil {
private RestHighLevelClient client;
public ElasticsearchUtil(String host, int port) {
client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
}
public void close() throws IOException {
client.close();
}
public boolean indexExists(String index) throws IOException {
return client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
}
public boolean createIndex(String index) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
request.mapping("properties", "{\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"age\": {\n" +
" \"type\": \"integer\"\n" +
" }\n" +
"}", XContentType.JSON);
request.alias(new Alias("my_alias"));
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
return response.isAcknowledged();
}
public boolean deleteIndex(String index) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
return response.isAcknowledged();
}
public boolean documentExists(String index, String id) throws IOException {
GetRequest getRequest = new GetRequest(index, id);
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
return client.exists(getRequest, RequestOptions.DEFAULT);
}
public boolean createDocument(String index, String id, Map<String, Object> source) throws IOException {
IndexRequest request = new IndexRequest(index);
if (id != null) {
request.id(id);
}
request.source(source);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
return response.getResult() == DocWriteResponse.Result.CREATED;
}
public boolean updateDocument(String index, String id, Map<String, Object> source) throws IOException {
UpdateRequest request = new UpdateRequest(index, id);
request.doc(source);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
return response.getResult() == DocWriteResponse.Result.UPDATED;
}
public boolean deleteDocument(String index, String id) throws IOException {
DeleteRequest request = new DeleteRequest(index, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
return response.getResult() == DocWriteResponse.Result.DELETED;
}
public Map<String, Object> getDocument(String index, String id) throws IOException {
GetRequest getRequest = new GetRequest(index, id);
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
if (response.isExists()) {
return response.getSource();
} else {
return null;
}
}
public List<Map<String, Object>> searchDocument(String index, String field, String value) throws IOException {
QueryBuilder queryBuilder = QueryBuilders.matchQuery(field, value);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> result = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
result.add(hit.getSourceAsMap());
}
return result;
}
public boolean bulkCreateDocuments(String index, List<Map<String, Object>> sources) throws IOException {
BulkRequest request = new BulkRequest();
for (Map<String, Object> source : sources) {
IndexRequest indexRequest = new IndexRequest(index);
indexRequest.source(source);
request.add(indexRequest);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
return !response.hasFailures();
}
public boolean bulkUpdateDocuments(String index, List<Map<String, Object>> sources) throws IOException {
BulkRequest request = new BulkRequest();
for (Map<String, Object> source : sources) {
String id = (String) source.remove("id");
UpdateRequest updateRequest = new UpdateRequest(index, id);
updateRequest.doc(source);
request.add(updateRequest);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
return !response.hasFailures();
}
public boolean bulkDeleteDocuments(String index, List<String> ids) throws IOException {
BulkRequest request = new BulkRequest();
for (String id : ids) {
DeleteRequest deleteRequest = new DeleteRequest(index, id);
request.add(deleteRequest);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
return !response.hasFailures();
}
}
```
这个工具类使用 Elasticsearch 的 Java High Level REST Client,支持创建、删除索引,增删改查文档,以及批量操作。