terms-set-query.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [[query-dsl-terms-set-query]]
  2. === Terms Set Query
  3. experimental[The terms_set query is a new query and its syntax may change in the future]
  4. Returns any documents that match with at least one or more of the
  5. provided terms. The terms are not analyzed and thus must match exactly.
  6. The number of terms that must match varies per document and is either
  7. controlled by a minimum should match field or computed per document in
  8. a minimum should match script.
  9. The field that controls the number of required terms that must match must
  10. be a number field:
  11. [source,js]
  12. --------------------------------------------------
  13. PUT /my-index
  14. {
  15. "mappings": {
  16. "_doc": {
  17. "properties": {
  18. "required_matches": {
  19. "type": "long"
  20. }
  21. }
  22. }
  23. }
  24. }
  25. PUT /my-index/_doc/1?refresh
  26. {
  27. "codes": ["ghi", "jkl"],
  28. "required_matches": 2
  29. }
  30. PUT /my-index/_doc/2?refresh
  31. {
  32. "codes": ["def", "ghi"],
  33. "required_matches": 2
  34. }
  35. --------------------------------------------------
  36. // CONSOLE
  37. // TESTSETUP
  38. An example that uses the minimum should match field:
  39. [source,js]
  40. --------------------------------------------------
  41. GET /my-index/_search
  42. {
  43. "query": {
  44. "terms_set": {
  45. "codes" : {
  46. "terms" : ["abc", "def", "ghi"],
  47. "minimum_should_match_field": "required_matches"
  48. }
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. // CONSOLE
  54. Response:
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "took": 13,
  59. "timed_out": false,
  60. "_shards": {
  61. "total": 1,
  62. "successful": 1,
  63. "skipped" : 0,
  64. "failed": 0
  65. },
  66. "hits": {
  67. "total": 1,
  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