Create or update query ruleset
editCreate or update query ruleset
editThis functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.
Creates or updates a query ruleset.
Request
editPUT _query_rules/<ruleset_id>
Prerequisites
editRequires the manage_search_query_rules privilege.
Request body
edit-
rules - (Required, array of objects) The specific rules included in this query ruleset.
Each rule must have the following information:
-
rule_id(Required, string) A unique identifier for this rule. -
type(Required, string) The type of rule. At this time onlypinnedquery rule types are allowed. -
criteria(Required, array of objects) 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. -
actions(Required, object) The actions to take when the rule is matched. The format of this action depends on the rule type.
Criteria must have the following information:
-
type(Required, string) The type of criteria. The following criteria types are supported:-
exactOnly exact matches meet the criteria defined by the rule. Applicable for string or numerical values. -
fuzzyExact matches or matches within the allowed Levenshtein Edit Distance meet the criteria defined by the rule. Only applicable for string values. -
prefixMatches that start with this value meet the criteria defined by the rule. Only applicable for string values. -
suffixMatches that end with this value meet the criteria defined by the rule. Only applicable for string values. -
containsMatches that contain this value anywhere in the field meet the criteria defined by the rule. Only applicable for string values. -
ltMatches with a value less than this value meet the criteria defined by the rule. Only applicable for numerical values. -
lteMatches with a value less than or equal to this value meet the criteria defined by the rule. Only applicable for numerical values. -
gtMatches with a value greater than this value meet the criteria defined by the rule. Only applicable for numerical values. -
gteMatches with a value greater than or equal to this value meet the criteria defined by the rule. Only applicable for numerical values. -
alwaysMatches all queries, regardless of input.
-
-
metadata(Optional, string) The metadata field to match against. This metadata will be used to match againstmatch_criteriasent in the Rule. Required for all criteria types exceptglobal. -
values(Optional, array of strings) The values to match against the metadata field. Only one value must match for the criteria to be met. Required for all criteria types exceptglobal.
Actions depend on the rule type.
For pinned rules, actions follow the format specified by the Pinned Query.
The following actions are allowed:
-
ids(Optional, array of strings) The unique document IDs of the documents to pin. Only one ofidsordocsmay be specified, and at least one must be specified. -
docs(Optional, array of objects) The documents to pin. Only one ofidsordocsmay be specified, and at least one must be specified. You can specify the following attributes for each document:-
_index(Required, string) The index of the document to pin. -
_id(Required, string) The unique document ID.
-
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.
Examples
editThe following example creates a new query ruleset called my-ruleset.
Two rules are associated with my-ruleset:
-
my-rule1will pin documents with IDsid1andid2whenuser_querycontainspugsorpugglesanduser_countryexactly matchesus. -
my-rule2will pin documents from different, specified indices with IDsid3andid4when thequery_stringfuzzily matchesrescue dogs.
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"
}
]
}
}
]
}