elasticsearchelasticsearch-7elasticsearch-analyzers

what types are best for elasticsearch "KEYWORDS"(like hashtags) field?


i want to make Elasticsearch index for something KEYWORDS, like.. hashtag. and make synonym filter for KEYWORDs.

i think two ways indexing keyword, first is make keyword type.

{
    "settings": {
        "keywordField": {
            "type": "keyword"
        }
    }
}

if make a index with League of Legends maybe this.

{
    "keywordField": ["leagueoflegends", "league", "legends", "lol" /* synonym */]
}

or text type:

{
    "settings": {
        "keywordField": {
            "type": "text",
            "analyzer": "lowercase_and_whitespace_and_synonym_analyzer"
        }
    }
}

maybe this.

{
    "keywordField": ["league of legends"](synonym: lol => leagueoflegends)
}

if use _analyzer api for this field, expects "leagueoflegends", "league", "legends"

search query: 'lol', 'league of legends', 'League of Legends' have to match this field.

which practice is best?


Solution

  • Adding a working example with index data, mapping, search query, and search result. In the below example, I have taken two synonyms lol and leagueoflegends

    Index Mapping:

    {
      "settings": {
        "index": {
          "analysis": {
            "filter": {
              "synonym_filter": {
                "type": "synonym",
                "synonyms": [
                  "leagueoflegends, lol"
                ]
              }
            },
            "analyzer": {
              "synonym_analyzer": {
                "filter": [
                  "lowercase",
                  "synonym_filter"
                ],
                "tokenizer": "standard"
              }
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "keywordField": {
            "type": "text"
          }
        }
      }
    }
    

    Index Data:

    {
        "keywordField": ["leagueoflegends", "league", "legends"]
    }
    

    Search Query:

     {
      "query": {
        "match": {
          "keywordField": {
            "query": "lol",
            "analyzer": "synonym_analyzer"
          }
        }
      }
    }
    

    Search Result:

    "hits": [
          {
            "_index": "66872989",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.19363807,
            "_source": {
              "keywordField": [
                "leagueoflegends",
                "league",
                "legends"
              ]
            }
          }
        ]