1
createIndexWithCustomMappings(String indexName, String fieldsMapping){CreateIndexResponse createIndexResponse = client.admin().indices()
                .prepareCreate(index).setSettings(fieldsMapping).execute().get();}

I have a code which creates the index in elastic search in a spring boot application. Currently the client used is transport client which is now depreciated as per elastic search documentation and now is replaced by High Level Rest Client.

For Creating Index using High Level Rest Client. I have seen this code.

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

Here fieldsMapping is a json file which has details regarding analyzer, tokenizer, filter and is passed as String to this method. I am not able to find methods in java rest high level client to incorporate setSettings(fieldsMapping).execute().get() as done above with transport client.

Any Idea on how this setSettings(fieldMappings) can work java high level rest client

1 Answer 1

2

You can use the implementation from the ElasticsearchRestTemplate itself.

Using Elasticsearch 6.x:

This is how you create the index with settings:

@Override
    public boolean createIndex(String indexName, Object settings) {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        if (settings instanceof String) {
            request.settings(String.valueOf(settings), Requests.INDEX_CONTENT_TYPE);
        } else if (settings instanceof Map) {
            request.settings((Map) settings);
        } else if (settings instanceof XContentBuilder) {
            request.settings((XContentBuilder) settings);
        }
        try {
            return client.indices().create(request, RequestOptions.DEFAULT).isAcknowledged();
        } catch (IOException e) {
            throw new ElasticsearchException("Error for creating index: " + request.toString(), e);
        }
    }

This is how you update the mappings for the index:

@Override
    public boolean putMapping(String indexName, String type, Object mapping) {
        Assert.notNull(indexName, "No index defined for putMapping()");
        Assert.notNull(type, "No type defined for putMapping()");
        PutMappingRequest request = new PutMappingRequest(indexName).type(type);
        if (mapping instanceof String) {
            request.source(String.valueOf(mapping), XContentType.JSON);
        } else if (mapping instanceof Map) {
            request.source((Map) mapping);
        } else if (mapping instanceof XContentBuilder) {
            request.source((XContentBuilder) mapping);
        }
        try {
            return client.indices().putMapping(request, RequestOptions.DEFAULT).isAcknowledged();
        } catch (IOException e) {
            throw new ElasticsearchException("Failed to put mapping for " + indexName, e);
        }
    }

Using Elasticsearch 7.x:

  1. You need to create a variable IndexCoordinates.of("indexName")
  2. Get the IndexOperations from the ElasticSearchTemplate for that index
  3. Create your index via the indexOperations variable like this:
IndexOperations indexOperations = elasticsearchTemplate.indexOps(indexCoordinates);
        String indexSettings = "" //Pass json string here
        String mappingJson = "" //Pass json string here
        Document mapping = Document.parse(mappingJson);
        Map<String, Object> settings = JacksonUtil.fromString(indexSettings, new TypeReference<>() {});

        indexOperations.create(settings, mapping);
        indexOperations.refresh(); //(Optional) refreshes the doc count

It really depends on which spring-data-elasticsearch you are using. Feel free to checkout the documentation as well: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#new-features

Hope this helps with your elasticsearch journey! Feel free to ask more questions regarding the java implementation :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. Here in this PutMappingRequest request = new PutMappingRequest(indexName).type(type); there are two imports for PutMappingRequest. They are import org.elasticsearch.action.admin.indices.mapping; and import org.elasticsearch.client.indices.PutMappingRequest; Out of these two, admin one is the transport client one. There is no type method found in PutMappingRequest with import without admin. Not sure whether type is supported in java high level rest client.
I tried this boolean acknowledged = client.indices().putMapping(request, RequestOptions.DEFAULT).isAcknowledged(); Here putMapping works but there is no way to set type after the index name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.