Skip to content

Latest commit

 

History

History
87 lines (70 loc) · 3.1 KB

File metadata and controls

87 lines (70 loc) · 3.1 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 filter 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 filter works: it checks the inverted index for all documents which contain a term, 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 filter 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 doc 1 and 2 contain the token in the inverted index, therefore they are both returned as a result.

The nature of an inverted index also means that entire field equality is rather difficult to calculate. How would you determine if a particular document contains only your request term? You would have to find the term in the inverted index, extract the document IDs, then scan every row in the inverted index looking for those IDs to see if 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 bool filter that enforces the appropriate number of terms:

GET /my_index/my_type/_search
{
    "query": {
        "filtered" : {
            "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 only has one tag.

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