Hi,
In kibana, we can search exact string by wrapping it by double quotation marks.
It seems
s = s.query('match', content="Donald Trump")
is searching for Donald or Trump.
Using dsl-py how can we search exact string?
Hi, kibana uses the query_string query so you can have the exact same functionality by using that:
s = s.query('query_string', query='"Donald Trump"')
I would however recommend more granular approach using either a phrase or terms queries - phrase (0) is useful if the string you are looking for is somewhere in a field, but not exactly the match, it will also work with type text (analyzed).
If you want to do true exact matching you need to use keyword type fields and a term query (1). That will do exact match against a field:
s = s.query('term', full_name="Donald Trump")
Hope this helps
0 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
1 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
but how to search for exact match "Donald Trump" in all fields?
@balaa you cannot really have a universal search like that for exact matches since it depends on the mappings also you cannot effectively search for an exact substring. The best approximation would be to use a phrase query as mentioned.
What if someone wants an exact match on a text field.
We cannot use terms query as it is not recommended in text fields.
@aayushpant96 Unfortunately text fields don't support exact matches, there is no clear way of doing it. Some information is lost during the analysis process.
I would however recommend more granular approach using either a
phraseortermsqueries -phrase(0) is useful if the string you are looking for is somewhere in a field, but not exactly the match, it will also work with typetext(analyzed).
To add, keyword type fields should mix fine with a match query:
https://discuss.elastic.co/t/term-vs-match-query-on-keyword-field/65892
Most helpful comment
Hi, kibana uses the
query_stringquery so you can have the exact same functionality by using that:s = s.query('query_string', query='"Donald Trump"')I would however recommend more granular approach using either a
phraseortermsqueries -phrase(0) is useful if the string you are looking for is somewhere in a field, but not exactly the match, it will also work with typetext(analyzed).If you want to do true exact matching you need to use
keywordtype fields and atermquery (1). That will do exact match against a field:s = s.query('term', full_name="Donald Trump")Hope this helps
0 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
1 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html