使用Java创建ElasticSearch索引

首先

在使用JAVA与ElasticSearch协同工作时,您可以使用ElasticSearch的Rest High Level Client库来轻松操作。
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

引入图书馆

image.png

请把这句话用中文翻译成不同的表述方式。

例:
Original: 我爱你。
Possible Paraphrase: 我对你有深深的感情。

// https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client
compile group: 'org.elasticsearch.client', name: 'elasticsearch-rest-high-level-client', version: '7.3.1'

索引创建摘要示例

// Indexのキー
String blogId = "xxxx";

// Indexデータ(MongoDBのDocumentで作成する例)
Document document = new Document();
document.put("title", "first blog");
document.put("body", "xxxxx");

// 新規INDEX
IndexRequest upsertRequest = new IndexRequest("blogs");
upsertRequest.source(document, XContentType.JSON);
upsertRequest.id(blogId);

// 更新INDEX
UpdateRequest updateRequest = new UpdateRequest("blogs", blogId);
updateRequest.upsert(upsertRequest);
updateRequest.doc(document, XContentType.JSON);

// クライアント生成
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

// ElasticSearchに投入
esClient.update(updateRequest, RequestOptions.DEFAULT);

// クライアントを閉じる
esClient.close();

其他

以下是新建、更新的示例。
GET:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-get.html
Exists:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-exists.html
Delete:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete.html
Search:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_search_apis.html

上述内容

广告
将在 10 秒后关闭
bannerAds