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)
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 |
|
|
|
|
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, |
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)
]
}
}
}
}
}
Find all documents that have the term search
.
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
.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。