Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...