Constant score queries

If you need to return documents that contain a certain word regardless of how many times the word appears, you can use a constant_ score query. A constant_score query wraps a filter query and assigns all documents in the results a relevance score equal to the value of the boost parameter. Thus, all returned documents have an equal relevance score, and term frequency/inverse document frequency (TF/IDF) is not considered. Filter queries do not calculate relevance scores. Further, OpenSearch caches frequently used filter queries to improve performance.

Example

Use the following query to return documents that contain the word “Hamlet” in the shakespeare index:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "match": {
  7. "text_entry": "Hamlet"
  8. }
  9. },
  10. "boost": 1.2
  11. }
  12. }
  13. }

copy

All documents in the results are assigned a relevance score of 1.2:

Response

  1. {
  2. "took": 8,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 96,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.2,
  16. "hits": [
  17. {
  18. "_index": "shakespeare",
  19. "_id": "32535",
  20. "_score": 1.2,
  21. "_source": {
  22. "type": "line",
  23. "line_id": 32536,
  24. "play_name": "Hamlet",
  25. "speech_number": 48,
  26. "line_number": "1.1.97",
  27. "speaker": "HORATIO",
  28. "text_entry": "Dared to the combat; in which our valiant Hamlet--"
  29. }
  30. },
  31. {
  32. "_index": "shakespeare",
  33. "_id": "32546",
  34. "_score": 1.2,
  35. "_source": {
  36. "type": "line",
  37. "line_id": 32547,
  38. "play_name": "Hamlet",
  39. "speech_number": 48,
  40. "line_number": "1.1.108",
  41. "speaker": "HORATIO",
  42. "text_entry": "His fell to Hamlet. Now, sir, young Fortinbras,"
  43. }
  44. },
  45. {
  46. "_index": "shakespeare",
  47. "_id": "32625",
  48. "_score": 1.2,
  49. "_source": {
  50. "type": "line",
  51. "line_id": 32626,
  52. "play_name": "Hamlet",
  53. "speech_number": 59,
  54. "line_number": "1.1.184",
  55. "speaker": "HORATIO",
  56. "text_entry": "Unto young Hamlet; for, upon my life,"
  57. }
  58. },
  59. {
  60. "_index": "shakespeare",
  61. "_id": "32633",
  62. "_score": 1.2,
  63. "_source": {
  64. "type": "line",
  65. "line_id": 32634,
  66. "play_name": "Hamlet",
  67. "speech_number": 60,
  68. "line_number": "",
  69. "speaker": "MARCELLUS",
  70. "text_entry": "Enter KING CLAUDIUS, QUEEN GERTRUDE, HAMLET, POLONIUS, LAERTES, VOLTIMAND, CORNELIUS, Lords, and Attendants"
  71. }
  72. },
  73. {
  74. "_index": "shakespeare",
  75. "_id": "32634",
  76. "_score": 1.2,
  77. "_source": {
  78. "type": "line",
  79. "line_id": 32635,
  80. "play_name": "Hamlet",
  81. "speech_number": 1,
  82. "line_number": "1.2.1",
  83. "speaker": "KING CLAUDIUS",
  84. "text_entry": "Though yet of Hamlet our dear brothers death"
  85. }
  86. },
  87. {
  88. "_index": "shakespeare",
  89. "_id": "32699",
  90. "_score": 1.2,
  91. "_source": {
  92. "type": "line",
  93. "line_id": 32700,
  94. "play_name": "Hamlet",
  95. "speech_number": 8,
  96. "line_number": "1.2.65",
  97. "speaker": "KING CLAUDIUS",
  98. "text_entry": "But now, my cousin Hamlet, and my son,--"
  99. }
  100. },
  101. {
  102. "_index": "shakespeare",
  103. "_id": "32703",
  104. "_score": 1.2,
  105. "_source": {
  106. "type": "line",
  107. "line_id": 32704,
  108. "play_name": "Hamlet",
  109. "speech_number": 12,
  110. "line_number": "1.2.69",
  111. "speaker": "QUEEN GERTRUDE",
  112. "text_entry": "Good Hamlet, cast thy nighted colour off,"
  113. }
  114. },
  115. {
  116. "_index": "shakespeare",
  117. "_id": "32723",
  118. "_score": 1.2,
  119. "_source": {
  120. "type": "line",
  121. "line_id": 32724,
  122. "play_name": "Hamlet",
  123. "speech_number": 16,
  124. "line_number": "1.2.89",
  125. "speaker": "KING CLAUDIUS",
  126. "text_entry": "Tis sweet and commendable in your nature, Hamlet,"
  127. }
  128. },
  129. {
  130. "_index": "shakespeare",
  131. "_id": "32754",
  132. "_score": 1.2,
  133. "_source": {
  134. "type": "line",
  135. "line_id": 32755,
  136. "play_name": "Hamlet",
  137. "speech_number": 17,
  138. "line_number": "1.2.120",
  139. "speaker": "QUEEN GERTRUDE",
  140. "text_entry": "Let not thy mother lose her prayers, Hamlet:"
  141. }
  142. },
  143. {
  144. "_index": "shakespeare",
  145. "_id": "32759",
  146. "_score": 1.2,
  147. "_source": {
  148. "type": "line",
  149. "line_id": 32760,
  150. "play_name": "Hamlet",
  151. "speech_number": 19,
  152. "line_number": "1.2.125",
  153. "speaker": "KING CLAUDIUS",
  154. "text_entry": "This gentle and unforced accord of Hamlet"
  155. }
  156. }
  157. ]
  158. }
  159. }

Parameters

The following table lists all top-level parameters supported by constant_score queries.

ParameterDescription
filterThe filter query that a document must match to be returned in the results. Required.
boostA floating-point value that is assigned as the relevance score to all returned documents. Optional. Default is 1.0.