LoginSignup
5
5

More than 3 years have passed since last update.

JavaからElasticSearchのindex作成

Last updated at Posted at 2019-08-31

はじめに

JAVAからElasticSearchを連携する際には、ElasticSearch Rest High Level Clientライブラリを利用して簡単に操作できます。
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

ライブラリ導入

MVNリポジトリからバージョンを探してGradleの記載をコピーしておきます。
image.png

例:

// 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作成概要例

// 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

以上

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5