Unable to read 'synonym' file while creating index

I am trying to build a synonym dictionary and write it to an index. Index is being created in the following way:

PUT /test_index
{
    "settings": {
        "index" : {
            "analysis" : {
                "filter" : {
                    "synonym_filter" : {
                        "type" : "synonym",
                        "synonyms_path" : "C:\\elasticsearch-7.1.0\\config\\synonym.txt",
                        "ignore_case": true
                    }
                },
                "analyzer" : {
                    "synonym_analyzer" : {
                        "tokenizer" : "standard",
                        "filter" : ["lowercase", "synonym_filter"] 
                    }
                }
            }
        }
    },
    "mappings": {
            "properties": {
              "note_text": { 
                "type": "text",
                "analyzer": "synonym_analyzer"
                         }
                          }
        
               }
}

However this gives me an error saying...

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "IOException while reading synonyms_path_path: C:\\elasticsearch-7.1.0\\config\\synonyms.txt"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "IOException while reading synonyms_path_path: C:\\elasticsearch-7.1.0\\config\\synonyms.txt",
    "caused_by": {
      "type": "no_such_file_exception",
      "reason": "C:\\elasticsearch-7.1.0\\config\\synonyms.txt"
    }
  },
  "status": 400
}

Any help is appreciated!

Try just "synonyms.txt". Note that synonyms.txt must be in the same directory that the configuration file since that path is relative to the config dir.

1 Like

Hey Chris, i tried that but i still get an error

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "failed to build synonyms"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to build synonyms",
    "caused_by": {
      "type": "parse_exception",
      "reason": "Invalid synonym rule at line 3",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "term:  was completely eliminated by analyzer"
      }
    }
  },
  "status": 400
}

This is a different error than before. It tells you that some rule in your synonyms file (line 3) is bad. Usually the offending term appears after "term: " which is why I think this might be a blank line or something in your file. The error points out that some input terms in your synonyms file can never match because they get eliminated by the analysis chain the precedes the synonym filter.

I figured what the problem was. I was writing synonyms in quotations (replicating how i would write it in the painless query)

So instead of writing,

ipod => IPOD,
i-pod => IPOD

I was writing,

"ipod => IPOD",
"i-pod => IPOD"

Hi Chris,

Looks like the problem is not yet solved. Because when I put the file name instead of the entire path. The script runs successfully but the synonyms inside are not really mapped.

However when do it explicitly in the code like below, i get the results correctly.

PUT diabetes_notes
{
  "settings": {
    "index": {
    "analysis": {
      "filter": {
        "synonym_filter" : {
          "type" : "synonym",
          "synonyms" : ["depression => Depression",
"Major depression => Depression",
"Major depressive disorder => Depression",
"mental depression => Depression"]
        },
        "autocomplete_filter" : {
          "type": "edge_ngram",
          "min_gram": 4,
          "max_gram": 10
        }
      },
      "analyzer": {
        "synonym_analyzer" : {
        "tokenizer" : "standard",
        "filter" : ["lowercase","synonym_filter"] 
        },
        "autocomplete_analyzer" : {
          "tokenizer" : "standard",
          "filter" : ["lowercase","autocomplete_filter"] 
        }
      }
    }
  }
}
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.