Using Elasticearch API addMapping () and setSettings () APIs

Problem: how to create index from json file using

The json file contains the de_brochures index de_brochures . It also defines a de_analyzer analyzer with custom filters that are used by the corresponding index. Since json works with curl and Sense, I assume that I need to adapt its syntax to work with the java API.

I do not want to use XContentFactory.jsonBuilder () since json comes from a file!

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

Using Sense with PUT / indexname, it creates an index from this.

 { "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" } } } "settings": { "analysis": { "filter": { "de_stopwords": { "type": "stop", "stopwords": "_german_" }, "de_stemmer": { "type": "stemmer", "name": "light_german" } }, "analyzer": { "de_analyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "de_stopwords", "de_stemmer" ] } } } } } 

Since the above does not work with addMapping (), I tried to split it into two separate files (I realized that I had to delete the part "mappings": and "settings": :

 ------ Mapping json ------ { "de_brochures": { "properties": { "text": { "type": "string", "store": true, "index_analyzer": "de_analyzer" }, "classification": { "type": "string", "index": "not_analyzed" }, "language": { "type": "string", "index": "not_analyzed" } } } } ------- Settings json -------- { "analysis": { "filter": { "de_stopwords": { "type": "stop", "stopwords": "_german_" }, "de_stemmer": { "type": "stemmer", "name": "light_german" } }, "analyzer": { "de_analyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "de_stopwords", "de_stemmer" ] } } } } 

This is my java code to download and add / install json.

 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("de_brochures", mapping_json); CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet(); 

There is no longer a complaint about the structure of the mapping file, but now it fails with an error:

 Caused by: org.elasticsearch.index.mapper.MapperParsingException: Analyzer [de_analyzer] not found for field [text] 
+6
source share
2 answers

Solution: I was able to do this using my original json file using createIndexRequestBuilder.setSource(settings_json);

+4
source

I think the problem is with the structure of your mapping file.

Here is an example.

 mapping.json { "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" } } } } String mapping = new String(Files.readAllBytes(Paths.get("mapping.json"))); createIndexRequestBuilder.addMapping('en_brochures', mapping); CreateIndexResponse indexResponse =createIndexRequestBuilder.execute().actionGet(); 

It works in mine, you can try.

+1
source

Source: https://habr.com/ru/post/984389/


All Articles