terms-set-query.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. [[query-dsl-terms-set-query]]
  2. === Terms Set Query
  3. Returns any documents that match with at least one or more of the
  4. provided terms. The terms are not analyzed and thus must match exactly.
  5. The number of terms that must match varies per document and is either
  6. controlled by a minimum should match field or computed per document in
  7. a minimum should match script.
  8. The field that controls the number of required terms that must match must
  9. be a number field:
  10. [source,js]
  11. --------------------------------------------------
  12. PUT /my-index
  13. {
  14. "mappings": {
  15. "properties": {
  16. "required_matches": {
  17. "type": "long"
  18. }
  19. }
  20. }
  21. }
  22. PUT /my-index/_doc/1?refresh
  23. {
  24. "codes": ["ghi", "jkl"],
  25. "required_matches": 2
  26. }
  27. PUT /my-index/_doc/2?refresh
  28. {
  29. "codes": ["def", "ghi"],
  30. "required_matches": 2
  31. }
  32. --------------------------------------------------
  33. // CONSOLE
  34. // TESTSETUP
  35. An example that uses the minimum should match field:
  36. [source,js]
  37. --------------------------------------------------
  38. GET /my-index/_search
  39. {
  40. "query": {
  41. "terms_set": {
  42. "codes" : {
  43. "terms" : ["abc", "def", "ghi"],
  44. "minimum_should_match_field": "required_matches"
  45. }
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. // CONSOLE
  51. Response:
  52. [source,js]
  53. --------------------------------------------------
  54. {
  55. "took": 13,
  56. "timed_out": false,
  57. "_shards": {
  58. "total": 1,
  59. "successful": 1,
  60. "skipped" : 0,
  61. "failed": 0
  62. },
  63. "hits": {
  64. "total" : {
  65. "value": 1,
  66. "relation": "eq"
  67. },
  68. "max_score": 0.87546873,
  69. "hits": [
  70. {
  71. "_index": "my-index",
  72. "_type": "_doc",
  73. "_id": "2",
  74. "_score": 0.87546873,
  75. "_source": {
  76. "codes": ["def", "ghi"],
  77. "required_matches": 2
  78. }
  79. }
  80. ]
  81. }
  82. }
  83. --------------------------------------------------
  84. // TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
  85. Scripts can also be used to control how many terms are required to match
  86. in a more dynamic way. For example a create date or a popularity field
  87. can be used as basis for the number of required terms to match.
  88. Also the `params.num_terms` parameter is available in the script to indicate the
  89. number of terms that have been specified.
  90. An example that always limits the number of required terms to match to never
  91. become larger than the number of terms specified:
  92. [source,js]
  93. --------------------------------------------------
  94. GET /my-index/_search
  95. {
  96. "query": {
  97. "terms_set": {
  98. "codes" : {
  99. "terms" : ["abc", "def", "ghi"],
  100. "minimum_should_match_script": {
  101. "source": "Math.min(params.num_terms, doc['required_matches'].value)"
  102. }
  103. }
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // CONSOLE