Crista23
Crista23

Reputation: 3243

Define AzureSearch index fieldmappings in c# code

I would like to programatically define in C# the fieldmappings between a JSON object and the Azure Search index. I have found the explanation on how to do this In Javascript here https://azure.microsoft.com/en-us/documentation/articles/search-howto-index-json-blobs/

 "fieldMappings" : [ 
{ "sourceFieldName" : "/article/text", "targetFieldName" : "text" },
{ "sourceFieldName" : "/article/datePublished", "targetFieldName" : "date" },
{ "sourceFieldName" : "/article/tags", "targetFieldName" : "tags" }
]

however I need the c# equivalent code. I have tried so far:

      var index = new
      {
           name = "testindex",
           fields = new []
           { 
              //...
           },
           fieldMappings = new[]
           {
              new { sourceFieldName = "/status/text", targetFieldName = "text"}
           }
      }

but this code crashes with the exception

{"Azure Search request failed:{\"error\":{\"code\":\"\",\"message\":\"The request is invalid. Details: index : The property 'fieldMappings' does not exist on type 'Microsoft.Azure.Search.V2015_02_28.IndexDefinition'. Make sure to only use property names that are defined by the type.\\r\\n\"}}"}

Any suggestions on how I can programatically in C# code define these field mappings? IN addition, should these fieldMappings be defined inside the indexer of the index definition? Thank you!

@LaterEdit: From this article Creating Collection in Azure Search Service using Indexer I found that these mappings have to be defined on the indexer, but it is still the case that if I add them when defining the indexer the property fieldMappings does not exist.

  var indexer = new
  {
      name = "textixr",
      dataSourceName = "testsdocdb",
      schedule = new { interval = "PT5M" }, // every 5 minutes
      targetIndexName = "test",
      fieldMappings = new []
      {
          new { sourceFieldName = "/status/text", targetFieldName = "text" }
      }
  };

Upvotes: 0

Views: 850

Answers (1)

Eugene Shvets
Eugene Shvets

Reputation: 4671

You need to use api-version 2015-02-28-Preview. Field mappings are not available in 2015-02-28.

By the way, field mappings with JSON pointers (such as /status/text) can only be used when indexing JSON blobs with blob indexer. They cannot be used with the DocumentDB indexer. To project nested properties with DocumentDB, use a SQL-like query in container.query property of the datasource definition. For more info on DocumentDB indexer, please check out this article

Upvotes: 2

Related Questions