How to add a mapping to ElasticSearch using JEST

I am trying to create an index in ES with a specific analyzer and mapping using JEST. I am using the following code:

CreateIndex createIndex = new CreateIndex.Builder(indexName) .settings( ImmutableSettings.builder() .loadFromClasspath( "jestconfiguration.json" ).build().getAsMap() ).build(); JestResult result = client.execute(createIndex); 

And this is jestconfiguration.java

 { "settings": { "analysis": { "analyzer": { "second": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "synonym" ] } }, "filter": { "synonym" : { "type" : "synonym", "synonyms" : [ "smart phone => smartphone" ] } } } }, "mappings": { "index_type": { "properties": { "Name": { "type": "string", "analyzer": "second" } } } } } 

As long as the index is sorted correctly with the specified “settings”, the “mappings” section does not work, and I cannot establish a mapping for the “Name” field. Does anyone have an idea? Is there something like putmapping() in JESt that allows you to add mappings? Ideally, I would like to be able to set the field_name dynamically, rather than in a .json file.

Thnks

+5
source share
1 answer

I found your question trying to figure out if I can create an index and mappings at a time. In the end, I created a second query to create the mappings.

  String mappingJson = new String(ByteStreams.toByteArray(mappingFile.getInputStream())); boolean indexExists = client.execute(new IndicesExists.Builder(configuration.getIndex()).build()).isSucceeded(); if (indexExists) { logger.info("Updating elasticsearch type mapping."); client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build()); } else { logger.info("Creating elasticsearch index and type mapping."); client.execute(new CreateIndex.Builder(configuration.getIndex()).build()); client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build()); } 
0
source

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


All Articles