Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Setting annotation for Elasticsearch is ignored in Spring Boot

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.

like image 487
Igor Stojanovski Avatar asked Jul 24 '16 09:07

Igor Stojanovski


1 Answers

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"
          ]
        }
      }
    }
  }
}
like image 187
Val Avatar answered Nov 15 '22 04:11

Val