terms-query.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. [[query-dsl-terms-query]]
  2. === Terms Query
  3. Filters documents that have fields that match any of the provided terms
  4. (*not analyzed*). For example:
  5. [source,js]
  6. --------------------------------------------------
  7. GET /_search
  8. {
  9. "query": {
  10. "terms" : { "user" : ["kimchy", "elasticsearch"]}
  11. }
  12. }
  13. --------------------------------------------------
  14. // CONSOLE
  15. NOTE: Highlighting `terms` queries is best-effort only, so terms of a `terms`
  16. query might not be highlighted depending on the highlighter implementation that
  17. is selected and on the number of terms in the `terms` query.
  18. [float]
  19. [[query-dsl-terms-lookup]]
  20. ===== Terms lookup mechanism
  21. When it's needed to specify a `terms` filter with a lot of terms it can
  22. be beneficial to fetch those term values from a document in an index. A
  23. concrete example would be to filter tweets tweeted by your followers.
  24. Potentially the amount of user ids specified in the terms filter can be
  25. a lot. In this scenario it makes sense to use the terms filter's terms
  26. lookup mechanism.
  27. The terms lookup mechanism supports the following options:
  28. [horizontal]
  29. `index`::
  30. The index to fetch the term values from.
  31. `type`::
  32. The type to fetch the term values from.
  33. `id`::
  34. The id of the document to fetch the term values from.
  35. `path`::
  36. The field specified as path to fetch the actual values for the
  37. `terms` filter.
  38. `routing`::
  39. A custom routing value to be used when retrieving the
  40. external terms doc.
  41. The values for the `terms` filter will be fetched from a field in a
  42. document with the specified id in the specified type and index.
  43. Internally a get request is executed to fetch the values from the
  44. specified path. At the moment for this feature to work the `_source`
  45. needs to be stored.
  46. Also, consider using an index with a single shard and fully replicated
  47. across all nodes if the "reference" terms data is not large. The lookup
  48. terms filter will prefer to execute the get request on a local node if
  49. possible, reducing the need for networking.
  50. [WARNING]
  51. Executing a Terms Query request with a lot of terms can be quite slow,
  52. as each additional term demands extra processing and memory.
  53. To safeguard against this, the maximum number of terms that can be used
  54. in a Terms Query both directly or through lookup has been limited to `65536`.
  55. This default maximum can be changed for a particular index with the index setting
  56. `index.max_terms_count`.
  57. [float]
  58. ===== Terms lookup twitter example
  59. At first we index the information for user with id 2, specifically, its
  60. followers, then index a tweet from user with id 1. Finally we search on
  61. all the tweets that match the followers of user 2.
  62. [source,js]
  63. --------------------------------------------------
  64. PUT /users/_doc/2
  65. {
  66. "followers" : ["1", "3"]
  67. }
  68. PUT /tweets/_doc/1
  69. {
  70. "user" : "1"
  71. }
  72. GET /tweets/_search
  73. {
  74. "query" : {
  75. "terms" : {
  76. "user" : {
  77. "index" : "users",
  78. "type" : "_doc",
  79. "id" : "2",
  80. "path" : "followers"
  81. }
  82. }
  83. }
  84. }
  85. --------------------------------------------------
  86. // CONSOLE
  87. The structure of the external terms document can also include an array of
  88. inner objects, for example:
  89. [source,js]
  90. --------------------------------------------------
  91. PUT /users/_doc/2
  92. {
  93. "followers" : [
  94. {
  95. "id" : "1"
  96. },
  97. {
  98. "id" : "2"
  99. }
  100. ]
  101. }
  102. --------------------------------------------------
  103. // CONSOLE
  104. In which case, the lookup path will be `followers.id`.