search-using-query-rules.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. [[search-using-query-rules]]
  2. == Searching with query rules
  3. ++++
  4. <titleabbrev>Searching with query rules</titleabbrev>
  5. ++++
  6. [[query-rules]]
  7. preview::[]
  8. _Query rules_ allow customization of search results for queries that match specified criteria metadata.
  9. This allows for more control over results, for example ensuring that promoted documents that match defined criteria are returned at the top of the result list.
  10. Metadata is defined in the query rule, and is matched against the query criteria.
  11. Query rules use metadata to match a query.
  12. Metadata is provided as part of the `rule_query` as an object and can be anything that helps differentiate the query, for example:
  13. * A user-entered query string
  14. * Personalized metadata about users (e.g. country, language, etc)
  15. * A particular topic
  16. * A referring site
  17. * etc.
  18. Query rules define a metadata key that will be used to match the metadata provided in the `rule_query` with the criteria specified in the rule.
  19. When a query rule matches the `rule_query` metadata according to its defined criteria, the query rule action is applied to the underlying `organic_query`.
  20. For example, a query rule could be defined to match a user-entered query string of `pugs` and a country `us` and promote adoptable shelter dogs if the rule query met both criteria.
  21. Rules are defined using the <<query-rules-apis, query rules API>> and searched using the <<query-dsl-rule-query,rule query>>.
  22. [discrete]
  23. [[query-rule-definition]]
  24. === Rule definition
  25. When defining a rule, consider the following:
  26. [discrete]
  27. [[query-rule-type]]
  28. ==== Rule type
  29. The type of rule we want to apply. For the moment there is a single rule type:
  30. * `pinned` will re-write the query into a <<query-dsl-pinned-query, pinned query>>, pinning specified results matching the query rule at the top of the returned result set.
  31. [discrete]
  32. [[query-rule-criteria]]
  33. ==== Rule criteria
  34. The criteria for which this rule will match. Criteria is defined as `type`, `metadata`, and `values`.
  35. Allowed criteria types are:
  36. [cols="2*", options="header"]
  37. |===
  38. |Type
  39. |Match Requirements
  40. |`exact`
  41. |Rule metadata matches the specified value exactly.
  42. |`fuzzy`
  43. |Rule metadata matches the specified value within an allowed {wikipedia}/Levenshtein_distance[Levenshtein edit distance].
  44. |`prefix`
  45. |Rule metadata starts with the specified value.
  46. |`suffix`
  47. |Rule metadata ends with the specified value.
  48. |`contains`
  49. |Rule metadata contains the specified value.
  50. |`lt`
  51. |Rule metadata is less than the specified value.
  52. |`lte`
  53. |Rule metadata is less than or equal to the specified value.
  54. |`gt`
  55. |Rule metadata is greater than the specified value.
  56. |`gte`
  57. |Rule metadata is greater than or equal to the specified value.
  58. |`always`
  59. |Always matches for all rule queries.
  60. |===
  61. [discrete]
  62. [[query-rule-actions]]
  63. ==== Rule actions
  64. The actions to take when the rule matches a query:
  65. * `ids` will pin the specified <<mapping-id-field,`_id`>>s.
  66. * `docs` will pin the specified documents in the specified indices.
  67. Use `ids` when searching over a single index, and `docs` when searching over multiple indices.
  68. `ids` and `docs` cannot be combined in the same query.
  69. See <<query-dsl-pinned-query,pinned query>> for details.
  70. [discrete]
  71. [[add-query-rules]]
  72. === Add query rules
  73. You can add query rules using the <<put-query-ruleset>> call. This adds a ruleset containing one or more query rules that will be applied to queries that match their specified criteria.
  74. The following command will create a query ruleset called `my-ruleset` with two pinned document rules:
  75. * The first rule will generate a <<query-dsl-pinned-query>> pinning the <<mapping-id-field,`_id`>>s `id1` and `id2` when the `query_string` metadata value is a fuzzy match to either `puggles` or `pugs` _and_ the user's location is in the US.
  76. * The second rule will generate a <<query-dsl-pinned-query>> pinning the <<mapping-id-field, `_id`>> of `id3` specifically from the `my-index-000001` index and `id4` from the `my-index-000002` index when the `query_string` metadata value contains `beagles`.
  77. ////
  78. [source,console]
  79. ----
  80. PUT /my-index-000001
  81. ----
  82. // TESTSETUP
  83. ////
  84. [source,console]
  85. ----
  86. PUT /_query_rules/my-ruleset
  87. {
  88. "rules": [
  89. {
  90. "rule_id": "rule1",
  91. "type": "pinned",
  92. "criteria": [
  93. {
  94. "type": "fuzzy",
  95. "metadata": "query_string",
  96. "values": [ "puggles", "pugs" ]
  97. },
  98. {
  99. "type": "exact",
  100. "metadata": "user_country",
  101. "values": [ "us" ]
  102. }
  103. ],
  104. "actions": {
  105. "ids": [
  106. "id1",
  107. "id2"
  108. ]
  109. }
  110. },
  111. {
  112. "rule_id": "rule2",
  113. "type": "pinned",
  114. "criteria": [
  115. {
  116. "type": "contains",
  117. "metadata": "query_string",
  118. "values": [ "beagles" ]
  119. }
  120. ],
  121. "actions": {
  122. "docs": [
  123. {
  124. "_index": "my-index-000001",
  125. "_id": "id3"
  126. },
  127. {
  128. "_index": "my-index-000002",
  129. "_id": "id4"
  130. }
  131. ]
  132. }
  133. }
  134. ]
  135. }
  136. ----
  137. The API response returns a results of `created` or `updated` depending on whether this was a new or edited ruleset.
  138. [source,console-result]
  139. ----
  140. {
  141. "result": "created"
  142. }
  143. ----
  144. // TEST[continued]
  145. You can use the <<get-query-ruleset>> call to retrieve the ruleset you just created,
  146. the <<list-query-rulesets>> call to retrieve a summary of all query rulesets,
  147. and the <<delete-query-ruleset>> call to delete a query ruleset.
  148. [discrete]
  149. [[rule-query-search]]
  150. === Perform a rule query
  151. Once you have defined a query ruleset, you can search this ruleset using the <<query-dsl-rule-query>> query.
  152. An example query for the `my-ruleset` defined above is:
  153. [source,console]
  154. ----
  155. GET /my-index-000001/_search
  156. {
  157. "query": {
  158. "rule_query": {
  159. "organic": {
  160. "query_string": {
  161. "query": "puggles"
  162. }
  163. },
  164. "match_criteria": {
  165. "query_string": "puggles",
  166. "user_country": "us"
  167. },
  168. "ruleset_id": "my-ruleset"
  169. }
  170. }
  171. }
  172. ----
  173. // TEST[continued]
  174. This rule query will match against `rule1` in the defined query ruleset, and will convert the organic query into a pinned query with `id1` and `id2` pinned as the top hits.
  175. Any other matches from the organic query will be returned below the pinned results.