Type to search.

Query rules retriever

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

The rule retriever enables fine-grained control over search results by applying contextual query rules to pin or exclude documents for specific queries. This retriever has similar functionality to the rule query, but works out of the box with other retrievers.

To use the rule retriever you must first create one or more query rulesets using the query rules management APIs.

retriever

(Required, retriever)

The child retriever that returns the results to apply query rules on top of. This can be a standalone retriever such as the standard or knn retriever, or it can be a compound retriever.

ruleset_ids

(Required, array)

An array of one or more unique query ruleset IDs with query-based rules to match and apply as applicable. Rulesets and their associated rules are evaluated in the order in which they are specified in the query and ruleset. The maximum number of rulesets to specify is 10.

match_criteria

(Required, object)

Defines the match criteria to apply to rules in the given query ruleset(s). Match criteria should match the keys defined in the criteria.metadata field of the rule.

rank_window_size

(Optional, int)

The number of top documents to return from the rule retriever. Defaults to 10.

This example shows the rule retriever executed without any additional retrievers. It runs the query defined by the retriever and applies the rules from my-ruleset on top of the returned results.

JSON
GET movies/_search
{
  "retriever": {
    "rule": {
      "match_criteria": {
        "query_string": "harry potter"
      },
      "ruleset_ids": [
        "my-ruleset"
      ],
      "retriever": {
        "standard": {
          "query": {
            "query_string": {
              "query": "harry potter"
            }
          }
        }
      }
    }
  }
}

This example shows how to combine the rule retriever with other rerank retrievers such as rrf or text_similarity_reranker.

Warning

The rule retriever will apply rules to any documents returned from its defined retriever or any of its sub-retrievers. This means that for the best results, the rule retriever should be the outermost defined retriever. Nesting a rule retriever as a sub-retriever under a reranker such as rrf or text_similarity_reranker may not produce the expected results.

JSON
GET movies/_search
{
  "retriever": {
    "rule": {
      "match_criteria": {
        "query_string": "harry potter"
      },
      "ruleset_ids": [
        "my-ruleset"
      ],
      "retriever": {
        "rrf": {
          "retrievers": [
            {
              "standard": {
                "query": {
                  "query_string": {
                    "query": "sorcerer's stone"
                  }
                }
              }
            },
            {
              "standard": {
                "query": {
                  "query_string": {
                    "query": "chamber of secrets"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
  1. The rule retriever is the outermost retriever, applying rules to the search results that were previously reranked using the rrf retriever.
  2. The rrf retriever returns results from all of its sub-retrievers, and the output of the rrf retriever is used as input to the rule retriever.