In the earlier article, we saw how to create a Search Index Programmatically. Now, as a continuation, let us see how to create an Indexer.
Creating an Indexer involves all the three prerequisites.
1. Skillset
2. DataSource – Refer Here
3. Index – Refer Here
Among the above three, we have seen how to create a data source and index in the earlier articles. But the skillset is not straight forward. The easiest approach which I would take up is, do the reverse engineering. By referring the earlier article, how to import data and create an indexer through the console and export the Indexer JSON and then by using that JSON, we can create our Indexer.
Because, the SkillSet is going to be an one time activity. We can create the SkillSet Manually.
The code to export the Indexer is using the REST API call.
namespace CallRestAPI.Console
{
using RestSharp;
class Program
{
static void Main(string[] args)
{
string str = System.DateTime.Today.AddDays(-1).ToString("ddMMM").ToLower();
// Create a RestClient
var client = new RestClient("https://SEARCHSERVICENAME.search.windows.net/indexers/INDEXERNAME?api-version=2017-11-11-Preview");
// Define the HTTP Method
var request = new RestRequest(Method.GET);
//Add the headers
request.AddHeader("api-key", "APPLICATIONKEY");
request.AddHeader("Content-Type", "application/json");
//Add the Body Parameter
request.AddParameter("undefined", "BODY", ParameterType.RequestBody);
//Execute the Call
IRestResponse response = client.Execute(request);
}
}
}
By updating the SearchServiceName, IndexerName and the Application Key, the response we will get as a JSON object.
The structure of the JSON object is referring to the Indexer which we created manually.
{
"@odata.context": "https://SEARCHSERVICE.search.windows.net/$metadata#indexers/$entity",
"@odata.etag": ""0x8D63405849585AF"",
"name": "INDEXERNAME",
"description": "",
"dataSourceName": "DATASOURCE",
"skillsetName": "SKILLSETNAME",
"targetIndexName": "INDEXNAME",
"schedule": {
"interval": "PT1H",
"startTime": "0001-01-01T00:00:00Z"
},
"parameters": {
"batchSize": null,
"maxFailedItems": 500,
"maxFailedItemsPerBatch": 500,
"base64EncodeKeys": false,
"configuration": {
"dataToExtract": "contentAndMetadata",
"imageAction": "generateNormalizedImages"
}
},
"fieldMappings": [
{
"sourceFieldName": "metadata_storage_path",
"targetFieldName": "metadata_storage_path",
"mappingFunction": {
"name": "base64Encode",
"parameters": null
}
}
],
"outputFieldMappings": [
{
"sourceFieldName": "/document/merged_content",
"targetFieldName": "merged_content",
"mappingFunction": null
},
{
"sourceFieldName": "/document/normalized_images/*/imageTags/*/name",
"targetFieldName": "imageTags",
"mappingFunction": null
},
{
"sourceFieldName": "/document/normalized_images/*/imageCaption",
"targetFieldName": "imageCaption",
"mappingFunction": null
}
],
"disabled": null
}
The above JSON file is a sample file. By using this JSON file, and updating the parameters like, IndexerName, DataSourceName, we can create a new indexer. The piece of code to create that is as follows.
string indexerName = “myindexer";
string indexerJsonPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), “indexer.json");
JObject o1 = JObject.Parse(System.IO.File.ReadAllText(indexerJsonPath));
o1.Root["name"] = "indexer";
o1.Root["dataSourceName"] = "datasource";
o1.Root["targetIndexName"] = "index";
var client = new RestClient(string.Format("https://{0}.search.windows.net/indexers?api-version=2017-11-11-Preview", this.ClientConnection.ClientConnectionDefinition.SearchServiceName));
var request = new RestRequest(Method.POST);
request.AddHeader("api-key", “Applicationkey”);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", o1, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
The above code is straight forward. We are reading the text from the JSON File, update the properties with appropriate name, then create the Indexer by calling the web API using RestSharp. To Use the RestSharp for the easy access to the web api, please refer here.
Happy Coding,
Sathish Nadarajan.
Leave a comment