Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

elasticsearch - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...