Test a query ruleset
Generally available
Evaluate match criteria against a query ruleset to identify the rules that would match that criteria.
Required authorization
- Cluster privileges:
manage_search_query_rules
Path parameters
-
ruleset_id
string Required The unique identifier of the query ruleset to be created or updated
Body
Required
-
match_criteria
object Required 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.
POST
/_query_rules/{ruleset_id}/_test
Console
PUT _query_rules/my-ruleset
{
"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"
}
]
}
}
]
}
resp = client.query_rules.put_ruleset(
ruleset_id="my-ruleset",
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"
}
]
}
}
],
)
const response = await client.queryRules.putRuleset({
ruleset_id: "my-ruleset",
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",
},
],
},
},
],
});
response = client.query_rules.put_ruleset(
ruleset_id: "my-ruleset",
body: {
"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"
}
]
}
}
]
}
)
$resp = $client->queryRules()->putRuleset([
"ruleset_id" => "my-ruleset",
"body" => [
"rules" => array(
[
"rule_id" => "my-rule1",
"type" => "pinned",
"criteria" => array(
[
"type" => "contains",
"metadata" => "user_query",
"values" => array(
"pugs",
"puggles",
),
],
[
"type" => "exact",
"metadata" => "user_country",
"values" => array(
"us",
),
],
),
"actions" => [
"ids" => array(
"id1",
"id2",
),
],
],
[
"rule_id" => "my-rule2",
"type" => "pinned",
"criteria" => array(
[
"type" => "fuzzy",
"metadata" => "user_query",
"values" => array(
"rescue dogs",
),
],
),
"actions" => [
"docs" => array(
[
"_index" => "index1",
"_id" => "id3",
],
[
"_index" => "index2",
"_id" => "id4",
],
),
],
],
),
],
]);
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"
Request example
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`.
{
"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"
}
]
}
}
]
}
Response examples (200)
A successful response from `POST _query_rules/my-ruleset/_test`.
{
"total_matched_rules": 1,
"matched_rules": [
{
"ruleset_id": "my-ruleset",
"rule_id": "my-rule1"
}
]
}