2015-05-05 02:27:52 -04:00
|
|
|
[[query-dsl-terms-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Terms Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Filters documents that have fields that match any of the provided terms
|
|
|
|
(*not analyzed*). For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
2016-11-30 09:29:09 -05:00
|
|
|
"terms" : { "user" : ["kimchy", "elasticsearch"]}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2018-01-31 09:03:08 -05:00
|
|
|
NOTE: Highlighting `terms` queries is best-effort only, so terms of a `terms`
|
|
|
|
query might not be highlighted depending on the highlighter implementation that
|
|
|
|
is selected and on the number of terms in the `terms` query.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
[[query-dsl-terms-lookup]]
|
|
|
|
===== Terms lookup mechanism
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
When it's needed to specify a `terms` filter with a lot of terms it can
|
|
|
|
be beneficial to fetch those term values from a document in an index. A
|
|
|
|
concrete example would be to filter tweets tweeted by your followers.
|
|
|
|
Potentially the amount of user ids specified in the terms filter can be
|
|
|
|
a lot. In this scenario it makes sense to use the terms filter's terms
|
|
|
|
lookup mechanism.
|
|
|
|
|
|
|
|
The terms lookup mechanism supports the following options:
|
|
|
|
|
|
|
|
[horizontal]
|
2015-06-03 19:59:22 -04:00
|
|
|
`index`::
|
2017-07-17 12:50:30 -04:00
|
|
|
The index to fetch the term values from.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-06-03 19:59:22 -04:00
|
|
|
`id`::
|
2013-08-28 19:24:34 -04:00
|
|
|
The id of the document to fetch the term values from.
|
|
|
|
|
2015-06-03 19:59:22 -04:00
|
|
|
`path`::
|
2013-08-28 19:24:34 -04:00
|
|
|
The field specified as path to fetch the actual values for the
|
|
|
|
`terms` filter.
|
|
|
|
|
2015-06-03 19:59:22 -04:00
|
|
|
`routing`::
|
2013-08-28 19:24:34 -04:00
|
|
|
A custom routing value to be used when retrieving the
|
|
|
|
external terms doc.
|
|
|
|
|
|
|
|
The values for the `terms` filter will be fetched from a field in a
|
|
|
|
document with the specified id in the specified type and index.
|
|
|
|
Internally a get request is executed to fetch the values from the
|
|
|
|
specified path. At the moment for this feature to work the `_source`
|
|
|
|
needs to be stored.
|
|
|
|
|
|
|
|
Also, consider using an index with a single shard and fully replicated
|
|
|
|
across all nodes if the "reference" terms data is not large. The lookup
|
|
|
|
terms filter will prefer to execute the get request on a local node if
|
|
|
|
possible, reducing the need for networking.
|
|
|
|
|
2017-12-28 17:36:29 -05:00
|
|
|
[WARNING]
|
|
|
|
Executing a Terms Query request with a lot of terms can be quite slow,
|
|
|
|
as each additional term demands extra processing and memory.
|
|
|
|
To safeguard against this, the maximum number of terms that can be used
|
|
|
|
in a Terms Query both directly or through lookup has been limited to `65536`.
|
|
|
|
This default maximum can be changed for a particular index with the index setting
|
|
|
|
`index.max_terms_count`.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
[float]
|
2015-06-03 19:59:22 -04:00
|
|
|
===== Terms lookup twitter example
|
2016-05-24 05:58:43 -04:00
|
|
|
At first we index the information for user with id 2, specifically, its
|
2017-02-15 03:37:25 -05:00
|
|
|
followers, then index a tweet from user with id 1. Finally we search on
|
2016-05-24 05:58:43 -04:00
|
|
|
all the tweets that match the followers of user 2.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /users/_doc/2
|
2016-05-24 05:58:43 -04:00
|
|
|
{
|
|
|
|
"followers" : ["1", "3"]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /tweets/_doc/1
|
2016-05-24 05:58:43 -04:00
|
|
|
{
|
|
|
|
"user" : "1"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /tweets/_search
|
|
|
|
{
|
|
|
|
"query" : {
|
|
|
|
"terms" : {
|
|
|
|
"user" : {
|
|
|
|
"index" : "users",
|
|
|
|
"id" : "2",
|
|
|
|
"path" : "followers"
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2016-05-24 05:58:43 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2017-05-30 09:41:18 -04:00
|
|
|
The structure of the external terms document can also include an array of
|
2013-08-28 19:24:34 -04:00
|
|
|
inner objects, for example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /users/_doc/2
|
2017-02-07 16:24:05 -05:00
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"followers" : [
|
|
|
|
{
|
|
|
|
"id" : "1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id" : "2"
|
|
|
|
}
|
|
|
|
]
|
2017-02-07 16:24:05 -05:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2017-02-07 16:24:05 -05:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
In which case, the lookup path will be `followers.id`.
|