elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Java elasticsearch将对象插入索引

Java elasticsearch将对象插入索引,java,elasticsearch,Java,elasticsearch,我是elasticsearch新手,正在寻找使用Java API的一些帮助。我有一些域对象 例如 我已经创建了一个连接到节点的传输客户端 Client client = new TransportClient() .addTransportAddress(new InetSocketTransportAddress("192.168.0.198",9300)); 是否有简单的方法将我的对象直接插入elasticsearch? 我见过这个 IndexResponse respon

我是elasticsearch新手,正在寻找使用Java API的一些帮助。我有一些域对象 例如

我已经创建了一个连接到节点的传输客户端

Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("192.168.0.198",9300));
是否有简单的方法将我的对象直接插入elasticsearch?

我见过这个

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
                    .setSource(jsonBuilder()
                                .startObject()
                                    .field("user", "kimchy")
                                    .field("postDate", new Date())
                                    .field("message", "trying out Elastic     Search")
                                .endObject()
                              )
                    .execute()
                    .actionGet();
但要做到这一点,我必须将每个对象都转换为json,虽然可能,但这并不是我的理想情况

如果我对它的工作原理(架构)有误解,请让我知道,我是来学习的

干杯,
罗伯

我认为你的思路是对的。Java API在您不习惯的时候可能很难获得。我认为随着时间的推移,情况会好转

您必须将对象转换为Json才能将其发送到ElasticSearch集群。是许多流行的图书馆之一,可以为你做这件事

上面显示的代码将创建一个索引。现在,要将文档添加到该索引,请运行以下操作

   Tweet tweet = new Tweet();
   tweet.setId("1234");
   tweet.setMessage("message");

   IndexRequest indexRequest = new IndexRequest("twitter","tweet", tweet.getId());
   indexRequest.source(new Gson().toJson(tweet));
   IndexResponse response = client.index(indexRequest).actionGet();
检查一次为多个项目编制索引。一旦对象变得更复杂,就需要创建

我在中找到了很好的示例,但通常在中找到了更详细的示例


我也得推荐前端。它显示了现有的索引和项。

Jackson是另一个Json库,在性能方面似乎优于Gson。有没有办法在不将对象转换为json的情况下添加它?
   Tweet tweet = new Tweet();
   tweet.setId("1234");
   tweet.setMessage("message");

   IndexRequest indexRequest = new IndexRequest("twitter","tweet", tweet.getId());
   indexRequest.source(new Gson().toJson(tweet));
   IndexResponse response = client.index(indexRequest).actionGet();