Aditya
Aditya

Reputation: 1068

Elastic Search Custom Create Index using Java High Level Rest Client

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

Upvotes: 1

Views: 1529

Answers (1)

Aethernite
Aethernite

Reputation: 273

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 :)

Upvotes: 2

Related Questions