elasticsearchsynonym

Implementing multiple synonym_path for single index in elastic search


I am trying to achieve multiple synonym_path for a single index in elasticsearch.

"settings": {
"index": {
  "analysis": {
    "analyzer": {
      "synonym": {
        "tokenizer": "whitespace",
        "filter": ["synonym"]
      }
    },
    "filter": {
      "bool": {
          "should": [{
            "synonym": {
              "type": "synonym",
              "synonyms_path": "synonyms.txt",
              "ignore_case": true
            }},
          {
            "synonym": {
              "type": "synonym",
              "synonyms_path": "synonyms2.txt",
              "ignore_case": true
          }}]
      }
    }
  }
}
},
  "mappings": {
    "animals": {
      "properties": {
        "name": {
          "type": "String",
          "analyzer": "synonym"
        }
      }
    }
  }

I tried the snippet above using JSON Sense in Chrome but it generated a TokenFilter [bool] must have a type associated with it error.

Is there other way to implement it?


Solution

  • The filter section in the analysis section is not meant to contain the Query DSL but token filter definitions.

    In your case, you need to re-create your index with the following settings:

    {
      "settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "synonyms": {
                "tokenizer": "whitespace",
                "filter": [
                  "synonym1",
                  "synonym2"
                ]
              }
            },
            "filter": {
                "synonym1": {
                  "type": "synonym",
                  "synonyms_path": "synonyms.txt",
                  "ignore_case": true
                },
                "synonym2": {
                  "type": "synonym",
                  "synonyms_path": "synonyms2.txt",
                  "ignore_case": true
                }
            }
          }
        }
      },
      "mappings": {
        "animals": {
          "properties": {
            "name": {
              "type": "string",
              "analyzer": "synonyms"
            }
          }
        }
      }
    }