Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a term query and a match one?

I have documents with string fields which are not analyzed (enforced by a mapping or set globally). I am trying to understand what is the practical difference between

{
    "query": {
        "bool": {
            "must": [
                {"match": {"hostname": "hello"}},
            ]
        }
    }
}

and

{
    "query": {
        "term": {
            "hostname": "hello"
        }
    }
}

I saw in the documentation for term queries that there is a difference when the strings are analyzed (which is not my case). Is there a reason to use term vs match?

like image 505
WoJ Avatar asked Oct 19 '16 07:10

WoJ


People also ask

What is match query?

The match query is of type boolean . It means that the text provided is analyzed and the analysis process constructs a boolean query from the provided text. The operator parameter can be set to or or and to control the boolean clauses (defaults to or ).

What is match phrase query in Elasticsearch?

The match_phrase query analyzes the text and creates a phrase query out of the analyzed text. For example: response = client.

What is Term Elasticsearch?

Elasticsearch provides a way to find a document containing a precise match of a specified term in a document field. Using term and terms query API, you can find documents that match accurate values within a specified field. Let us learn how to use the term and terms queries in Elasticsearch.

What is leaf query?

Leaf query clauses are those clauses that search for a specific value in a specific field like term, match, or range queries. These queries are used by themselves. 2.


1 Answers

In a term query, the searched term (i.e. hello) is not analyzed and is matched exactly as is against the terms present in the inverted index.

In a match query, the searched term (i.e. hello) is analyzed first and then matched against the terms present in the inverted index.

In your case, since hostname is not_analyzed in your mapping, your first choice should be to use a term query since it makes no sense to analyze a term at search time for searching the same term that hasn't been analyzed in the first place at indexing time.

like image 53
Val Avatar answered Oct 21 '22 16:10

Val