New here to Elasticsearch and trying to get a better understanding on the difference between these queries. As far as I can tell, term
matches a single term (needs to be lowercase for the match to work?), and both match phrase
and query string
matches a string of text.
2 Answers
term
query matches a single term as it is : the value is not analyzed.
So, it doesn't have to be lowercased depending on what you have indexed.
If you provided Bennett
at index time and the value is not analyzed, the following query won't return anything :
{
"query": {
"term" : { "user" : "bennett" }
}
}
match_phrase
query will analyze the input if analyzers are defined for the queried field and find documents matching the following criteria:
- all the terms must appear in the field
- they must have the same order as the input value
- there must not be any intervening terms, i.e. be consecutive (potentially excluding stop-words but this can be complicated)
For example, if you index the following documents (using standard
analyzer for the field foo
):
{ "foo":"I just said hello world" }
{ "foo":"Hello world" }
{ "foo":"World Hello" }
{ "foo":"Hello dear world" }
This match_phrase
query will only return the first and second documents :
{
"query": {
"match_phrase": {
"foo": "Hello World"
}
}
}
query_string
query search, by default, on a _all field which contains the text of several text fields at once. On top of that, it's parsed and supports some operators (AND/OR...), wildcards and so on (see related syntax).
As the match_phrase
queries, the input is analyzed according to the analyzer set on the queried field.
Unlike the match_phrase
, the terms obtained after analysis don't have to be in the same order, unless the user has used quotes around the input.
For example, using the same documents as before, this query will return all the documents :
{
"query": {
"query_string": {
"query": "hello World"
}
}
}
But this query will return the same 2 documents as the match_phrase
query :
{
"query": {
"query_string": {
"query": "\"Hello World\""
}
}
}
There is much more to say about the different options for those queries, please take a look at the related documentation :
Hope this is clear enough and it will help.
-
Great, wonderful, and thorough explanation! The only thing I'm unclear on is what exactly an analyze is or does... – blee908 Sep 23 '14 at 20:30
-
1You're welcome :) Analyzers process the text in order to obtain the terms that are finally indexed/searched. Read these [pages of the ElasticSearch Definitive Guide](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_exact_values_vs_full_text.html) as this is a really important concept to understand. – ThomasC Sep 23 '14 at 20:50
-
1Yeah coming from SQL, there's a lot of new ideas here. The difference between queries and filters, exact values vs full text, JSON search object, and just the way elastic search is executing it's search. SO MUCH TO TAKE IN!!! Thanks for the resource! – blee908 Sep 23 '14 at 21:03
-
1@ThomasC If you had { "foo":"Hello beautiful world" } in your index, will match query return this? – Emil Feb 05 '16 at 13:37
-
1@batmaci a `match` query use the analyzer if the field is analyzed, so yes. By default, it will return documents having at least one of the terms (see parameter `operator`) and the order is not important. – ThomasC Feb 08 '16 at 09:05
-
"they must have the same order as the input value" -> can the terms form a subsequence ? – sumanth232 Oct 03 '16 at 23:40
-
A word of warning, if your field is an ngram analyzer, say with 1+ characters, you very well could get phrase matches on completely unrelated phrases. So match_phrase is probably not useful in that situation. – reactive-core Dec 05 '22 at 16:40
-
To be able to return "Hello beautiful world" using the provided `match_phrase` search, one can [change the `slop`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html) to a number smaller than the [position_increment_gap](https://www.elastic.co/guide/en/elasticsearch/reference/current/position-increment-gap.html) – Ricardo Aug 31 '23 at 21:30
I think some one definitely looking for differences between them with respect to PARTIAL SEARCH Here is my analysis with default ‘standard analyzer’ :-
Suppose ,We have data :-
{ "name" : “Hello”}
Now what if we want to do partial search with ell ???
Term Query OR Match query
{"term":{"name": "*ell*" }
Will not work , return noting .
{"term":{"name": "*zz* *ell*" }
Will not work , return noting .
Conclusion - Term or Match is not able to do partial search at all
wildcard Query :-
{"wildcard":{"name": "*ell*" }
Will work give result { "name" : "Hello"}
{"wildcard":{"name": "*zz* *ell*" }
Will not work , return noting .
Conclusion - wildcard is able to do partial search with one token only
Query_string :-
{"query_string": {"default_field": "name","query": "*ell*"}
Will work give result { "name" : “Hello”}
{"query_string": {"default_field": "name","query": "*zz* *ell*" }
Will work give result { "name" : “Hello”} .
Conclusion - query_string is able to search with two token are given
-> here token are ell and zz

- 4,694
- 1
- 30
- 38
-
4`wildcard is able to do partial search with one token only`: more precisely, [`wildcard` queries by default work only on `keyword` fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html#fielddata-disabled-text-fields), which are by definition single-token. That has nothing to do with the field contents being more than one word. In your wildcard query, "ozzy hello" would match. – sox supports the mods Jul 24 '20 at 11:09