Home > other >  How to get the token of words which matches in elasticsearch
How to get the token of words which matches in elasticsearch

Time:06-21

  • I need list of values of words which matches. if my search string is abc which is theere in my name of document my expected out is just list of values [abcdef, abcdefgh, .....]

myd is below

[
{'id':1,  'name': 'christiano ronaldo', 'description': 'portugal'},
{'id':2,  'name': 'lionel messi', 'description': 'argentina'},
{'id':3,  'name': 'Lionel Jr', 'description': 'brazil'}
]

DSL query is below

{
  "query": {
    "match_phrase_prefix": {
      "name": {
        "query": "lio",
        "slop": 3,
      }
    }
  }
}

My output is below

{'took': 2,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 2, 'relation': 'eq'},
  'max_score': 0.4700036,
  'hits': [{'_index': 'players',
    '_type': '_doc',
    '_id': '2',
    '_score': 0.4700036,
    '_source': {'id': 2, 'name': 'lionel messi', 'description': 'argentina'}},
   {'_index': 'players',
    '_type': '_doc',
    '_id': '3',
    '_score': 0.4700036,
    '_source': {'id': 3, 'name': 'Lionel Jr', 'description': 'brazil'}}]}}
  • is there any way to extract the token word which matches

Like my expected out is only is just lionel only as lio matches the name which is lionel and Lionel

CodePudding user response:

As far as I'm aware, there is no direct way to get only the text's matched tokens in the search result.

However, one method is to use highlighting to get the matching terms in the text.

{
    "query": {
        "match_phrase_prefix": {
            "name": {
                "query": "lio",
                "slop": 3
            }
        }
    },
    "highlight": {
        "fields": {
            "name": {}
        }
    }
}

You can parse the search result response and retrieve the highlighted terms later on the application side.

  • Related