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

Hari Rao
  • 2,990
  • 3
  • 21
  • 34
  • 1
    `IndexOperations.createWithMapping()` which creates an index with settings and mappings in one call is available since the current version 4.2.0 – P.J.Meisch May 27 '21 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"). – Hari Rao May 27 '21 at 12:54
  • 2
    `elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings, mapping)` – P.J.Meisch May 27 '21 at 13:09

1 Answers1

0

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("{\n" +
                "\n" +
                "    \"properties\": {\n" +
                "      \"message\": {\n" +
                "        \"type\": \"text\"\n" +
                "      },\n" +
                "      \"query\": {\n" +
                "        \"type\": \"percolator\"\n" +
                "      }\n" +
                "    }\n" +
                "\n" +
                "}");
 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);
Hari Rao
  • 2,990
  • 3
  • 21
  • 34
  • 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 – GalAbra Jul 27 '21 at 14:11