11

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?

WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

25

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.

Val
  • 207,596
  • 13
  • 358
  • 360
  • This is quite helpful and to the point. Is there any place in the documentation of Elasticsearch where this is mentioned? – akki Aug 09 '18 at 06:59
  • @akki Actually, when using a [`match` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html), ES will be smart enough to do what I described above. – Val Aug 09 '18 at 07:03
  • @akki The official docs mentions this info here: [Term-Based Versus Full-Text](https://www.elastic.co/guide/en/elasticsearch/guide/current/term-vs-full-text.html) – Ray Feb 22 '19 at 23:05