> ## Documentation Index
> Fetch the complete documentation index at: https://doc.lucidworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Apply Rules Stage

export const schema = {
  "type": "object",
  "title": "Apply Rules",
  "description": "Look up and apply rules to the query",
  "required": ["handler", "method"],
  "properties": {
    "skip": {
      "type": "boolean",
      "title": "Skip This Stage",
      "description": "Set to true to skip this stage.",
      "default": false,
      "hints": ["advanced"]
    },
    "label": {
      "type": "string",
      "title": "Label",
      "description": "A unique label for this stage.",
      "hints": ["advanced"],
      "maxLength": 255
    },
    "condition": {
      "type": "string",
      "title": "Condition",
      "description": "Define a conditional script that must result in true or false. This can be used to determine if the stage should process or not.",
      "hints": ["code", "code/javascript", "advanced"]
    },
    "asyncConfig": {
      "type": "object",
      "title": "Asynchronous Execution Config",
      "required": ["enabled", "asyncId"],
      "properties": {
        "enabled": {
          "type": "boolean",
          "title": "Enable Async Execution",
          "description": "Run the expensive data loading or processing part of this stage in a separate thread allowing the pipeline to continue executing. The results of this asynchronous execution can be merged into the pipeline request using a downstream \"Merge Async Results\" stage.",
          "default": false
        },
        "asyncId": {
          "type": "string",
          "title": "Async ID",
          "description": "A unique value to use as reference in downstream \"Merge Async Results\" stages."
        }
      }
    },
    "collection": {
      "type": "string",
      "title": "Collection",
      "description": "The collection to read rules from. Leave blank to use the default rules collection for the current application."
    },
    "handler": {
      "type": "string",
      "title": "Request Handler",
      "default": "select"
    },
    "method": {
      "type": "string",
      "title": "HTTP Method",
      "enum": ["GET", "POST"],
      "default": "POST"
    },
    "ruleLimit": {
      "type": "string",
      "title": "Rule Triggering Limit",
      "description": "Max number of business rules to be triggered by the same query. The default rules matching limit is 100. This config overwrites \"rows\" param set in \"Query Parameters\" section",
      "default": "100"
    },
    "params": {
      "type": "array",
      "title": "Query Parameters",
      "description": "Additional query parameters to use when querying the rules collection. NOTE: \"rows\" parameter set here will be overwritten by the config in \"Rule Triggering Limit\" section",
      "items": {
        "type": "object",
        "required": ["key"],
        "properties": {
          "key": {
            "type": "string",
            "title": "Parameter Name"
          },
          "value": {
            "type": "string",
            "title": "Parameter Value"
          }
        }
      }
    },
    "subqueryRewritePipelineId": {
      "type": "string",
      "title": "Subquery Rewrite Pipeline Id",
      "description": "Option to call a Fusion query pipeline to modify rule-retrieving subquery"
    },
    "hierarchicalFilter": {
      "type": "array",
      "title": "Hierarchical Filter",
      "description": "Specify hierarchical filter and delimiter, e.g. category: clothing/dress/cocktail",
      "items": {
        "type": "object",
        "required": ["field", "delimiter"],
        "properties": {
          "field": {
            "type": "string",
            "title": "Field Name"
          },
          "delimiter": {
            "type": "string",
            "title": "Field Delimiter"
          }
        }
      }
    },
    "headers": {
      "type": "array",
      "title": "Headers",
      "items": {
        "type": "object",
        "required": ["key"],
        "properties": {
          "key": {
            "type": "string",
            "title": "Parameter Name"
          },
          "value": {
            "type": "string",
            "title": "Parameter Value"
          }
        }
      }
    },
    "useEmbeddedSolrServer": {
      "type": "boolean",
      "title": "Use Embedded Solr",
      "description": "If checked, then this stage will load the remote tagger collection into an embedded Solr to perform the lookup in memory. Only use this option if the tagger collection fits in the memory available to the Fusion API service.",
      "default": false
    },
    "useOriginalQueryIfNoRulesMatch": {
      "type": "boolean",
      "title": "Use Original Query If No Rules Match",
      "description": "If checked, then this stage will try to match rules using the original query (un-tagged) sent into the tagger stage if available.",
      "default": true
    },
    "matchPartialFilterQueries": {
      "type": "boolean",
      "title": "Partially Matched Filter Queries Will Trigger the Rule",
      "description": "If checked, then this stage will trigger filter rules as long as there is one filter query in the query parameter that matches the filter specified in the rule.",
      "default": true
    }
  },
  "category": "Fetch Data",
  "categoryPriority": 5,
  "unsafe": false
};

export const SchemaParamFields = ({schema}) => {
  const sanitize = str => {
    if (typeof str !== "string") return str;
    return str.replace(/^"(.*)"$/s, "$1").replace(/\\/g, "").replace(/"/g, "'");
  };
  const formatDescription = str => {
    const s = sanitize(str);
    return (/[.!?]\)*$/).test(s) ? s : `${s}.`;
  };
  const {description, properties = {}, required: requiredProps = []} = schema;
  const visibleProps = useMemo(() => Object.entries(properties).filter(([, prop]) => !prop.hints?.includes("hidden")), [properties]);
  return <div>
      {description && <p>{formatDescription(description)}</p>}

      {visibleProps.map(([name, prop]) => {
    const isRequired = requiredProps.includes(name);
    const hasDefault = prop.default !== undefined;
    const rawDefault = prop.default;
    const isComplexDefault = hasDefault && (typeof rawDefault === "object" || typeof rawDefault === "string" && (rawDefault.length > 20 || rawDefault.includes('"')));
    const fieldProps = {
      key: name,
      body: prop.title || name,
      type: prop.type,
      ...prop.title && ({
        post: [<><span className="text-stone-400 dark:text-stone-500">API property: </span>{name}</>]
      }),
      ...isRequired && ({
        required: true
      }),
      ...!isComplexDefault && hasDefault ? {
        default: sanitize(String(rawDefault))
      } : {}
    };
    const isObject = prop.type === "object" && prop.properties;
    const isArrayOfObjects = prop.type === "array" && prop.items?.type === "object" && prop.items.properties;
    return <ParamField {...fieldProps}>
            {prop.description && <p>{formatDescription(prop.description)}</p>}

            {isComplexDefault && <div className="flex">
                <p>
                  <strong>Default:</strong>
                </p>
                <pre className="!my-0">
                  <code>
                    {JSON.stringify(rawDefault, null, 2)}
                  </code>
                </pre>
              </div>}

            {isArrayOfObjects && <div className="flex">
              <p>
                <strong>Object attributes:</strong>
              </p>
              <pre className="!my-0">
                <code>
                  {'{\n'}
                  {Object.entries(prop.items.properties).map(([iname, iprop]) => <>
                      {`  ${iname}`}
                      {prop.items?.required?.includes(iname) && <span style={{
      color: 'red'
    }}> required</span>}
                      {`: {\n    display name: ${sanitize(iprop.title || '')}\n    type: ${iprop.type}\n  }\n`}
                    </>)}
                  {'}'}
                </code>
              </pre>
              </div>}

            {isObject && <Expandable title="properties">
                <SchemaParamFields schema={{
      properties: prop.properties,
      required: prop.required
    }} />
              </Expandable>}
          </ParamField>;
  })}
    </div>;
};

export const LwTemplate = ({title = "Key questions to get you started", icon = "sparkles", cta = "Powered by Agent Studio", linkHref = "https://lucidworks.com/demo/?utm_source=docs&utm_medium=referral&utm_campaign=docs_cta_ai"}) => {
  const [isLoaded, setIsLoaded] = useState(false);
  useEffect(() => {
    const timer = setTimeout(() => {
      setIsLoaded(true);
    }, 500);
    return () => clearTimeout(timer);
  }, []);
  return <div className="lw-template-container">
      <Card title={title} icon={icon}>
        {isLoaded && <span dangerouslySetInnerHTML={{
    __html: `<lw-template id="a029c1a9-28be-427e-b0e1-5d918920246a"></lw-template
            >`
  }} />}
        <Link href={linkHref} className="agent-studio-link text-left text-gray-600 gap-2 dark:text-gray-400 text-sm font-medium flex flex-row items-center hover:text-primary dark:hover:text-primary-light group-hover:text-primary group-hover:dark:text-primary-light">Powered by Lucidworks Agent Studio</Link>
      </Card>
    </div>;
};

[localhost link]: http://localhost:3000/docs/4/fusion-ai/reference/query-pipeline-stages/query-rules-query-stage

[mintlify link]: https://doc.lucidworks.com/docs/4/fusion-ai/reference/query-pipeline-stages/query-rules-query-stage

[old doc.lw link]: https://doc.lucidworks.com/fusion/5.9/535

This stage looks up rules that have been deployed to the [response rewriting](/docs/4/fusion-ai/concepts/response-rewriting) are applied by the [Modify Response with Rules](/docs/4/fusion-server/reference/pipeline-stages/query/rules-augment-response-query-stage) stage later in the pipeline.

See [Rules](/docs/4/fusion-ai/concepts/query-rewriting/business-rules) for additional details about how to configure this set of features.

This stage supports [asynchronous processing](/docs/4/fusion-server/concepts/querying/pipelines/query-pipelines).

<Note>
  Although this stage is available without a Fusion AI license, it is only effective after creating rules in Fusion AI.
</Note>

<LwTemplate />

## Configuration

<Tip>
  When entering configuration values in the UI, use *unescaped* characters, such as `\t` for the tab character. When entering configuration values in the API, use *escaped* characters, such as `\\t` for the tab character.
</Tip>

<SchemaParamFields schema={schema} />
