Search code examples
azure-cognitive-search

How to create an Azure Search indexer using the REST API


Due to a bug in the Azure portal, I am needing to create an Azure Cognitive Search data source, index, and indexer programmatically using the REST API. There were no issues creating the data source or index, but the POST request below returns the following error.

{
    "error": {
        "code": "",
        "message": "The request is invalid. Details: dataSource : A resource without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified.\r\n"
    }
}

The following POST request is a modified example found on this page, with the variables replaced with the correct names of the service-name, admin-key, dataSourceName and targetIndexName.

POST request (using postman)

POST https://SERVICENAME.search.windows.net/indexers?api-version=2019-05-06
Content-Type: application/json
api-key: ADMINKEY

{
  "name" : "my-json-indexer",
  "dataSourceName" : "BLOBDATASOURCE",
  "targetIndexName" : "TARGETINDEX",
  "schedule" : { "interval" : "PT2H" },
  "parameters" : { "configuration" : { "parsingMode" : "json" } }
}

Solution

  • Seems that when you've created your data source, the type property was not provided.

    Here's the two requests:

    Create data source

    POST https://[service name].search.windows.net/datasources?api-version=2019-05-06
    Content-Type: application/json
    api-key: [admin key for Azure Cognitive Search]
    
    {
        "name" : "my-blob-datasource",
        "type" : "azureblob",
        "credentials" : { "connectionString" : "DefaultEndpointsProtocol=https;AccountName=<account name>;AccountKey=<account key>;" },
        "container" : { "name" : "my-container", "query" : "optional, my-folder" }
    }  
    

    Create indexer

    POST https://[service name].search.windows.net/indexers?api-version=2019-05-06
    Content-Type: application/json
    api-key: [admin key for Azure Cognitive Search]
    
    {
      "name" : "my-json-indexer",
      "dataSourceName" : "my-blob-datasource",
      "targetIndexName" : "my-target-index",
      "schedule" : { "interval" : "PT2H" },
      "parameters" : { "configuration" : { "parsingMode" : "json" } },
      "fieldMappings" : [
        { "sourceFieldName" : "/article/text", "targetFieldName" : "text" },
        { "sourceFieldName" : "/article/datePublished", "targetFieldName" : "date" },
        { "sourceFieldName" : "/article/tags", "targetFieldName" : "tags" }
        ]
    }