> ## 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"]
    },
    "legacy": {
      "type": "boolean",
      "title": "Legacy",
      "description": "True if this stage only supports legacy mode",
      "hints": ["readonly", "hidden"]
    },
    "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. Supports template expressions."
    },
    "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. Supports template expressions."
    },
    "hierarchicalFilter": {
      "type": "array",
      "title": "Hierarchical Filter",
      "description": "Document fields that use delimiters to express a hierarchical structure.",
      "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"
          }
        }
      }
    },
    "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
    },
    "maxWaitMs": {
      "type": "integer",
      "title": "Max Wait for Lookup (ms)",
      "description": "Max time to wait for call to remote tagger collection to return; set to -1 to disable.",
      "default": 500
    },
    "emConfig": {
      "type": "object",
      "title": "Commerce Studio Configuration",
      "description": "This values applies only when application is connected to Commerce Studio.",
      "properties": {
        "abTestHashHeader": {
          "type": "string",
          "title": "AB Test Header for Variant Selection",
          "description": "Http header with value to use for A/B test determined variant selection",
          "default": "User-Agent"
        }
      }
    }
  },
  "category": "Fetch Data",
  "categoryPriority": 6,
  "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/5/fusion/reference/config-ref/pipeline-stages/query-stages/query-rules-query-stage

[mintlify link]: https://doc.lucidworks.com/docs/5/fusion/reference/config-ref/pipeline-stages/query-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 [`COLLECTION_NAME_query_rewrite` collection](/docs/5/fusion/getting-data-out/query-enhancement/query-rewriting) and matches them against the query. Matching rules that perform [query rewriting](/docs/5/fusion/getting-data-out/query-enhancement/query-rewriting) are applied at this stage, while matching rules that perform [response rewriting](/docs/5/fusion/getting-data-out/query-enhancement/response-rewriting) are applied by the [Modify Response with Rules](/docs/5/fusion/reference/config-ref/pipeline-stages/query-stages/rules-augment-response-query-stage) stage later in the pipeline.

<Note>
  To trigger a rule that contains a tag, specify the tagname in the request URL of the user search app. See [Easily define triggers in tags](/docs/5/fusion/getting-data-out/query-enhancement/business-rules/general-config) for more information.
</Note>

See [Rules](/docs/5/fusion/getting-data-out/query-enhancement/business-rules/overview) for additional details about how to configure this set of features.

This stage supports [asynchronous processing](/docs/5/fusion/getting-data-out/query-basics/query-pipelines/overview).

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

<LwTemplate />

## Query pipeline stage condition examples

Stages can be triggered conditionally when a script in the **Condition** field evaluates to true.
Some examples are shown below.

Run this stage only for mobile clients:

```js wrap  theme={"dark"}
params.deviceType === "mobile"
```

Run this stage when debugging is enabled:

```js wrap  theme={"dark"}
params.debug === "true"
```

Run this stage when the query includes a specific term:

```js wrap  theme={"dark"}
params.q && params.q.includes("sale")
```

Run this stage when multiple conditions are met:

```js wrap  theme={"dark"}
request.hasParam("fusion-user-name") && request.getFirstParam("fusion-user-name").equals("SuperUser");
!request.hasParam("isFusionPluginQuery")
```

The first condition checks that the request parameter "fusion-user-name" is present and has the value "SuperUser".
The second condition checks that the request parameter "isFusionPluginQuery" is not present.

## 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} />
