# Add function and field

Add a function with its output field and optional index configuration. The input field must already exist and have its analyzer enabled for BM25.

SDK: `@zilliz/milvus2-sdk-node@3.0.10` · Tested with Milvus 3.0.6

Canonical documentation: https://milvus-io.github.io/milvus-sdk-node/rest-api/schema/add-function-field/

## Section links

- [cURL command](https://milvus-io.github.io/milvus-sdk-node/rest-api/schema/add-function-field/#curl-command)
- [Default request](https://milvus-io.github.io/milvus-sdk-node/rest-api/schema/add-function-field/#default-request)
- [Sample response](https://milvus-io.github.io/milvus-sdk-node/rest-api/schema/add-function-field/#sample-response)
- [Request fields](https://milvus-io.github.io/milvus-sdk-node/rest-api/schema/add-function-field/#request-fields)

`POST /v2/vectordb/collections/add_function_field`

Replace `https://your-cluster.example.com` with your Milvus endpoint. Set `MILVUS_TOKEN` for authentication or omit the Authorization header on an unauthenticated cluster.

## cURL command

```bash
curl --request POST --url 'https://your-cluster.example.com/v2/vectordb/collections/add_function_field' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer ${MILVUS_TOKEN}" \
  --data-raw '{
  "dbName": "default",
  "collectionName": "books",
  "function": {
    "name": "title_bm25",
    "type": "BM25",
    "inputFieldNames": [
      "title"
    ],
    "outputFieldNames": [
      "title_sparse"
    ]
  },
  "outputField": {
    "fieldName": "title_sparse",
    "dataType": "SparseFloatVector"
  },
  "indexParams": {
    "fieldName": "title_sparse",
    "indexType": "SPARSE_INVERTED_INDEX",
    "metricType": "BM25"
  }
}'
```

## Default request

```json
{
  "dbName": "default",
  "collectionName": "books",
  "function": {
    "name": "title_bm25",
    "type": "BM25",
    "inputFieldNames": [
      "title"
    ],
    "outputFieldNames": [
      "title_sparse"
    ]
  },
  "outputField": {
    "fieldName": "title_sparse",
    "dataType": "SparseFloatVector"
  },
  "indexParams": {
    "fieldName": "title_sparse",
    "indexType": "SPARSE_INVERTED_INDEX",
    "metricType": "BM25"
  }
}
```

## Sample response

```json
{
  "code": 0,
  "data": {}
}
```

## Request fields

- `dbName` string
  Database name. Operations on collections use default when omitted.
- `collectionName` string (required)
  Name of the collection.
- `function` object (required)
  Function definition with name, type, inputFieldNames and outputFieldNames.
- `outputField` object (required)
  Schema of the field created for the function output.
- `indexParams` object
  Index definition for the new output field.

See [`HttpClient.addFunctionField`](https://milvus-io.github.io/milvus-sdk-node/api-reference/http-client/#addfunctionfield) for the Node.js SDK method.
