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

# Neural Hybrid Query Stage

export const schema = {
  "type": "object",
  "title": "Hybrid Query",
  "description": "Hybrid of Vector and Lexical Search. Will be skipped if query string is blank or wildcard (* or *:*).  Note this will not work well if the incoming q parameter is a Solr query parser string (e.g. field_t:foo) rather than a raw user query string.  Note: The resulting query will ALWAYS be written to <request.params.q>.  This stage is deprecated and will be removed in a future release.  Please use the 'Neural Hybrid Search' stage instead.",
  "required": ["lexicalQuery", "lexicalWeight", "lexicalDepth", "vectorQueryField", "vector", "vectorWeight"],
  "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"]
    },
    "lexicalQuery": {
      "type": "string",
      "title": "Lexical Query Input",
      "description": "The lexical query itself is retrieved from here.  This field supports Template Expressions such as '<request.params.q>' to evaluate the original user query.",
      "default": "<request.params.q>"
    },
    "lexicalWeight": {
      "type": "number",
      "title": "Lexical Query Weight",
      "description": "Relative weight of the lexical query. If this value is 0, no re-ranking will be applied using the lexical query scores.",
      "default": 0.3,
      "maximum": 10,
      "exclusiveMaximum": false
    },
    "lexicalDepth": {
      "type": "integer",
      "title": "Number of Lexical Results",
      "description": "The number of lexical search results to include in re-ranking.  This value is ignored if \"Lexical Query Weight\" is 0.",
      "default": 1000
    },
    "vectorQueryField": {
      "type": "string",
      "title": "Vector Query Field",
      "description": "The name of the Solr field for knn vector search."
    },
    "vector": {
      "type": "string",
      "title": "Vector Input",
      "description": "The Vector itself is retrieved from here.  This field supports Template Expressions such as '<ctx.vector>' to evaluate the context variable resulting from a previous stage, such as the Vectorize Query via Lucidworks AI stage.",
      "default": "<ctx.vector>"
    },
    "vectorWeight": {
      "type": "number",
      "title": "Vector Query Weight",
      "description": "Relative weight of the vector query.",
      "default": 0.7,
      "maximum": 10,
      "exclusiveMaximum": false,
      "minimum": 0.001,
      "exclusiveMinimum": false
    },
    "knn": {
      "type": "object",
      "title": "Use KNN Query",
      "description": "Select this option if you wish to use the 'knn' query parser and configure its options. This option may not be selected if 'Use VecSim Query' is selected. Using 'knn' is the default if neither option is selected",
      "required": ["vectorDepth"],
      "properties": {
        "vectorDepth": {
          "type": "integer",
          "title": "Number of Vector Results",
          "description": "The number of vector search results to include in re-ranking.  Also known as topK.",
          "default": 1000
        }
      }
    },
    "vecSim": {
      "type": "object",
      "title": "Use VecSim Query",
      "description": "Select this option if you wish to use the 'vecSim' query parser and configure its options. This option may not be selected if 'Use KNN Query' is selected. Using 'knn' is the default if neither option is selected",
      "required": ["minReturnSim", "minTraverseSim"],
      "properties": {
        "minReturnSim": {
          "type": "number",
          "title": "Min Return Vector Similarity",
          "description": "The minimum vector similarity value to qualify as a match from the Vector portion of the hybrid query.",
          "default": 0.85,
          "maximum": 1,
          "exclusiveMaximum": false
        },
        "minTraverseSim": {
          "type": "number",
          "title": "Min Traversal Vector Similarity",
          "description": "The minimum vector similarity value to use when walking the graph during the Vector portion of the hybrid query. Must be lower than, or equal to, the Min Return Vector Similarity",
          "default": 0.5,
          "maximum": 1,
          "exclusiveMaximum": false
        }
      }
    },
    "minSimFilter": {
      "type": "number",
      "title": "Minimum Vector Similarity Filter",
      "description": "Select this option if you wish to apply a minimum similarity threshold for filtering documents. This option will apply to all documents, regardless of other score boosting such as rules or signals.",
      "maximum": 1,
      "exclusiveMaximum": false
    }
  },
  "category": "AI",
  "categoryPriority": 10,
  "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/lucidworks-search/09-developer-documentation/config-specs/query-pipeline-stages/neural-hybrid-query-stage

[mintlify link]: https://doc.lucidworks.com/docs/lucidworks-search/09-developer-documentation/config-specs/query-pipeline-stages/neural-hybrid-query-stage

[old doc.lw link]: https://doc.lucidworks.com/managed-fusion/5.9/4xm0li

The Neural Hybrid Query stage performs hybrid lexical-semantic search that combines BM25-type lexical search with KNN dense vector search via Solr.

Lucidworks Search automatically switches to lexical-only search if vectorization is unavailable to ensure uninterrupted results.

* If no vector is produced, you will see the message: `Input vector value is blank, skipping vector processing`.
* If a vector exists but is empty, you will see the message: `Input vector value is empty, {}, skipping vector processing`.

Not sure which hybrid query stage is right for you? Read about the
[differences between the hybrid query stages](/docs/lucidworks-search/11-vector-search/hybrid-stage-differences).

<Note>
  This feature is available starting in Lucidworks Search 5.9.10 and in all subsequent Lucidworks Search 5.9 releases.
</Note>

<LwTemplate />

## Prefiltering

Prefiltering is a technique that can improve performance and accuracy by filtering documents before applying the algorithm, reducing the number of documents that need to be processed.
This is especially effective with the KNN algorithm.

Prefiltering is disabled by default.
**To enable it, uncheck **Block pre-filtering** in this stage.**

When prefiltering is enabled, you can configure the filters using one or both of these methods:

* **Security filters**\
  You can use security filters as prefilters by placing the [Graph Security Trimming Stage](/docs/5/fusion/reference/config-ref/pipeline-stages/query-stages/security-trimming-graph-query-stage) *after* this one in the pipeline.\
  Then Fusion uses the security trimming filter as a prefilter.
* **JavaScript**\
  When prefiltering is enabled, this stage adds a `preFilterKey` object to the Javascript `ctx` object.\
  You can place a [Javascript stage](/docs/5/fusion/reference/config-ref/pipeline-stages/query-stages/javascript-query-stage) after this one and use it to access the `preFilterKey` object, as in this example:

  ```js theme={"dark"}
  if(ctx.hasProperty("preFilterKey")) {
    var preFilter = ctx.getProperty("preFilterKey");
    preFilter.addFilter(filterQuery)
  }
  ```
* **Additional Query Parameters stage**\
  If you do not want to create a JavaScript stage, you can create additional query parameters to prefilter the documents to be processed by using what the previous JavaScript example adds to the request. The following example uses a single prefilter:

  ```js theme={"dark"}
  "fq" = "{!bool filter=$vectorPreFilter}"
  "vectorPreFilter" = "EXAMPLE_FILTER"
  ```

  The following example uses multiple prefilters:

  ```js theme={"dark"}
  "fq": "{!bool filter=$filterClauses}",
  "vectorPreFilter": "{!bool should=$filterClauses}",
  "filterClauses": ["id:EXAMPLE_FILTER1","id:EXAMPLE_FILTER2"]
  ```

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