---
type: api
title: Create or update a query rule
description: Create or update a query rule within a query ruleset.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/operation/operation-query-rules-put-rule
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/operation/operation-query-rules-put-rule
products:
  - Elasticsearch
---

# Create or update a query rule

Availability: 8.15+

## Paths

- `PUT` `/_query_rules/{ruleset_id}/_rule/{rule_id}`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#bearerauth)

#### Path Parameters

- `ruleset_id` (string Id)
  The unique identifier of the query ruleset containing the rule to be created or updated.
- `rule_id` (string Id)
  The unique identifier of the query rule within the specified ruleset to be created or updated.

## 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.

## Required authorization

* Cluster privileges: `manage_search_query_rules`

[About editing rules and rulesets](https://www.elastic.co/docs/solutions/search/query-rules-ui#edit-a-rule)

## Request Body

`application/json`

- `type` (enum QueryRuleType) — required
  The type of rule.
  Values: `pinned`, `exclude`
- `criteria` (union oneOf) — required
  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.
  `QueryRuleCriteria` or `QueryRuleCriteria[]`
  - `type` (enum QueryRuleCriteriaType) — required
    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.
    Values: `global`, `exact`, `fuzzy`, `prefix`, `suffix`, `contains`, `lt`, `lte`, `gt`, `gte`, `always`
  - `metadata` (string) — optional
    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`.
  - `values` ([] object) — optional
    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`.
- `actions` ({} QueryRuleActions) — required
  The actions to take when the rule is matched.
The format of this action depends on the rule type.
  - `ids` ([] string Id) — optional
    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.
  - `docs` ([] PinnedDoc) — optional
    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.
    - `_id` (string Id) — required
      The unique document ID.
    - `_index` (string IndexName) — optional
      The index that contains the document.
- `priority` (number) — optional

## Response

`application/json`

- `result` (enum Result)
  Values: `created`, `updated`, `deleted`, `not_found`, `noop`
## Code Examples

#### Console

```console
POST _query_rules/my-ruleset/_test
{
  "match_criteria": {
    "query_string": "puggles"
  }
}
```

#### JavaScript

```javascript
const response = await client.queryRules.test({
  ruleset_id: "my-ruleset",
  match_criteria: {
    query_string: "puggles",
  },
});
```

#### Python

```python
resp = client.query_rules.test(
    ruleset_id="my-ruleset",
    match_criteria={
        "query_string": "puggles"
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_query_rules/my-ruleset/_test" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "match_criteria": {
      "query_string": "puggles"
    }
  }'
```

#### Java

```java
client.queryRules().test(t -> t
    .matchCriteria("query_string", JsonData.fromJson("\"puggles\""))
    .rulesetId("my-ruleset")
);
```

#### C#

```c#
var response = await client.QueryRules
    .TestAsync(rulesetId: "my-ruleset", d1 => d1
        .MatchCriteria(new Dictionary<string, object>()
        {
            { "query_string", "puggles" }
        })
    );
```

#### PHP

```php
$resp = $client->queryRules()->test([
    "ruleset_id" => "my-ruleset",
    "body" => [
        "match_criteria" => [
            "query_string" => "puggles",
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.query_rules.test(
  ruleset_id: "my-ruleset",
  body: {
    "match_criteria": {
      "query_string": "puggles"
    }
  }
)
```

