How to create the field mapping in Azure cognitive search

I have been using the Azure cognitive search to create the search index where the data source will be the azure SQL database.

I have created the search index for azure cognitive search and for the key index will be the id column from the table and the datatype will be fixed as edm.string type.

I have seen that we can change the datatype of id column to edm.int32 using the field mapping.

Can anyone help me with how we can able to create the field mapping for id column by changing the datatype to edm.int32

Upvotes: 0

Views: 622

Answers (2)

Gotem
Gotem

Reputation: 93

I think you mean change the data type from Int to String so that the ID field of the indexer accepts it? This should be possible using a field mapping.

Add this to your indexer definition:

"fieldMappings": [
  {
    "sourceFieldName": "id",
    "targetFieldName": "id",
    "mappingFunction": null
  }
]

Or I'm assuming even the following might work:

"fieldMappings": [
  {
    "sourceFieldName": "id"
  }
]

The docs say "If you're handling a data type issue, a field's data type is specified in the index definition. The field mapping just needs to have the field's name."

Source: https://learn.microsoft.com/en-us/azure/search/search-indexer-field-mappings?tabs=rest

Upvotes: 0

Thiago Custodio
Thiago Custodio

Reputation: 18387

Id column must be string. Here's the statement from the official doc:

Identify a document key. A document key is an index requirement. It's a single string field and it will be populated from a source data field that contains unique values. For example, if you're indexing from Blob Storage, the metadata storage path is often used as the document key because it uniquely identifies each blob in the container.

https://learn.microsoft.com/en-us/azure/search/search-how-to-create-search-index?tabs=portal#schema-checklist

You can create a T-SQL view and cast your id from int to varchar over there.

Upvotes: 0

Related Questions