terms-set-query.asciidoc 2.8 KB

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