Skip to content

Latest commit

 

History

History
88 lines (73 loc) · 3.32 KB

File metadata and controls

88 lines (73 loc) · 3.32 KB

Contains, but Does Not Equal

It is important to understand that term and terms are contains operations, not equals. What does that mean?

If you have a term query for { "term" : { "tags" : "search" } }, it will match both of the following documents:

{ "tags" : ["search"] }
{ "tags" : ["search", "open_source"] } (1)
  1. This document is returned, even though it has terms other than search.

Recall how the term query works: it checks the inverted index for all documents that contain a term, and then constructs a bitset. In our simple example, we have the following inverted index:

Token

DocIDs

open_source

2

search

1,2

When a term query is executed for the token search, it goes straight to the corresponding entry in the inverted index and extracts the associated doc IDs. As you can see, both document 1 and document 2 contain the token in the inverted index. Therefore, they are both returned as a result.

Note

The nature of an inverted index also means that entire field equality is rather difficult to calculate. How would you determine whether a particular document contains only your request term? You would have to find the term in the inverted index, extract the document IDs, and then scan every row in the inverted index, looking for those IDs to see whether a doc has any other terms.

As you might imagine, that would be tremendously inefficient and expensive. For that reason, term and terms are must contain operations, not must equal exactly.

Equals Exactly

If you do want that behavior—​entire field equality—​the best way to accomplish it involves indexing a secondary field. In this field, you index the number of values that your field contains. Using our two previous documents, we now include a field that maintains the number of tags:

{ "tags" : ["search"], "tag_count" : 1 }
{ "tags" : ["search", "open_source"], "tag_count" : 2 }

Once you have the count information indexed, you can construct a constant_score that enforces the appropriate number of terms:

GET /my_index/my_type/_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                 "bool" : {
                    "must" : [
                        { "term" : { "tags" : "search" } }, (1)
                        { "term" : { "tag_count" : 1 } } (2)
                    ]
                }
            }
        }
    }
}
  1. Find all documents that have the term search.

  2. But make sure the document has only one tag.

This query will now match only the document that has a single tag that is search, rather than any document that contains search.