openapi: 3.2.0
info:
title: Elasticsearch Query Rules API
description: 'Elasticsearch provides REST APIs that are used by the UI components and can be called directly to configure and access Elasticsearch features.
## Documentation source and versions
This documentation is derived from the main branch of the elasticsearch-specification repository. It is provided under license Attribution-NonCommercial-NoDerivatives 4.0 International.
This documentation contains work-in-progress information for future Elastic Stack releases.'
license:
name: Apache 2.0
url: https://github.com/elastic/elasticsearch-specification/blob/main/LICENSE
version: ''
security:
- apiKeyAuth: []
- basicAuth: []
- bearerAuth: []
tags:
- name: query_rules
description: Query rules enable you to configure per-query rules that are applied at query time to queries that match the specific rule.
externalDocs:
description: Learn more.
url: https://www.elastic.co/docs/reference/elasticsearch/rest-apis/searching-with-query-rules
x-displayName: Query rules
paths:
/_query_rules/{ruleset_id}/_rule/{rule_id}:
get:
tags:
- query_rules
summary: Get a query rule
description: Get details about a query rule within a query ruleset.
externalDocs:
description: See rules and rulesets in Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#accessing-the-query-rules-ui
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/get-query-rule.html
operationId: query-rules-get-rule
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset containing the rule to retrieve
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
- in: path
name: rule_id
description: The unique identifier of the query rule within the specified ruleset to retrieve
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/query_rules._types.QueryRule'
examples:
QueryRuleGetResponseExample1:
description: A successful response from `GET _query_rules/my-ruleset/_rule/my-rule1`.
value: "{\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"query_string\",\n \"values\": [\n \"pugs\",\n \"puggles\"\n ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n}"
x-state: Generally available; Added in 8.15.0
x-variations:
- "
\n GET\n /_query_rules/{ruleset_id}/_rule/{rule_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: get_rule.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: 'GET _query_rules/my-ruleset/_rule/my-rule1
'
- lang: Python
source: "resp = client.query_rules.get_rule(\n ruleset_id=\"my-ruleset\",\n rule_id=\"my-rule1\",\n)"
- lang: JavaScript
source: "const response = await client.queryRules.getRule({\n ruleset_id: \"my-ruleset\",\n rule_id: \"my-rule1\",\n});"
- lang: Ruby
source: "response = client.query_rules.get_rule(\n ruleset_id: \"my-ruleset\",\n rule_id: \"my-rule1\"\n)"
- lang: PHP
source: "$resp = $client->queryRules()->getRule([\n \"ruleset_id\" => \"my-ruleset\",\n \"rule_id\" => \"my-rule1\",\n]);"
- lang: curl
source: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_query_rules/my-ruleset/_rule/my-rule1"'
- lang: Java
source: "client.queryRules().getRule(g -> g\n .ruleId(\"my-rule1\")\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
put:
tags:
- query_rules
summary: Create or update a query rule
description: 'Create or update a query rule within a query ruleset.
IMPORTANT: Due to limitations within pinned queries, you can only pin documents using ids or docs, but cannot use both in single rule.
It is advised to use one or the other in query rulesets, to avoid errors.
Additionally, pinned queries have a maximum limit of 100 pinned hits.
If multiple matching rules pin more than 100 documents, only the first 100 documents are pinned in the order they are specified in the ruleset.'
externalDocs:
description: Edit rules and rulesets from the Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#edit-a-rule
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/put-query-rule.html
operationId: query-rules-put-rule
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset containing the rule to be created or updated.
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
- in: path
name: rule_id
description: The unique identifier of the query rule within the specified ruleset to be created or updated.
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
requestBody:
content:
application/json:
schema:
type: object
properties:
type:
description: The type of rule.
allOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleType'
criteria:
description: 'The criteria that must be met for the rule to be applied.
If multiple criteria are specified for a rule, all criteria must be met for the rule to be applied.'
oneOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleCriteria'
- type: array
items:
$ref: '#/components/schemas/query_rules._types.QueryRuleCriteria'
actions:
description: 'The actions to take when the rule is matched.
The format of this action depends on the rule type.'
allOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleActions'
priority:
type: number
required:
- type
- criteria
- actions
examples:
QueryRulePutRequestExample1:
description: 'Run `POST _query_rules/my-ruleset/_test` to test a ruleset. Provide the match criteria that you want to test against.
'
value: "{\n \"match_criteria\": {\n \"query_string\": \"puggles\"\n }\n}"
required: true
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
properties:
result:
allOf:
- $ref: '#/components/schemas/_types.Result'
required:
- result
x-state: Generally available; Added in 8.15.0
x-variations:
- "\n PUT\n /_query_rules/{ruleset_id}/_rule/{rule_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: put_rule.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: "POST _query_rules/my-ruleset/_test\n{\n \"match_criteria\": {\n \"query_string\": \"puggles\"\n }\n}"
- lang: Python
source: "resp = client.query_rules.test(\n ruleset_id=\"my-ruleset\",\n match_criteria={\n \"query_string\": \"puggles\"\n },\n)"
- lang: JavaScript
source: "const response = await client.queryRules.test({\n ruleset_id: \"my-ruleset\",\n match_criteria: {\n query_string: \"puggles\",\n },\n});"
- lang: Ruby
source: "response = client.query_rules.test(\n ruleset_id: \"my-ruleset\",\n body: {\n \"match_criteria\": {\n \"query_string\": \"puggles\"\n }\n }\n)"
- lang: PHP
source: "$resp = $client->queryRules()->test([\n \"ruleset_id\" => \"my-ruleset\",\n \"body\" => [\n \"match_criteria\" => [\n \"query_string\" => \"puggles\",\n ],\n ],\n]);"
- lang: curl
source: 'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d ''{"match_criteria":{"query_string":"puggles"}}'' "$ELASTICSEARCH_URL/_query_rules/my-ruleset/_test"'
- lang: Java
source: "client.queryRules().test(t -> t\n .matchCriteria(\"query_string\", JsonData.fromJson(\"\\\"puggles\\\"\"))\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
delete:
tags:
- query_rules
summary: Delete a query rule
description: 'Delete a query rule within a query ruleset.
This is a destructive action that is only recoverable by re-adding the same rule with the create or update query rule API.'
externalDocs:
description: Delete a rule from the Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#delete-a-rule
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/delete-query-rule.html
operationId: query-rules-delete-rule
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset containing the rule to delete
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
- in: path
name: rule_id
description: The unique identifier of the query rule within the specified ruleset to delete
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/_types.AcknowledgedResponseBase'
x-state: Generally available; Added in 8.15.0
x-variations:
- "\n DELETE\n /_query_rules/{ruleset_id}/_rule/{rule_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: delete_rule.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: 'DELETE _query_rules/my-ruleset/_rule/my-rule1
'
- lang: Python
source: "resp = client.query_rules.delete_rule(\n ruleset_id=\"my-ruleset\",\n rule_id=\"my-rule1\",\n)"
- lang: JavaScript
source: "const response = await client.queryRules.deleteRule({\n ruleset_id: \"my-ruleset\",\n rule_id: \"my-rule1\",\n});"
- lang: Ruby
source: "response = client.query_rules.delete_rule(\n ruleset_id: \"my-ruleset\",\n rule_id: \"my-rule1\"\n)"
- lang: PHP
source: "$resp = $client->queryRules()->deleteRule([\n \"ruleset_id\" => \"my-ruleset\",\n \"rule_id\" => \"my-rule1\",\n]);"
- lang: curl
source: 'curl -X DELETE -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_query_rules/my-ruleset/_rule/my-rule1"'
- lang: Java
source: "client.queryRules().deleteRule(d -> d\n .ruleId(\"my-rule1\")\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
/_query_rules/{ruleset_id}:
get:
tags:
- query_rules
summary: Get a query ruleset
description: Get details about a query ruleset.
externalDocs:
description: See rules and rulesets in Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#accessing-the-query-rules-ui
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/get-query-ruleset.html
operationId: query-rules-get-ruleset
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/query_rules._types.QueryRuleset'
examples:
QueryRulesetGetResponseExample1:
description: A successful response from `GET _query_rules/my-ruleset/`.
value: "{\n \"ruleset_id\": \"my-ruleset\",\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"query_string\",\n \"values\": [ \"pugs\", \"puggles\" ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"query_string\",\n \"values\": [ \"rescue dogs\" ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n}"
x-state: Generally available; Added in 8.10.0
x-variations:
- "\n GET\n /_query_rules/{ruleset_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: get_ruleset.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: 'GET _query_rules/my-ruleset/
'
- lang: Python
source: "resp = client.query_rules.get_ruleset(\n ruleset_id=\"my-ruleset\",\n)"
- lang: JavaScript
source: "const response = await client.queryRules.getRuleset({\n ruleset_id: \"my-ruleset\",\n});"
- lang: Ruby
source: "response = client.query_rules.get_ruleset(\n ruleset_id: \"my-ruleset\"\n)"
- lang: PHP
source: "$resp = $client->queryRules()->getRuleset([\n \"ruleset_id\" => \"my-ruleset\",\n]);"
- lang: curl
source: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_query_rules/my-ruleset/"'
- lang: Java
source: "client.queryRules().getRuleset(g -> g\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
put:
tags:
- query_rules
summary: Create or update a query ruleset
description: 'There is a limit of 100 rules per ruleset.
This limit can be increased by using the `xpack.applications.rules.max_rules_per_ruleset` cluster setting.
IMPORTANT: Due to limitations within pinned queries, you can only select documents using `ids` or `docs`, but cannot use both in single rule.
It is advised to use one or the other in query rulesets, to avoid errors.
Additionally, pinned queries have a maximum limit of 100 pinned hits.
If multiple matching rules pin more than 100 documents, only the first 100 documents are pinned in the order they are specified in the ruleset.'
externalDocs:
description: Edit rules and rulesets from the Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#edit-a-rule
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/put-query-ruleset.html
operationId: query-rules-put-ruleset
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset to be created or updated.
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
requestBody:
content:
application/json:
schema:
type: object
properties:
rules:
oneOf:
- $ref: '#/components/schemas/query_rules._types.QueryRule'
- type: array
items:
$ref: '#/components/schemas/query_rules._types.QueryRule'
required:
- rules
examples:
QueryRulesetPutRequestExample1:
description: 'Run `PUT _query_rules/my-ruleset` to create a new query ruleset. Two rules are associated with `my-ruleset`. `my-rule1` will pin documents with IDs `id1` and `id2` when `user_query` contains `pugs` or `puggles` and `user_country` exactly matches `us`. `my-rule2` will exclude documents from different specified indices with IDs `id3` and `id4` when the `query_string` fuzzily matches `rescue dogs`.
'
value: "{\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [ \"pugs\", \"puggles\" ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [ \"us\" ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [ \"rescue dogs\" ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n}"
required: true
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
properties:
result:
allOf:
- $ref: '#/components/schemas/_types.Result'
required:
- result
x-state: Generally available; Added in 8.10.0
x-variations:
- "\n PUT\n /_query_rules/{ruleset_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: put_ruleset.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: "PUT _query_rules/my-ruleset\n{\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [ \"pugs\", \"puggles\" ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [ \"us\" ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [ \"rescue dogs\" ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n}"
- lang: Python
source: "resp = client.query_rules.put_ruleset(\n ruleset_id=\"my-ruleset\",\n rules=[\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"pugs\",\n \"puggles\"\n ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [\n \"us\"\n ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"rescue dogs\"\n ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ],\n)"
- lang: JavaScript
source: "const response = await client.queryRules.putRuleset({\n ruleset_id: \"my-ruleset\",\n rules: [\n {\n rule_id: \"my-rule1\",\n type: \"pinned\",\n criteria: [\n {\n type: \"contains\",\n metadata: \"user_query\",\n values: [\"pugs\", \"puggles\"],\n },\n {\n type: \"exact\",\n metadata: \"user_country\",\n values: [\"us\"],\n },\n ],\n actions: {\n ids: [\"id1\", \"id2\"],\n },\n },\n {\n rule_id: \"my-rule2\",\n type: \"pinned\",\n criteria: [\n {\n type: \"fuzzy\",\n metadata: \"user_query\",\n values: [\"rescue dogs\"],\n },\n ],\n actions: {\n docs: [\n {\n _index: \"index1\",\n _id: \"id3\",\n },\n {\n _index: \"index2\",\n _id: \"id4\",\n },\n ],\n },\n },\n ],\n});"
- lang: Ruby
source: "response = client.query_rules.put_ruleset(\n ruleset_id: \"my-ruleset\",\n body: {\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"pugs\",\n \"puggles\"\n ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [\n \"us\"\n ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"rescue dogs\"\n ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n }\n)"
- lang: PHP
source: "$resp = $client->queryRules()->putRuleset([\n \"ruleset_id\" => \"my-ruleset\",\n \"body\" => [\n \"rules\" => array(\n [\n \"rule_id\" => \"my-rule1\",\n \"type\" => \"pinned\",\n \"criteria\" => array(\n [\n \"type\" => \"contains\",\n \"metadata\" => \"user_query\",\n \"values\" => array(\n \"pugs\",\n \"puggles\",\n ),\n ],\n [\n \"type\" => \"exact\",\n \"metadata\" => \"user_country\",\n \"values\" => array(\n \"us\",\n ),\n ],\n ),\n \"actions\" => [\n \"ids\" => array(\n \"id1\",\n \"id2\",\n ),\n ],\n ],\n [\n \"rule_id\" => \"my-rule2\",\n \"type\" => \"pinned\",\n \"criteria\" => array(\n [\n \"type\" => \"fuzzy\",\n \"metadata\" => \"user_query\",\n \"values\" => array(\n \"rescue dogs\",\n ),\n ],\n ),\n \"actions\" => [\n \"docs\" => array(\n [\n \"_index\" => \"index1\",\n \"_id\" => \"id3\",\n ],\n [\n \"_index\" => \"index2\",\n \"_id\" => \"id4\",\n ],\n ),\n ],\n ],\n ),\n ],\n]);"
- lang: curl
source: 'curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d ''{"rules":[{"rule_id":"my-rule1","type":"pinned","criteria":[{"type":"contains","metadata":"user_query","values":["pugs","puggles"]},{"type":"exact","metadata":"user_country","values":["us"]}],"actions":{"ids":["id1","id2"]}},{"rule_id":"my-rule2","type":"pinned","criteria":[{"type":"fuzzy","metadata":"user_query","values":["rescue dogs"]}],"actions":{"docs":[{"_index":"index1","_id":"id3"},{"_index":"index2","_id":"id4"}]}}]}'' "$ELASTICSEARCH_URL/_query_rules/my-ruleset"'
- lang: Java
source: "client.queryRules().putRuleset(p -> p\n .rules(List.of(QueryRule.queryRuleOf(q -> q\n .ruleId(\"my-rule1\")\n .type(QueryRuleType.Pinned)\n .criteria(List.of(QueryRuleCriteria.of(qu -> qu\n .type(QueryRuleCriteriaType.Contains)\n .metadata(\"user_query\")\n .values(List.of(JsonData.fromJson(\"\\\"pugs\\\"\"),JsonData.fromJson(\"\\\"puggles\\\"\")))\n ),QueryRuleCriteria.of(qu -> qu\n .type(QueryRuleCriteriaType.Exact)\n .metadata(\"user_country\")\n .values(JsonData.fromJson(\"\\\"us\\\"\"))\n )))\n .actions(a -> a\n .ids(List.of(\"id1\",\"id2\"))\n )\n ),QueryRule.queryRuleOf(q -> q\n .ruleId(\"my-rule2\")\n .type(QueryRuleType.Pinned)\n .criteria(c -> c\n .type(QueryRuleCriteriaType.Fuzzy)\n .metadata(\"user_query\")\n .values(JsonData.fromJson(\"\\\"rescue dogs\\\"\"))\n )\n .actions(a -> a\n .docs(List.of(PinnedDoc.of(pi -> pi\n .id(\"id3\")\n .index(\"index1\")\n ),PinnedDoc.of(pi -> pi\n .id(\"id4\")\n .index(\"index2\")\n )))\n )\n )))\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
delete:
tags:
- query_rules
summary: Delete a query ruleset
description: 'Remove a query ruleset and its associated data.
This is a destructive action that is not recoverable.'
externalDocs:
description: Delete a ruleset from the Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#delete-a-ruleset
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/delete-query-ruleset.html
operationId: query-rules-delete-ruleset
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset to delete
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/_types.AcknowledgedResponseBase'
x-state: Generally available; Added in 8.10.0
x-variations:
- "\n DELETE\n /_query_rules/{ruleset_id}\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: delete_ruleset.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: 'DELETE _query_rules/my-ruleset/
'
- lang: Python
source: "resp = client.query_rules.delete_ruleset(\n ruleset_id=\"my-ruleset\",\n)"
- lang: JavaScript
source: "const response = await client.queryRules.deleteRuleset({\n ruleset_id: \"my-ruleset\",\n});"
- lang: Ruby
source: "response = client.query_rules.delete_ruleset(\n ruleset_id: \"my-ruleset\"\n)"
- lang: PHP
source: "$resp = $client->queryRules()->deleteRuleset([\n \"ruleset_id\" => \"my-ruleset\",\n]);"
- lang: curl
source: 'curl -X DELETE -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_query_rules/my-ruleset/"'
- lang: Java
source: "client.queryRules().deleteRuleset(d -> d\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
/_query_rules:
get:
tags:
- query_rules
summary: Get all query rulesets
description: Get summarized information about the query rulesets.
externalDocs:
description: See rules and rulesets in Query Rules UI
url: https://www.elastic.co/docs/solutions/search/query-rules-ui#accessing-the-query-rules-ui
x-previousVersionUrl: https://www.elastic.co/guide/en/elasticsearch/reference/8.19/list-query-rulesets.html
operationId: query-rules-list-rulesets
parameters:
- in: query
name: from
description: The offset from the first result to fetch.
deprecated: false
schema:
type: number
style: form
- in: query
name: size
description: The maximum number of results to retrieve.
deprecated: false
schema:
type: number
style: form
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
properties:
count:
type: number
results:
type: array
items:
$ref: '#/components/schemas/query_rules.list_rulesets.QueryRulesetListItem'
required:
- count
- results
examples:
QueryRulesetListResponseExample1:
description: A successful response from `GET _query_rules/?from=0&size=3`.
value: "{\n \"count\": 3,\n \"results\": [\n {\n \"ruleset_id\": \"ruleset-1\",\n \"rule_total_count\": 1,\n \"rule_criteria_types_counts\": {\n \"exact\": 1\n }\n },\n {\n \"ruleset_id\": \"ruleset-2\",\n \"rule_total_count\": 2,\n \"rule_criteria_types_counts\": {\n \"exact\": 1,\n \"fuzzy\": 1\n }\n },\n {\n \"ruleset_id\": \"ruleset-3\",\n \"rule_total_count\": 3,\n \"rule_criteria_types_counts\": {\n \"exact\": 1,\n \"fuzzy\": 2\n }\n }\n ]\n}"
x-state: Generally available; Added in 8.10.0
x-variations:
- "\n GET\n /_query_rules\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: list_rulesets.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: 'GET _query_rules/?from=0&size=3
'
- lang: Python
source: "resp = client.query_rules.list_rulesets(\n from=\"0\",\n size=\"3\",\n)"
- lang: JavaScript
source: "const response = await client.queryRules.listRulesets({\n from: 0,\n size: 3,\n});"
- lang: Ruby
source: "response = client.query_rules.list_rulesets(\n from: \"0\",\n size: \"3\"\n)"
- lang: PHP
source: "$resp = $client->queryRules()->listRulesets([\n \"from\" => \"0\",\n \"size\" => \"3\",\n]);"
- lang: curl
source: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_query_rules/?from=0&size=3"'
- lang: Java
source: "client.queryRules().listRulesets(l -> l\n .from(0)\n .size(3)\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
/_query_rules/{ruleset_id}/_test:
post:
tags:
- query_rules
summary: Test a query ruleset
description: Evaluate match criteria against a query ruleset to identify the rules that would match that criteria.
operationId: query-rules-test
parameters:
- in: path
name: ruleset_id
description: The unique identifier of the query ruleset to be created or updated
required: true
deprecated: false
schema:
$ref: '#/components/schemas/_types.Id'
style: simple
requestBody:
content:
application/json:
schema:
type: object
properties:
match_criteria:
description: 'The match criteria to apply to rules in the given query ruleset.
Match criteria should match the keys defined in the `criteria.metadata` field of the rule.'
type: object
additionalProperties:
type: object
required:
- match_criteria
examples:
QueryRulesetTestRequestExample1:
description: 'Run `PUT _query_rules/my-ruleset` to create a new query ruleset. Two rules are associated with `my-ruleset`. `my-rule1` will pin documents with IDs `id1` and `id2` when `user_query` contains `pugs` or `puggles` and `user_country` exactly matches `us`. `my-rule2` will exclude documents from different specified indices with IDs `id3` and `id4` when the `query_string` fuzzily matches `rescue dogs`.
'
value: "{\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [ \"pugs\", \"puggles\" ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [ \"us\" ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [ \"rescue dogs\" ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n}"
required: true
responses:
'200':
description: ''
content:
application/json:
schema:
type: object
properties:
total_matched_rules:
type: number
matched_rules:
type: array
items:
$ref: '#/components/schemas/query_rules.test.QueryRulesetMatchedRule'
required:
- total_matched_rules
- matched_rules
examples:
QueryRulesetTestResponseExample1:
description: A successful response from `POST _query_rules/my-ruleset/_test`.
value: "{\n \"total_matched_rules\": 1,\n \"matched_rules\": [\n {\n \"ruleset_id\": \"my-ruleset\",\n \"rule_id\": \"my-rule1\"\n }\n ]\n}"
x-state: Generally available; Added in 8.10.0
x-variations:
- "\n POST\n /_query_rules/{ruleset_id}/_test\n
\n "
x-req-auth:
- 'Cluster privileges: `manage_search_query_rules`
'
x-api: test.query_rules
x-category: ai/ml
x-codeSamples:
- lang: Console
source: "PUT _query_rules/my-ruleset\n{\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [ \"pugs\", \"puggles\" ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [ \"us\" ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [ \"rescue dogs\" ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n}"
- lang: Python
source: "resp = client.query_rules.put_ruleset(\n ruleset_id=\"my-ruleset\",\n rules=[\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"pugs\",\n \"puggles\"\n ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [\n \"us\"\n ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"rescue dogs\"\n ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ],\n)"
- lang: JavaScript
source: "const response = await client.queryRules.putRuleset({\n ruleset_id: \"my-ruleset\",\n rules: [\n {\n rule_id: \"my-rule1\",\n type: \"pinned\",\n criteria: [\n {\n type: \"contains\",\n metadata: \"user_query\",\n values: [\"pugs\", \"puggles\"],\n },\n {\n type: \"exact\",\n metadata: \"user_country\",\n values: [\"us\"],\n },\n ],\n actions: {\n ids: [\"id1\", \"id2\"],\n },\n },\n {\n rule_id: \"my-rule2\",\n type: \"pinned\",\n criteria: [\n {\n type: \"fuzzy\",\n metadata: \"user_query\",\n values: [\"rescue dogs\"],\n },\n ],\n actions: {\n docs: [\n {\n _index: \"index1\",\n _id: \"id3\",\n },\n {\n _index: \"index2\",\n _id: \"id4\",\n },\n ],\n },\n },\n ],\n});"
- lang: Ruby
source: "response = client.query_rules.put_ruleset(\n ruleset_id: \"my-ruleset\",\n body: {\n \"rules\": [\n {\n \"rule_id\": \"my-rule1\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"contains\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"pugs\",\n \"puggles\"\n ]\n },\n {\n \"type\": \"exact\",\n \"metadata\": \"user_country\",\n \"values\": [\n \"us\"\n ]\n }\n ],\n \"actions\": {\n \"ids\": [\n \"id1\",\n \"id2\"\n ]\n }\n },\n {\n \"rule_id\": \"my-rule2\",\n \"type\": \"pinned\",\n \"criteria\": [\n {\n \"type\": \"fuzzy\",\n \"metadata\": \"user_query\",\n \"values\": [\n \"rescue dogs\"\n ]\n }\n ],\n \"actions\": {\n \"docs\": [\n {\n \"_index\": \"index1\",\n \"_id\": \"id3\"\n },\n {\n \"_index\": \"index2\",\n \"_id\": \"id4\"\n }\n ]\n }\n }\n ]\n }\n)"
- lang: PHP
source: "$resp = $client->queryRules()->putRuleset([\n \"ruleset_id\" => \"my-ruleset\",\n \"body\" => [\n \"rules\" => array(\n [\n \"rule_id\" => \"my-rule1\",\n \"type\" => \"pinned\",\n \"criteria\" => array(\n [\n \"type\" => \"contains\",\n \"metadata\" => \"user_query\",\n \"values\" => array(\n \"pugs\",\n \"puggles\",\n ),\n ],\n [\n \"type\" => \"exact\",\n \"metadata\" => \"user_country\",\n \"values\" => array(\n \"us\",\n ),\n ],\n ),\n \"actions\" => [\n \"ids\" => array(\n \"id1\",\n \"id2\",\n ),\n ],\n ],\n [\n \"rule_id\" => \"my-rule2\",\n \"type\" => \"pinned\",\n \"criteria\" => array(\n [\n \"type\" => \"fuzzy\",\n \"metadata\" => \"user_query\",\n \"values\" => array(\n \"rescue dogs\",\n ),\n ],\n ),\n \"actions\" => [\n \"docs\" => array(\n [\n \"_index\" => \"index1\",\n \"_id\" => \"id3\",\n ],\n [\n \"_index\" => \"index2\",\n \"_id\" => \"id4\",\n ],\n ),\n ],\n ],\n ),\n ],\n]);"
- lang: curl
source: 'curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d ''{"rules":[{"rule_id":"my-rule1","type":"pinned","criteria":[{"type":"contains","metadata":"user_query","values":["pugs","puggles"]},{"type":"exact","metadata":"user_country","values":["us"]}],"actions":{"ids":["id1","id2"]}},{"rule_id":"my-rule2","type":"pinned","criteria":[{"type":"fuzzy","metadata":"user_query","values":["rescue dogs"]}],"actions":{"docs":[{"_index":"index1","_id":"id3"},{"_index":"index2","_id":"id4"}]}}]}'' "$ELASTICSEARCH_URL/_query_rules/my-ruleset"'
- lang: Java
source: "client.queryRules().putRuleset(p -> p\n .rules(List.of(QueryRule.queryRuleOf(q -> q\n .ruleId(\"my-rule1\")\n .type(QueryRuleType.Pinned)\n .criteria(List.of(QueryRuleCriteria.of(qu -> qu\n .type(QueryRuleCriteriaType.Contains)\n .metadata(\"user_query\")\n .values(List.of(JsonData.fromJson(\"\\\"pugs\\\"\"),JsonData.fromJson(\"\\\"puggles\\\"\")))\n ),QueryRuleCriteria.of(qu -> qu\n .type(QueryRuleCriteriaType.Exact)\n .metadata(\"user_country\")\n .values(JsonData.fromJson(\"\\\"us\\\"\"))\n )))\n .actions(a -> a\n .ids(List.of(\"id1\",\"id2\"))\n )\n ),QueryRule.queryRuleOf(q -> q\n .ruleId(\"my-rule2\")\n .type(QueryRuleType.Pinned)\n .criteria(c -> c\n .type(QueryRuleCriteriaType.Fuzzy)\n .metadata(\"user_query\")\n .values(JsonData.fromJson(\"\\\"rescue dogs\\\"\"))\n )\n .actions(a -> a\n .docs(List.of(PinnedDoc.of(pi -> pi\n .id(\"id3\")\n .index(\"index1\")\n ),PinnedDoc.of(pi -> pi\n .id(\"id4\")\n .index(\"index2\")\n )))\n )\n )))\n .rulesetId(\"my-ruleset\")\n);\n"
x-metaTags:
- content: Elasticsearch
name: product_name
components:
schemas:
query_rules._types.QueryRuleset:
type: object
properties:
ruleset_id:
description: A unique identifier for the ruleset.
allOf:
- $ref: '#/components/schemas/_types.Id'
rules:
description: Rules associated with the query ruleset.
type: array
items:
$ref: '#/components/schemas/query_rules._types.QueryRule'
required:
- ruleset_id
- rules
_types.AcknowledgedResponseBase:
type: object
properties:
acknowledged:
description: For a successful response, this value is always true. On failure, an exception is returned instead.
type: boolean
required:
- acknowledged
query_rules._types.QueryRuleType:
type: string
enum:
- pinned
- exclude
_types.query_dsl.PinnedDoc:
type: object
properties:
_id:
description: The unique document ID.
allOf:
- $ref: '#/components/schemas/_types.Id'
_index:
description: The index that contains the document.
allOf:
- $ref: '#/components/schemas/_types.IndexName'
required:
- _id
query_rules.test.QueryRulesetMatchedRule:
type: object
properties:
ruleset_id:
description: Ruleset unique identifier
allOf:
- $ref: '#/components/schemas/_types.Id'
rule_id:
description: Rule unique identifier within that ruleset
allOf:
- $ref: '#/components/schemas/_types.Id'
required:
- ruleset_id
- rule_id
query_rules._types.QueryRuleCriteria:
type: object
properties:
type:
description: 'The type of criteria. The following criteria types are supported:
* `always`: Matches all queries, regardless of input.
* `contains`: Matches that contain this value anywhere in the field meet the criteria defined by the rule. Only applicable for string values.
* `exact`: Only exact matches meet the criteria defined by the rule. Applicable for string or numerical values.
* `fuzzy`: Exact matches or matches within the allowed Levenshtein Edit Distance meet the criteria defined by the rule. Only applicable for string values.
* `gt`: Matches with a value greater than this value meet the criteria defined by the rule. Only applicable for numerical values.
* `gte`: Matches with a value greater than or equal to this value meet the criteria defined by the rule. Only applicable for numerical values.
* `lt`: Matches with a value less than this value meet the criteria defined by the rule. Only applicable for numerical values.
* `lte`: Matches with a value less than or equal to this value meet the criteria defined by the rule. Only applicable for numerical values.
* `prefix`: Matches that start with this value meet the criteria defined by the rule. Only applicable for string values.
* `suffix`: Matches that end with this value meet the criteria defined by the rule. Only applicable for string values.'
allOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleCriteriaType'
metadata:
description: 'The metadata field to match against.
This metadata will be used to match against `match_criteria` sent in the rule.
It is required for all criteria types except `always`.'
type: string
values:
description: 'The values to match against the `metadata` field.
Only one value must match for the criteria to be met.
It is required for all criteria types except `always`.'
type: array
items:
type: object
required:
- type
query_rules.list_rulesets.QueryRulesetListItem:
type: object
properties:
ruleset_id:
description: A unique identifier for the ruleset.
allOf:
- $ref: '#/components/schemas/_types.Id'
rule_total_count:
description: The number of rules associated with the ruleset.
type: number
rule_criteria_types_counts:
description: 'A map of criteria type (for example, `exact`) to the number of rules of that type.
NOTE: The counts in `rule_criteria_types_counts` may be larger than the value of `rule_total_count` because a rule may have multiple criteria.'
type: object
additionalProperties:
type: number
rule_type_counts:
description: A map of rule type (for example, `pinned`) to the number of rules of that type.
type: object
additionalProperties:
type: number
required:
- ruleset_id
- rule_total_count
- rule_criteria_types_counts
- rule_type_counts
query_rules._types.QueryRule:
type: object
properties:
rule_id:
description: A unique identifier for the rule.
allOf:
- $ref: '#/components/schemas/_types.Id'
type:
description: 'The type of rule.
`pinned` will identify and pin specific documents to the top of search results.
`exclude` will exclude specific documents from search results.'
allOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleType'
criteria:
description: 'The criteria that must be met for the rule to be applied.
If multiple criteria are specified for a rule, all criteria must be met for the rule to be applied.'
oneOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleCriteria'
- type: array
items:
$ref: '#/components/schemas/query_rules._types.QueryRuleCriteria'
actions:
description: 'The actions to take when the rule is matched.
The format of this action depends on the rule type.'
allOf:
- $ref: '#/components/schemas/query_rules._types.QueryRuleActions'
priority:
type: number
required:
- rule_id
- type
- criteria
- actions
query_rules._types.QueryRuleCriteriaType:
type: string
enum:
- global
- exact
- exact_fuzzy
- fuzzy
- prefix
- suffix
- contains
- lt
- lte
- gt
- gte
- always
_types.Id:
type: string
_types.Result:
type: string
enum:
- created
- updated
- deleted
- not_found
- noop
query_rules._types.QueryRuleActions:
type: object
properties:
ids:
description: 'The unique document IDs of the documents to apply the rule to.
Only one of `ids` or `docs` may be specified and at least one must be specified.'
type: array
items:
$ref: '#/components/schemas/_types.Id'
docs:
description: 'The documents to apply the rule to.
Only one of `ids` or `docs` may be specified and at least one must be specified.
There is a maximum value of 100 documents in a rule.
You can specify the following attributes for each document:
* `_index`: The index of the document to pin.
* `_id`: The unique document ID.'
type: array
items:
$ref: '#/components/schemas/_types.query_dsl.PinnedDoc'
_types.IndexName:
type: string
securitySchemes:
apiKeyAuth:
type: apiKey
in: header
name: Authorization
description: "Elasticsearch APIs support key-based authentication.\nYou must create an API key and use the encoded value in the request header.\nFor example:\n\n```\ncurl -X GET \"${ES_URL}/_cat/indices?v=true\" \\\n -H \"Authorization: ApiKey ${API_KEY}\"\n```\n\nTo get API keys, use the `/_security/api_key` APIs."
basicAuth:
type: http
scheme: basic
bearerAuth:
type: http
scheme: bearer
description: 'Elasticsearch APIs support the use of bearer tokens in the `Authorization` HTTP header to authenticate with the API.
For examples, refer to [Token-based authentication services](https://www.elastic.co/docs/deploy-manage/users-roles/cluster-or-deployment-auth/token-based-authentication-services)'
x-tagGroups:
- name: AI & Machine Learning
tags:
- analytics
- graph
- inference
- ml
- ml anomaly
- ml data frame
- ml trained model
- query_rules
- text_structure
- name: Cluster Management
tags:
- ccr
- cluster
- connector
- data stream
- ilm
- indices
- rollup
- script
- search_application
- searchable_snapshots
- slm
- snapshot
- name: Data Processing
tags:
- enrich
- fleet
- ingest
- logstash
- synonyms
- transform
- name: Information & Monitoring
tags:
- cat
- features
- health_report
- info
- license
- migration
- tasks
- watcher
- xpack
- name: Search & Document APIs
tags:
- document
- eql
- esql
- search
- sql
- name: Security
tags:
- security