terms-query.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. {
  8. "constant_score" : {
  9. "filter" : {
  10. "terms" : { "user" : ["kimchy", "elasticsearch"]}
  11. }
  12. }
  13. }
  14. --------------------------------------------------
  15. The `terms` query is also aliased with `in` as the filter name for
  16. simpler usage.
  17. [float]
  18. [[query-dsl-terms-lookup]]
  19. ===== Terms lookup mechanism
  20. When it's needed to specify a `terms` filter with a lot of terms it can
  21. be beneficial to fetch those term values from a document in an index. A
  22. concrete example would be to filter tweets tweeted by your followers.
  23. Potentially the amount of user ids specified in the terms filter can be
  24. a lot. In this scenario it makes sense to use the terms filter's terms
  25. lookup mechanism.
  26. The terms lookup mechanism supports the following options:
  27. [horizontal]
  28. `index`::
  29. The index to fetch the term values from. Defaults to the
  30. current index.
  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. [float]
  51. ===== Terms lookup twitter example
  52. [source,js]
  53. --------------------------------------------------
  54. # index the information for user with id 2, specifically, its followers
  55. curl -XPUT localhost:9200/users/user/2 -d '{
  56. "followers" : ["1", "3"]
  57. }'
  58. # index a tweet, from user with id 2
  59. curl -XPUT localhost:9200/tweets/tweet/1 -d '{
  60. "user" : "2"
  61. }'
  62. # search on all the tweets that match the followers of user 2
  63. curl -XGET localhost:9200/tweets/_search -d '{
  64. "query" : {
  65. "terms" : {
  66. "user" : {
  67. "index" : "users",
  68. "type" : "user",
  69. "id" : "2",
  70. "path" : "followers"
  71. }
  72. }
  73. }
  74. }'
  75. --------------------------------------------------
  76. The structure of the external terms document can also include array of
  77. inner objects, for example:
  78. [source,js]
  79. --------------------------------------------------
  80. curl -XPUT localhost:9200/users/user/2 -d '{
  81. "followers" : [
  82. {
  83. "id" : "1"
  84. },
  85. {
  86. "id" : "2"
  87. }
  88. ]
  89. }'
  90. --------------------------------------------------
  91. In which case, the lookup path will be `followers.id`.