7

In my Spring Boot application I have a @Setting annotation pointing to a settings JSON file but it seems to be completely ignored.

@Setting(settingPath = "/settings/elasticsearch-settings.json")
@Document(indexName = "hermes", type = "client", shards = 1, replicas = 0, refreshInterval = "-1")
public class Client {

    @Id
    private String externalId;
    private String name;
    private String surname;
    private String taxNumber;
    private String uid;

    //getters and setter intentionally left out
}

My settings file is placed in:

src/main/resources/settings/elasticsearch-settings.json

The content of the file is the following:

{
  "analysis": {
    "analyzer": {
      "my_ngram_analyzer": {
        "tokenizer": "my_ngram_tokenizer"
      }
    },
    "tokenizer": {
      "my_ngram_tokenizer": {
        "type": "nGram",
        "min_gram": "2",
        "max_gram": "3",
        "token_chars": [
          "letter",
          "digit"
        ]
      }
    }
  }
}

When I run this using Elasticsearch REST api it changes the settings without a problem, so I guess the JSON itself is valid. But even when i put an invalid JSON, or delete the file all together, I get nothing, no warning or error from Spring. That is why my guess is that the annotation is completely ignored.

If it might have anything to do with this, I also have an Elasticsearch configuration class that I use to expose the client on port 9200. It is annotated with:

@EnableConfigurationProperties(ElasticsearchProperties.class)

And the:

@EnableAutoConfiguration(exclude= { ElasticsearchAutoConfiguration.class })

annotation on my main class.

1 Answers1

3

Your elasticsearch-settings.json file is missing the index element. Try like this instead:

{
  "index": {
    "analysis": {
      "analyzer": {
        "my_ngram_analyzer": {
          "tokenizer": "my_ngram_tokenizer"
        }
      },
      "tokenizer": {
        "my_ngram_tokenizer": {
          "type": "nGram",
          "min_gram": "2",
          "max_gram": "3",
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks for your answer Val. Unfortunately nothing changed. It still looks like the @Setting annotation is ignored. – Igor Stojanovski Jul 27 '16 at 19:34
  • Have you deleted your index first, before restarting your application? Spring Data ES will not modify the settings of an existing index. – Val Jul 28 '16 at 05:13
  • I have the same problem. Stopping the server, deleting the index, then starting the server has no effect. – Steve Nov 29 '16 at 22:51
  • Fixed. My `settingPath` and `mappingPath` both referred to a directory with a typo in its name. Spring Boot's default logging level swallowed the error message. – Steve Nov 30 '16 at 11:28
  • 1
    Good job! glad you made it – Val Nov 30 '16 at 11:31
  • 1
    Hello Steve Taylor, did you need more config to use the json setting, because I just add @Setting annotation and even with an invalid settingPath, there was no error from Spring boot! And of course, I think Spring boot didn't read this Setting . – nguyenbkcse Nov 12 '17 at 05:44
  • @Val does Spring Boot ignore this setting and its file contents if the index is already created? – asgs Mar 23 '22 at 10:14
  • 1
    @asgs yes, most likely since it's only useful at index creation time. – Val Mar 23 '22 at 10:43