Elasticsearch-dsl-py: Way to search exact string?

Created on 2 Jun 2017  路  6Comments  路  Source: elastic/elasticsearch-dsl-py

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?

Most helpful comment

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

All 6 comments

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 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).

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vmogilev picture vmogilev  路  4Comments

abuzakaria picture abuzakaria  路  4Comments

berinhard picture berinhard  路  3Comments

gabrielpjordao picture gabrielpjordao  路  3Comments

leoliuxd picture leoliuxd  路  4Comments