0

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

3
  • 1
    IndexOperations.createWithMapping() which creates an index with settings and mappings in one call is available since the current version 4.2.0 Commented May 27, 2021 at 10:52
  • @P.J.Meisch - Thanks for the response and your time. Since I want the index name to be dynamic (i.e I am accepting as a parameter in the API end point) I wont be able to use any class that will need @Document(indexName = "name-of-the-index"). Commented May 27, 2021 at 12:54
  • 2
    elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings, mapping) Commented May 27, 2021 at 13:09

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This helped me a lot, so thank you! As a side note, I think it's better to use Document.from(Map) than using a stringified JSON
This JSON looks even better if using Java's text block syntax.

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.