3
votes

I want to change default similarity algorithm of elastic search

I viewed this link: https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-similarity.html#default-base

but I don't know how set this config by rest api or config file or etc...

I also viewed this link: https://www.elastic.co/guide/en/elasticsearch/guide/master/changing-similarities.html

but in this link changing the similarity type when building property.

3

3 Answers

5
votes

One can change the default similarity of an index when you create the index as follows:

curl -XPUT "http://<server>/<index>" -d '
{
  "settings": {
    "similarity": {
      "default": { 
        "type": "BM25"
      }
    }
  }
}

If an index already exists it requires a rebuild since at the moment it is not possible to dynamically update the similarity of an index as mentioned in this :open issue

You can change the default similarity for all indexes by specifying the value for index.similarity.default.type in the config file :

Example:

index.similarity.default.type: BM25
3
votes

Following up on keety's answer, you can now (ES6) change similarity of an existing indexing by first closing it, changing the similarity and opening it again. from the docs:

curl -X POST "localhost:9200/index/_close"
curl -X PUT "localhost:9200/index/_settings" -H 'Content-Type: application/json' -d'
{
  "index": {
    "similarity": {
      "default": {
        "type": "classic"
      }
    }
  }
}
'
curl -X POST "localhost:9200/index/_open"
2
votes

Keety's answer is correct but you can also try using index templates.

Index templates allow to define templates that will automatically be applied to new indices created. The templates include both settings and mappings, and a simple pattern template that controls if the template will be applied to the index created.

In your case :

curl -XPUT localhost:9200/_template/template_1 -d '
{
  "template" : "te*",
  "settings": {
    "similarity": {
      "default": { 
        "type": "BM25"
      }
    }
  }
}'

Defines a template named template_1, with a template pattern of te*. The settings and mappings will be applied to any index name that matches the te* template.

For more information about Index Templates, you can read the official documentation.