如何将post数据弹性搜索到java?

How to post data to elastic search over java?

如何从java应用程序post数据到elasticsearch? 对弹性搜索引擎进行查询所必需的 Maven 依赖项是什么?

我做了很多研究,但还是很困惑。

提前致谢!!!!!

您需要执行以下操作:

  • 克隆elasticsearch的maven依赖(来自here
  • 使用传输客户端或节点客户端连接到 Elasticsearch(here's the documentation that explains both the types and here 是示例)
  • 使用IndexRequest索引文档,例如:

    IndexRequest request = new IndexRequest("<index_name>","<document_type>", "<document_id>"); request.source("<document_json>"); IndexResponse response = client.index(indexRequest).actionGet();

最新版本的Elasticsearch就够了(在maven仓库https://mvnrepository.com/artifact/org.elasticsearch中寻找最新版本):

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.4.0</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>rest</artifactId>
    <version>5.4.0</version>
</dependency>

此外,如果您熟悉 scala(在 java 中这些操作几乎相同),您可以使用此 XContentBuilder 创建 json 个对象的示例:https://github.com/sslavian812/needls2/blob/master/src/main/scala/ru/yandex/spark/ElasticSearchHelper.scala#L42

和这个 elasticsearch 客户端 请求示例:https://github.com/sslavian812/needls2/blob/master/src/main/scala/ru/yandex/spark/ElasticSearchHelper.scala#L125