sim
sim

Reputation: 488

How to get the token of words which matches in elasticsearch

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'}}]}}

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

Upvotes: 0

Views: 387

Answers (1)

Bhavya
Bhavya

Reputation: 16172

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.

Upvotes: 1

Related Questions