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

# Machine Learning Stage

export const schema = {
  "type": "object",
  "title": "Machine Learning",
  "description": "Use a machine learning model to generate a prediction about a query.",
  "required": ["modelId", "queryFeatureFieldName", "predictionFieldName"],
  "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"]
    },
    "modelId": {
      "type": "string",
      "title": "Machine Learning Model ID",
      "description": "The ID of the ML model stored in the Fusion blob store. If you trained a model using a Fusion Job, by default the model ID will be the same as the job name.",
      "minLength": 1,
      "reference": "blob",
      "blobType": "model:ml-model"
    },
    "queryFeatureFieldName": {
      "type": "string",
      "title": "Query Feature Field",
      "description": "Name of Request fields to feed into the model. Can be a single field name, or multiple field names are supported in this format: field1:weight,field2:weight,field3:weight",
      "default": "q",
      "minLength": 1
    },
    "predictionFieldName": {
      "type": "string",
      "title": "Prediction Field Name",
      "description": "Name of the Request field to store the prediction",
      "minLength": 1
    },
    "defaultValue": {
      "type": "string",
      "title": "Default Value",
      "description": "Value to provide if a prediction cannot be made"
    },
    "failOnError": {
      "type": "boolean",
      "title": "Fail on Error",
      "description": "Flag to indicate if this stage should throw an exception if an error occurs while generating a prediction for a document.",
      "default": false
    },
    "storeInContext": {
      "type": "boolean",
      "title": "Store the Prediction in the Context",
      "description": "Flag to indicate that the prediction should be set as a context property instead of setting a field on the document.",
      "default": false
    }
  },
  "category": "Advanced",
  "categoryPriority": 2,
  "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/machine-learning-query-stage

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

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

The Machine Learning query pipeline stage uses a trained machine learning model to analyze a field or fields of a [Request](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/4.2/?com/lucidworks/apollo/pipeline/query/Request.html) object and stores the results of analysis in a new field added to either the Request or the [Context](https://javadoc.lucidworks.com/fusion-pipeline-javadocs/4.2/com/lucidworks/apollo/pipeline/Context.html) object.

In order to use the Machine Learning Stage, you must train a machine learning model. There are two different ways to train a model:

* Use a Fusion job that trains a model, like [Logistic Regression](/docs/4/fusion-ai/reference/jobs/logistic-regression-classifier-training) or [Random Forest](/docs/4/fusion-ai/reference/jobs/random-forest-classifier-training).
* Train a model using [Spark’s MLlib API](https://spark.apache.org/docs/latest/mllib-guide.html) outside of Fusion, and upload this model into Fusion’s [blob store](/docs/4/fusion-server/concepts/indexing/blob-storage). Complete details are available in [Machine Learning Models in Fusion](/docs/4/fusion-ai/concepts/machine-learning/machine-learning-models).

<Tip>
  When specifying field names, multiple field names are supported, in this format: `field1:weight,field2:weight,field3:weight`
</Tip>

<LwTemplate />

## Configuration

<SchemaParamFields schema={schema} />
