Elasticsearch index mapping in java

Hi guys,

I am trying to create an index with the following code:
XContentBuilder source = XContentFactory.jsonBuilder().startObject()//
.startObject("settings")
.field("number_of_shards", 1)
.endObject()// end settings
.startObject("mappings")
.startObject(INDEX_TYPE)//
.startObject("properties")//
.startObject("user")
.field("type", "string") // start user
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end user
.startObject("postDate")//
.field("type", "date")
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end post date
.startObject("message") //
.field("type", "string")
.field("store", "yes")
.field("index", "not_analyzed")
.endObject() // end user field
.endObject() // end properties
.endObject() // end index type
.endObject() // end mappings
.endObject(); // end the container object

    IndexResponse response = this.client.prepareIndex(INDEX, INDEX_TYPE

).setSource(source)
.setType(INDEX_TYPE).execute()
.actionGet();

I want to have the "message" field not analyzed, because later I want to
use facets to obtain unique messages.
Unfortunately my code seems to add just a document in index with the
following structure:
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"tweet": {
"properties": {
"user": {
"type": "string",
"store": "yes",
"index": "analyzed"
},
"postDate": {
"type": "date",
"store": "yes",
"index": "analyzed"
},
"message": {
"type": "string",
"store": "yes",
"index": "not_analyzed"
}
}
}
}
}

Please help me to spot the error, it seems that mapping are not created.
Thank you very much,
Doru

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/d6635c65-41e5-43e9-b477-908f320127c5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

1 Like

The index request is used to index document, you should use put mapping
request.

e,g,
PutMappingResponse response =
client.admin().indices().preparePutMapping(INDEX).setType(INDEX_TYPE).setSource(source).get();

On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:

Hi guys,

I am trying to create an index with the following code:
XContentBuilder source = XContentFactory.jsonBuilder().startObject()//
.startObject("settings")
.field("number_of_shards", 1)
.endObject()// end settings
.startObject("mappings")
.startObject(INDEX_TYPE)//
.startObject("properties")//
.startObject("user")
.field("type", "string") // start user
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end user
.startObject("postDate")//
.field("type", "date")
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end post date
.startObject("message") //
.field("type", "string")
.field("store", "yes")
.field("index", "not_analyzed")
.endObject() // end user field
.endObject() // end properties
.endObject() // end index type
.endObject() // end mappings
.endObject(); // end the container object

    IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE

).setSource(source)
.setType(INDEX_TYPE).execute()
.actionGet();

I want to have the "message" field not analyzed, because later I want to
use facets to obtain unique messages.
Unfortunately my code seems to add just a document in index with the
following structure:
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"tweet": {
"properties": {
"user": {
"type": "string",
"store": "yes",
"index": "analyzed"
},
"postDate": {
"type": "date",
"store": "yes",
"index": "analyzed"
},
"message": {
"type": "string",
"store": "yes",
"index": "not_analyzed"
}
}
}
}
}

Please help me to spot the error, it seems that mapping are not created.
Thank you very much,
Doru

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/38ae7a3e-b8ef-4a05-8f7a-5ff20917f85e%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hi, Kevin:

Create Index refhttp://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappingssays mappings can be included in index settings JSON. Are you saying that's
not supported by the Java client? (Fwiw, I am seeing the same - just wanted
to confirm):
the following settings successfully create index with predefined mappings
when using curl put /idx -d @settings.json but not when fed to java client
like so:

    Settings settings = 

ImmutableSettings.settingsBuilder().loadFromClasspath("es_admin_idx_settings.json").build();
return
getClient().admin().indices().prepareCreate(idxName).setSettings(settings).execute();

INFO metadata:114 - [Seth] [idx-elasticsearchserviceintegrationtest]
creating index, cause [api], shards [2]/[0], mappings //EMPTY MAPPINGS

{
"index" : {
"number_of_shards" : 1,
"number_of_replicas": 0
},
"mappings": {
"eligibility_criteria": {
"properties": {
"name": {
"type" : "string",
"fields": {
"raw": {
"type" : "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

On Tuesday, February 4, 2014 5:00:30 PM UTC-8, Kevin Wang wrote:

The index request is used to index document, you should use put mapping
request.

e,g,
PutMappingResponse response =
client.admin().indices().preparePutMapping(INDEX).setType(INDEX_TYPE).setSource(source).get();

On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:

Hi guys,

I am trying to create an index with the following code:
XContentBuilder source = XContentFactory.jsonBuilder().startObject()
//
.startObject("settings")
.field("number_of_shards", 1)
.endObject()// end settings
.startObject("mappings")
.startObject(INDEX_TYPE)//
.startObject("properties")//
.startObject("user")
.field("type", "string") // start user
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end user
.startObject("postDate")//
.field("type", "date")
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end post date
.startObject("message") //
.field("type", "string")
.field("store", "yes")
.field("index", "not_analyzed")
.endObject() // end user field
.endObject() // end properties
.endObject() // end index type
.endObject() // end mappings
.endObject(); // end the container object

    IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE

).setSource(source)
.setType(INDEX_TYPE).execute()
.actionGet();

I want to have the "message" field not analyzed, because later I want to
use facets to obtain unique messages.
Unfortunately my code seems to add just a document in index with the
following structure:
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"tweet": {
"properties": {
"user": {
"type": "string",
"store": "yes",
"index": "analyzed"
},
"postDate": {
"type": "date",
"store": "yes",
"index": "analyzed"
},
"message": {
"type": "string",
"store": "yes",
"index": "not_analyzed"
}
}
}
}
}

Please help me to spot the error, it seems that mapping are not created.
Thank you very much,
Doru

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/c04558a1-5faa-48eb-8861-0145d4d8c5e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prepareIndex() is to index a document. What you want is prepareCreate(). I
have an example here (check the method createIndexFullMapping()):

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yep, that's what i used (see my prior post)

On Thu, Mar 13, 2014 at 1:08 PM, Binh Ly binhly_es@yahoo.com wrote:

prepareIndex() is to index a document. What you want is prepareCreate(). I
have an example here (check the method createIndexFullMapping()):

https://github.com/bly2k/es-java-examples/blob/master/admin/IndexAdminExample.java

--
You received this message because you are subscribed to a topic in the
Google Groups "elasticsearch" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/elasticsearch/NjBqXhwloq4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.comhttps://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com?utm_medium=email&utm_source=footer
.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAJwaA224_AMwCoLRUTWJj6JLzrqJKRFhdNFeGByf4%2Bye3g18sQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

:slight_smile: I was confused. I meant to reply to OP. Thanks!

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/96f1a30f-f4ba-4cbf-9d05-680ac25e1086%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fwiw, I fixed my issue below by using prepareCreate().setSource() - rather
than .setSettings() with idx config in the following format:

{"settings" : {
"index": {
"number_of_shards" : 1,
"number_of_replicas": 0,
"analysis" : {
"analyzer": {
"lowercase_keyword": {
"tokenizer": "keyword",
"filter" : ["lowercase"]
}
}
}
}
}, "mappings": {
"type1": {
"properties": {
"name": {
"type" : "string",
"index": "not_analyzed"
}
}
}
}}

On Thursday, March 13, 2014 9:58:56 AM UTC-7, Nikita Tovstoles wrote:

Hi, Kevin:

Create Index refhttp://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappingssays mappings can be included in index settings JSON. Are you saying that's
not supported by the Java client? (Fwiw, I am seeing the same - just wanted
to confirm):
the following settings successfully create index with predefined mappings
when using curl put /idx -d @settings.json but not when fed to java client
like so:

    Settings settings = 

ImmutableSettings.settingsBuilder().loadFromClasspath("es_admin_idx_settings.json").build();
return
getClient().admin().indices().prepareCreate(idxName).setSettings(settings).execute();

INFO metadata:114 - [Seth] [idx-elasticsearchserviceintegrationtest]
creating index, cause [api], shards [2]/[0], mappings //EMPTY MAPPINGS

{
"index" : {
"number_of_shards" : 1,
"number_of_replicas": 0
},
"mappings": {
"eligibility_criteria": {
"properties": {
"name": {
"type" : "string",
"fields": {
"raw": {
"type" : "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

On Tuesday, February 4, 2014 5:00:30 PM UTC-8, Kevin Wang wrote:

The index request is used to index document, you should use put mapping
request.

e,g,
PutMappingResponse response =
client.admin().indices().preparePutMapping(INDEX).setType(INDEX_TYPE).setSource(source).get();

On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:

Hi guys,

I am trying to create an index with the following code:
XContentBuilder source = XContentFactory.jsonBuilder().startObject()
//
.startObject("settings")
.field("number_of_shards", 1)
.endObject()// end settings
.startObject("mappings")
.startObject(INDEX_TYPE)//
.startObject("properties")//
.startObject("user")
.field("type", "string") // start user
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end user
.startObject("postDate")//
.field("type", "date")
.field("store", "yes")
.field("index", "analyzed")//
.endObject()// end post date
.startObject("message") //
.field("type", "string")
.field("store", "yes")
.field("index", "not_analyzed")
.endObject() // end user field
.endObject() // end properties
.endObject() // end index type
.endObject() // end mappings
.endObject(); // end the container object

    IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE

).setSource(source)
.setType(INDEX_TYPE).execute()
.actionGet();

I want to have the "message" field not analyzed, because later I want to
use facets to obtain unique messages.
Unfortunately my code seems to add just a document in index with the
following structure:
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"tweet": {
"properties": {
"user": {
"type": "string",
"store": "yes",
"index": "analyzed"
},
"postDate": {
"type": "date",
"store": "yes",
"index": "analyzed"
},
"message": {
"type": "string",
"store": "yes",
"index": "not_analyzed"
}
}
}
}
}

Please help me to spot the error, it seems that mapping are not created.
Thank you very much,
Doru

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/8d0946a7-be01-4962-bfbb-f43ac303275a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.