Creating mappings via the Java API


(Frank LaRosa) #1

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(David Pilato) #2

You can have a look here :
https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticsearch/river/rss/RssRiverTest.java

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa frank@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(Frank LaRosa) #3

Thanks for the example.

I'm pretty disappointed that the Java API actually makes you create
JSON objects. I was expecting it to be more well-developed.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here :https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el...

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(David Pilato) #4

You mean with @annotations ?
I think that it's so easy to use Jackson to serialize/deserialize javabeans to Json that ES doesn't have to reinvent the wheel.

BTW, some users starts to implement OSEM annotations. It could help you...

David :wink:
@dadoonet

Le 6 janv. 2012 à 20:24, Frank LaRosa frank@studyblue.com a écrit :

Thanks for the example.

I'm pretty disappointed that the Java API actually makes you create
JSON objects. I was expecting it to be more well-developed.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here :https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el...

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(Frank LaRosa) #5

I'm confused as to the scope of the Json object I'm supposed to be
creating.

	CreateIndexRequest createRequest = new CreateIndexRequest("a");
	XContentBuilder mapping = ....
	createRequest.mapping("b", mapping);

I want "a" to be the name of my index and "b" to be the name of the
mapping. Am I doing this right?

What should the root element of the XContentBuilder object be? Should
it be "properties", since I'm presumably specifying the properties of
the mapping? Or should it be the mapping name? Or does the API assume
this will be a collection of properties and I don't need to explicitly
say "properties"?

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here :https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el...

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(Frank LaRosa) #6

Where do I find a list of all the possible properties and their
values? I don't see this in the documentation.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here :https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el...

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(David Pilato) #7

Have a look also here :
https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse
arch/river/rss/AbstractRssRiverTest.java

Here is a line to create an index named "rss"
node.client().admin().indices().create(new
CreateIndexRequest("rss")).actionGet();

Here is the mapping for the page type
It contains 5 properties :
source (type string)
title (type string, french analyzer)
description (type string, french analyzer)
author (type string)
link (type string)

XContentBuilder xbMapping =
jsonBuilder()
.startObject()
.startObject("page")
.startObject("properties")

.startObject("source")

.field("type", "string")
.endObject()

.startObject("title")

.field("type", "string")

.field("analyzer", "french")
.endObject()

.startObject("description")

.field("type", "string")

.field("analyzer", "french")
.endObject()

.startObject("author")

.field("type", "string")
.endObject()
.startObject("link")

.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject();

You put the mapping in the rss index for the page type
node.client().admin().indices()
.preparePutMapping("rss")
.setType("page")
.setSource(xbMapping)
.execute().actionGet();

You can find docs : http://www.elasticsearch.org/guide/reference/mapping/
and core types
(http://www.elasticsearch.org/guide/reference/mapping/core-types.html )
http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.h
tml

There is a nice search engine on top of Elasticsearch site which is very
useful to find your way in the documentation

Not sure of what you are looking after...
HTH,
David.

-----Message d'origine-----
De : elasticsearch@googlegroups.com
[mailto:elasticsearch@googlegroups.com] De la part de Frank LaRosa
Envoyé : vendredi 6 janvier 2012 22:07
À : elasticsearch
Objet : Re: Creating mappings via the Java API

Where do I find a list of all the possible properties and their
values? I don't see this in the documentation.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here
:https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el.
..

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function
in

CreateIndexRequest. There are three variants of this method, one
that

takes a java.util.Map, one that takes a String, and one that takes
an

object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me
what

the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in
the

JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(David Pilato) #8

I send it again in HTML format as it seems that my email client or google
groups modify the look&feel (indent is not correct)

Sorry.

Have a look also here :
<https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elastics
earch/river/rss/AbstractRssRiverTest.java>
https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse
arch/river/rss/AbstractRssRiverTest.java

Here is a line to create an index named "rss"

node.client().admin().indices().create(new
CreateIndexRequest("rss")).actionGet();

Here is the mapping for the page type

It contains 5 properties :

· source (type string)

· title (type string, french analyzer)

· description (type string, french analyzer)

· author (type string)

· link (type string)

XContentBuilder xbMapping =

                                           jsonBuilder()

.startObject()

.startObject("page")

.startObject("properties")

.startObject("source")

.field("type", "string")

.endObject()

.startObject("title")

.field("type", "string")

.field("analyzer", "french")

.endObject()

.startObject("description")

.field("type", "string")

.field("analyzer", "french")

.endObject()

.startObject("author")

.field("type", "string")

.endObject()

.startObject("link")

.field("type", "string")

.endObject()

.endObject()

.endObject()

                                                           .endObject();

You put the mapping in the rss index for the page type

node.client().admin().indices()

            .preparePutMapping("rss")

            .setType("page")

            .setSource(xbMapping)

            .execute().actionGet();

You can find docs : http://www.elasticsearch.org/guide/reference/mapping/
http://www.elasticsearch.org/guide/reference/mapping/ and core types (
http://www.elasticsearch.org/guide/reference/mapping/core-types.html
http://www.elasticsearch.org/guide/reference/mapping/core-types.html )
<http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.
html>
http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.h
tml

There is a nice search engine on top of Elasticsearch site which is very
useful to find your way in the documentation

Not sure of what you are looking after...

HTH,

David.

-----Message d'origine-----

De : elasticsearch@googlegroups.com

[mailto:elasticsearch@googlegroups.com] De la part de Frank LaRosa

Envoyé : vendredi 6 janvier 2012 22:07

À : elasticsearch

Objet : Re: Creating mappings via the Java API

Where do I find a list of all the possible properties and their

values? I don't see this in the documentation.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here

:https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el.

..

David :wink:

@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit

:

Hi,

I'm just getting started with Elasticsearch. My first goal is to

create a particular index. I'm an experienced Solr/Lucene user,

so

I've already designed what I want in terms of field names, types,

analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely

documented with few examples. In particular, the API for adding a

mapping during index creation is (I think) the mapping() function

in

CreateIndexRequest. There are three variants of this method, one

that

takes a java.util.Map, one that takes a String, and one that

takes

an

object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me

what

the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in

the

JSON API, but I can't find any objects in the Java API that seem

to

have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?

Thanks.

Frank


(Shay Banon) #9

Here is the mapping docs:
http://www.elasticsearch.org/guide/reference/mapping/, all the options are
there. The Java API is very close to the REST API, for the mapping case,
you simply provide a JSON for the mapping, similar to the REST API:
http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html
.

How you construct the json structure that defines the mapping is up to you.
You can provide is a nested Map structure, build it with a XContentBuilder
(a builder structure to construct json like structure), or have a file
representing the mapping, read it up, and provide it as a String.

On Fri, Jan 6, 2012 at 11:06 PM, Frank LaRosa frank@studyblue.com wrote:

Where do I find a list of all the possible properties and their
values? I don't see this in the documentation.

On Jan 6, 1:04 pm, David Pilato da...@pilato.fr wrote:

You can have a look here :
https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el...

David :wink:
@dadoonet

Le 6 janv. 2012 à 19:54, Frank LaRosa fr...@studyblue.com a écrit :

Hi,

I'm just getting started with Elasticsearch. My first goal is to
create a particular index. I'm an experienced Solr/Lucene user, so
I've already designed what I want in terms of field names, types,
analyizers, etc.

I'm a little confused by the Java API, which seems to be sparsely
documented with few examples. In particular, the API for adding a
mapping during index creation is (I think) the mapping() function in
CreateIndexRequest. There are three variants of this method, one that
takes a java.util.Map, one that takes a String, and one that takes an
object called XContentBuilder with which I am not familiar.

I have no idea what to do here. The documentation doesn't tell me what
the API is looking for in the map or the string.

I found some documentation showing what the mapping looks like in the
JSON API, but I can't find any objects in the Java API that seem to
have the field names or structure shown in the JSON.

Can anyone point me in the right direction or show me an example?
Thanks.

Frank


(system) #10