Indexing Json using Java API


(Mohit Anchlia) #1

I am unable to find example of how to index Json String using Java API.
Could someone help me with that?

I am converting xml to json. I see how to do it in REST api but not sure
how this can be done in Java API


(Shairon Toledo) #2

You can index the json as string just setting the source, looks like this

Client client = new TransportClient().addTransportAddress(new
InetSocketTransportAddress("localhost", 9300));
String source = yourJson.toString();

IndexResponse idxResp =
client.prepareIndex().setIndex("documents").setType("document").setId("1")
.setSource(source).setRefresh(true).execute().actionGet();

On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia mohitanchlia@gmail.comwrote:

I am unable to find example of how to index Json String using Java API.
Could someone help me with that?

I am converting xml to json. I see how to do it in REST api but not sure
how this can be done in Java API

--

Shairon Toledo
http://hashcode.me


(Mohit Anchlia) #3

Thanks, that helps. Can that "setType" be anything? I am assuming it's
another level of hierarchy.

On Wed, May 16, 2012 at 11:17 AM, Shairon Toledo
shairon.toledo@gmail.comwrote:

You can index the json as string just setting the source, looks like this

Client client = new TransportClient().addTransportAddress(new
InetSocketTransportAddress("localhost", 9300));
String source = yourJson.toString();

IndexResponse idxResp =
client.prepareIndex().setIndex("documents").setType("document").setId("1")
.setSource(source).setRefresh(true).execute().actionGet();

On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia mohitanchlia@gmail.comwrote:

I am unable to find example of how to index Json String using Java API.
Could someone help me with that?

I am converting xml to json. I see how to do it in REST api but not sure
how this can be done in Java API

--

Shairon Toledo
http://hashcode.me


(Shairon Toledo) #4

Yeap it is, you can understand that as, an index has many types.

On Wed, May 16, 2012 at 3:20 PM, Mohit Anchlia mohitanchlia@gmail.comwrote:

Thanks, that helps. Can that "setType" be anything? I am assuming it's
another level of hierarchy.

On Wed, May 16, 2012 at 11:17 AM, Shairon Toledo <shairon.toledo@gmail.com

wrote:

You can index the json as string just setting the source, looks like
this

Client client = new TransportClient().addTransportAddress(new
InetSocketTransportAddress("localhost", 9300));
String source = yourJson.toString();

IndexResponse idxResp =
client.prepareIndex().setIndex("documents").setType("document").setId("1")
.setSource(source).setRefresh(true).execute().actionGet();

On Tue, May 15, 2012 at 8:20 PM, Mohit Anchlia mohitanchlia@gmail.comwrote:

I am unable to find example of how to index Json String using Java API.
Could someone help me with that?

I am converting xml to json. I see how to do it in REST api but not sure
how this can be done in Java API

--

Shairon Toledo
http://hashcode.me

--

Shairon Toledo
http://hashcode.me


(system) #5