dimanche 29 mars 2015

Elasticsearch Java API addMapping() usage



Problem: How to create a mapping with java from a json file


I don't want to use XContentBuilder as the json comes from a file!


I have the following json file to create my mapping from:


Using Sense with PUT /indexname it does create an index from it.



{
"mappings": {
"de_brochures": {
"properties": {
"text": {
"type": "string",
"store": true,
"index_analyzer": "de_analyzer"
},
"classification": {
"type": "string",
"index": "not_analyzed"
},
"language": {
"type": "string",
"index": "not_analyzed"
}
}
},
"en_brochures": {
"properties": {
"text": {
"type": "string",
"store": true,
"index_analyzer": "en_analyzer",
"term_vector": "yes"
},
"classification": {
"type": "string",
"index": "not_analyzed"
},
"language": {
"type": "string",
"index": "not_analyzed"
}
}
}
},
"settings": {
"analysis": {
"filter": {
"de_stopwords": {
"type": "stop",
"stopwords": "_german_"
},
"de_stemmer": {
"type": "stemmer",
"name": "light_german"
},
"en_stopwords": {
"type": "stop",
"stopwords": "_english_"
},
"en_stemmer": {
"type": "stemmer",
"name": "light_english"
}
},
"analyzer": {
"de_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"de_stopwords",
"de_stemmer"
]
},
"en_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"en_stopwords",
"en_stemmer"
]
}
}
}
}
}


I have tried this in java: (This is already my second attempt, where I splitted the json into two separate files. Just using addMapping did not work for me)



CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
// CREATE SETTINGS
String settings_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.setSettings(settings_json);
// CREATE MAPPING
String mapping_json = new String(Files.readAllBytes(brochures_mapping_path));
createIndexRequestBuilder.addMapping(type, mapping_json);
CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();


It fails with the error:



Caused by: org.elasticsearch.index.mapper.MapperParsingException:
Root type mapping not empty after parsing! Remaining fields: [mappings : {en_brochures={properties={text={index_analyzer=en_analyzer, store=true, term_vector=yes, type=string}, classification={index=not_analyzed, type=string}, language={index=not_analyzed, type=string}}}, de_brochures={properties={text={index_analyzer=de_analyzer, store=true, type=string}, classification={index=not_analyzed, type=string}, language={index=not_analyzed, type=string}}}}]


Any help is welcome :)




Aucun commentaire:

Enregistrer un commentaire