Q: Elasticsearch custom file based analyzer I am using elasticsearch 6 and have a full text field in the index. This field stores the category of a product and can take only one of few possible values(e.g fruit, leafy vegetables etc). I want to analyse the field in a custom way with tokens specified in a file.
E.g
fresh fruit->[fruit, fresh fruit]
Is there a way to use a custom analyser with final tokens coming from a mapping file as above.

A: What you are looking for is synonym token filter. You need to create a custom analyser which used this filter so that when input string is fresh fruit or fruit then it generates single token fruit. You can achieve this by defining analysis in index settings. Create a custom analyser that uses the filter and then apply that analyser to the category field as below:
PUT my_index
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_synonym_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "custom_synonym"
            ]
          }
        },
        "filter": {
          "custom_synonym": {
            "type": "synonym",
            "synonyms": [
              "fresh fruit, fruit => fruit"
            ],
            //"synonyms_path": "analysis/synonyms.txt"    <---- replace "synonyms" above with this to use file instead of array of synonyms
          }
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "category": {
          "type": "text",
          "analyzer": "my_synonym_analyzer"
        }
      }
    }
  }
}

Now when you search for either fruit or fresh fruit against category field documents with either fruit or fresh fruit will match. This is because when searching against a field elastic search by default uses same analyzer on the search string that was applied on the the field while indexing, which in this case will eventually boils down to fruit in either case (fruit or fresh fruit) and hence the documents will match.
