terms-query.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "constant_score" : {
  11. "filter" : {
  12. "terms" : { "user" : ["kimchy", "elasticsearch"]}
  13. }
  14. }
  15. }
  16. }
  17. --------------------------------------------------
  18. // CONSOLE
  19. [float]
  20. [[query-dsl-terms-lookup]]
  21. ===== Terms lookup mechanism
  22. When it's needed to specify a `terms` filter with a lot of terms it can
  23. be beneficial to fetch those term values from a document in an index. A
  24. concrete example would be to filter tweets tweeted by your followers.
  25. Potentially the amount of user ids specified in the terms filter can be
  26. a lot. In this scenario it makes sense to use the terms filter's terms
  27. lookup mechanism.
  28. The terms lookup mechanism supports the following options:
  29. [horizontal]
  30. `index`::
  31. The index to fetch the term values from. Defaults to the
  32. current index.
  33. `type`::
  34. The type to fetch the term values from.
  35. `id`::
  36. The id of the document to fetch the term values from.
  37. `path`::
  38. The field specified as path to fetch the actual values for the
  39. `terms` filter.
  40. `routing`::
  41. A custom routing value to be used when retrieving the
  42. external terms doc.
  43. The values for the `terms` filter will be fetched from a field in a
  44. document with the specified id in the specified type and index.
  45. Internally a get request is executed to fetch the values from the
  46. specified path. At the moment for this feature to work the `_source`
  47. needs to be stored.
  48. Also, consider using an index with a single shard and fully replicated
  49. across all nodes if the "reference" terms data is not large. The lookup
  50. terms filter will prefer to execute the get request on a local node if
  51. possible, reducing the need for networking.
  52. [float]
  53. ===== Terms lookup twitter example
  54. At first we index the information for user with id 2, specifically, its
  55. followers, than index a tweet from user with id 1. Finally we search on
  56. all the tweets that match the followers of user 2.
  57. [source,js]
  58. --------------------------------------------------
  59. PUT /users/user/2
  60. {
  61. "followers" : ["1", "3"]
  62. }
  63. PUT /tweets/tweet/1
  64. {
  65. "user" : "1"
  66. }
  67. GET /tweets/_search
  68. {
  69. "query" : {
  70. "terms" : {
  71. "user" : {
  72. "index" : "users",
  73. "type" : "user",
  74. "id" : "2",
  75. "path" : "followers"
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. The structure of the external terms document can also include array of
  83. inner objects, for example:
  84. [source,js]
  85. --------------------------------------------------
  86. curl -XPUT localhost:9200/users/user/2 -d '{
  87. "followers" : [
  88. {
  89. "id" : "1"
  90. },
  91. {
  92. "id" : "2"
  93. }
  94. ]
  95. }'
  96. --------------------------------------------------
  97. In which case, the lookup path will be `followers.id`.