0
votes

So I have an index with the synonym mapping defined in the search analyzer. When I first created the index, the synonyms were picked up on search. After that, I updated the synonyms.txt files on the nodes once to update a synonym mapping and restarted each node after making a change. This caused the synonym change to be reflected on search thoughout the index.

Now, when I change the synonyms file and restart the nodes, the synonym mapping isn't updating as I believe it should. Am I missing something? I thought since the synonym mapping was on a search_analyzer I wouldn't have to reindex each time to reflect the changes.

Here is my index definition:

PUT /synonym_index
{
  "aliases": {},
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "english": {
              "type": "text",
              "analyzer": "english",
              "search_analyzer":"english_and_synonyms"
            }
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "english": {
          "tokenizer":  "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        },
        "english_and_synonyms": {
          "tokenizer":  "standard",
          "filter": [
            "search_synonyms",
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        }
      },
      "filter": {
        "english_stop": {
          "type":       "stop",
          "stopwords":  "_english_" 
        },
        "english_keywords": {
          "type":       "keyword_marker",
          "keywords":   ["example"] 
        },
        "english_stemmer": {
          "type":       "stemmer",
          "language":   "english"
        },
        "english_possessive_stemmer": {
          "type":       "stemmer",
          "language":   "possessive_english"
        },
        "search_synonyms" : {
            "type" : "synonym_graph",
            "synonyms_path" : "analysis/synonyms.txt"
        }
      }
    },
    "index": {
      "number_of_shards": "5",
      "number_of_replicas": "1"
    }
  }
}

I've tried restarting the node with

sudo service elasticsearch restart

and also with

sudo service elasticsearch stop sudo service elasticsearch start

but neither are causing my changes to reflect. Do I need to reindex every time I update the synonyms file even though it's a search analyzer?

2

2 Answers

0
votes

To reflect the change in the synonyms file, you need to close and open the index after making the changes to the file. This can be done by doing a post request:

POST /synonym_index/_close

POST /synonym_index/_open

After the _open call, you should see the changes reflected in your searches

0
votes

Maybe the Reload Search Analyzers API is what you are looking for: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-reload-analyzers.html

You have to declare that your synonyms are updatable:

"search_synonyms" : {
    "type" : "synonym_graph",
    "synonyms_path" : "analysis/synonyms.txt",
    "updatable": true
}

And in your mapping you need to declare your custom search_analyzer:

"mappings": {
    "properties": {
        "one_attribute": {
            "type": "text",
            "search_analyzer": "english_and_synonyms" 
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html

Do I need to reindex every time I update the synonyms file even though it's a search analyzer?

Only, if your synonyms are being used during index time. If they are only used during search time you don't have to reindex every time.