elasticsearch将对象插入索引 [英] elasticsearch insert objects into index

查看:77
本文介绍了elasticsearch将对象插入索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  @XmlRootElement 

公共类BasicActivity {

private String activityName;
个人FullActivity活动;
//获取器和设置器
}

我创建了一个已连接的传输客户端

 客户端客户端= new TransportClient()
.addTransportAddress(new InetSocketTransportAddress( 192.168.0.198, 9300));

是否存在将对象直接插入Elasticsearch的简便方法?



我已经看到了

  IndexResponse响应= client.prepareIndex( twitter, tweet, 1)
.setSource(jsonBuilder()
.startObject()
.field( user, kimchy)
.field ( postDate,新的Date())
.field(消息,尝试弹性搜索)
.endObject()

.execute()
.actionGet();

但是要做到这一点,我必须将每个对象都转换成json,这虽然不是我的理想选择



如果我对它的工作方式(体系结构)有误解,请告诉我,我在这里学习!



欢呼,
抢劫

解决方案

我认为您的做法正确。当您不习惯Java API时,有时很难获得它。我认为随着时间的推移它会变得更好。



您必须将对象转换为Json才能将它们发送到ElasticSearch集群。 Gson 是许多可以为您做到这一点的流行图书馆之一。



上面显示的代码将创建一个索引。现在将文档添加到该索引中,运行类似的内容。

  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响应= client.index(indexRequest).actionGet();

查看 BulkRequest 用于一次索引多个项目。一旦对象变得更加复杂,您就需要创建映射



我在指南,但通常是 ES Google Group 中更详细的示例。 p>

我必须推荐 Head 前端也一样它显示了现有的索引和项目。


I am new to elasticsearch and looking for a bit of help using the Java API. I have some domain objects E.g.

@XmlRootElement

public class BasicActivity {

private String activityName;
private FullActivity activity;  
// Getters and setters
}

I have created a transport client connected to a node

Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("192.168.0.198",9300));

Is there and easy way to insert my object straight into elasticsearch?

I have seen this

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();

But to do that I would have to convert every object into json, which while possible is not my ideal situation.

If I have a misunderstanding of how it works (architecturally) then please let me know, I am here to learn!

cheers, Rob

解决方案

I think you are on the right track. The Java API can be hard to get at times when you are not used to it. I think over time it will get better.

You do have to convert your objects to Json to send them to your ElasticSearch cluster. Gson is one of many popular libraries out there that can do that for you.

The code you show above will create an index. Now to add a document to that index, run something like this.

   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();

Check out BulkRequest for indexing several items at once. Once your objects get more complex, you'll need to create Mappings.

I have found great examples in the Guide, but usually more detailed examples in the ES Google Group.

I have to recommend the Head front end, too. It shows you existing indices and items.

这篇关于elasticsearch将对象插入索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆