Hari Rao
Hari Rao

Reputation: 3260

Spring data elasticsearch to create index dynamically based on request parameter, percolator support and create index via Elasticsearch Operations

I read through https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference to begin with

My requirements

  1. I want to use percolator. Is there any support for it in spring data elasticsearch? I don't see any in the above link although I understand that percolating is same as indexing (technically from using spring data elasticsearch's perspective). So I can use the indexing part of spring data elasticsearch but just checking if there are any that are specific to percolator.
  2. I want to create an index dynamically. I do understand I can achieve that using SpEL template expression as mentioned in https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations but my case is slightly different, I will get the index name via the RequestParam as part of the API call. So this means as of my knowledge I cannot use SpEL or try something like https://stackoverflow.com/a/33520421/4068218
  3. I see I can use ElasticsearchOperations or ElasticsearchRepository to create Index. Because of #2 (i.e index name via request parameter) I think ElasticsearchOperations better suits but I see IndexOperations facilitating createMapping, createSettings but not both together. I see putMapping too but I dont see anything that says both mapping and settings. The reason I want both is I want to create something like below to begin with
  "settings" : {
                "index" : {
                  "number_of_shards" : 1,
                  "number_of_replicas" : 0
                }
              },
            "mappings": {
                "properties": {
                  "message": {
                    "type": "text"
                  },
                  "query": {
                    "type": "percolator"
                  }
                }
              }

Bottom line :- How do I create an index (name of the index will be dynamic via request param) with mappings, settings using ElasticsearchOperations?
Any lead/help is much appreciated

Upvotes: 0

Views: 5194

Answers (1)

Hari Rao
Hari Rao

Reputation: 3260

First of all thank you very much @P.J.Meisch. Upvoted both your comments as a token of gratitude.

Below worked for me. Below might help others in future

 Document mapping = Document.create().fromJson("""
                {
                "properties": {
                      "message": {
                        "type": "text"
                      },
                      "query": {
                        "type": "percolator"
                      }
                    }
                }""");
 Map<String, Object> settings = ImmutableMap.of( "number_of_shards" ,2,"number_of_replicas",1);
 elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings,mapping);

Upvotes: 1

Related Questions