---
type: api
title: 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. Query rules are organized into rulesets, collections of query rules that are matched against incoming queries. Query rules are applied using the rule query.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/group/endpoint-query_rules
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/group/endpoint-query_rules
products:
  - Elasticsearch Serverless
---

# Query rules

`query_rules`

Query rules enable you to configure per-query rules that are applied at query time to queries that match the specific rule. Query rules are organized into rulesets, collections of query rules that are matched against incoming queries. Query rules are applied using the rule query.
If a query matches one or more rules in the ruleset, the query is re-written to apply the rules before searching. This allows pinning documents for only queries that match a specific term. Alternatively, you can use the [Query Rules UI](https://www.elastic.co/docs/solutions/search/query-rules-ui) to manage query rules.

[Learn more about searching with query rules.](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/searching-with-query-rules)

- [Get a query rule](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-get-rule): `GET` `/_query_rules/{ruleset_id}/_rule/{rule_id}`
- [Create or update a query rule](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-put-rule): `PUT` `/_query_rules/{ruleset_id}/_rule/{rule_id}`
- [Delete a query rule](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-delete-rule): `DELETE` `/_query_rules/{ruleset_id}/_rule/{rule_id}`
- [Get a query ruleset](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-get-ruleset): `GET` `/_query_rules/{ruleset_id}`
- [Create or update a query ruleset](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-put-ruleset): `PUT` `/_query_rules/{ruleset_id}`
- [Delete a query ruleset](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-delete-ruleset): `DELETE` `/_query_rules/{ruleset_id}`
- [Get all query rulesets](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-list-rulesets): `GET` `/_query_rules`
- [Test a query ruleset](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-query-rules-test): `POST` `/_query_rules/{ruleset_id}/_test`
