This contains the generated SDKs that can help developers integrate with Bing for Commerce platform, both on the Search and Ingestion sides. The repository also contains unit tests and samples that can show you quick examples for how to use the SDKs.
For more details about the project, please refer to the Bing for Commerce main repository, or Bing for Commerce API Documentation.
Coming soon.
Bing for Commerce APIs use Bearer Tokens for authentication. You can use the Bing for Commerce Portal Documentation for help creating one.
You will need to either add using
statements for the following namespaces, or fully qualify each reference to the Bing for Commerce SDK when you're trying to use them.
using Microsoft.Bing.Commerce.Ingestion;
using Microsoft.Bing.Commerce.Ingestion.Models;
using Microsoft.Bing.Commerce.Search;
using Microsoft.Bing.Commerce.Search.Models;
Creating the SDK client object SDK are the first step you need to do in order to call the Bing for Commerce services APis. You will need first to get an access token with the proper access scope as described here.
private static BingCommerceIngestion CreateSearchClient(string accessToken)
{
return new BingCommerceIngestion(new Microsoft.Rest.TokenCredentials(accessToken));
}
private static BingCommerceSearch CreateSearchClient(string accessToken)
{
return new BingCommerceSearch(new Microsoft.Rest.TokenCredentials(accessToken));
}
You can create and manage you index using the Bing for Commerce portal. However, you could also use the SDK to manage your indexes.
private async Task<ResponseIndex> CreateIndex(BingCommerceIngestion ingestionclient, string tenantId, string indexName)
{
// Prepare the index fields
var idField = new IndexField()
{
Name = "ProductId",
Type = IndexFieldType.ProductId, // Exactly one Product Id field is required while creating an index.
Filterable = true,
Retrievable = true
};
var titleField = new IndexField()
{
Name = "ProductTitle",
Type = IndexFieldType.Title,
Filterable = true,
Searchable = true
};
var descriptionField = new IndexField()
{
Name = "ProductDescription",
Type = IndexFieldType.Description,
Filterable = true,
Searchable = true
};
// Create the request using the prepared fields
var newIndexReq = new Index()
{
Name = indexName,
Description = "My sample index",
Fields = { idField, titleField, descriptionField }
};
// Send the request, create the index
var createResponse = await ingestionClient.CreateIndexAsync(tenantId, body: newIndexReq);
return createResponse.Indexes[0];
}
var allIndexes = await ingestionClient.GetAllIndexesAsync(tenantId);
var myIndex = ingestionClient.GetIndexAsync(tenantId, indexId);
The APIs to push data to Bing for Commerce are asynchronous, where the service / SDK contains two separate APIs to serve this, one for the push itself, and another to track down the status.
The content that you will be pushing to your index catalog needs to match the index configuration that you have the index created with, and it can be in any of the following formats:
- JSon Array.
- ND-JSon (New-Line Delimited JSon).
- CSV.
- TSV.
Please note however that if you have a transformation config added to your index, the format of the pushed data needs to match that of what your transformation script is expecting.
private async Task<String> PushData(BingCommerceIngestion ingestionClient, string tenantId, string indexId, string content)
{
var pushResponse = await ingestionClient.PushDataUpdateAsync(content, tenantId, indexId);
return pushResponse.UpdateId;
}
private async Task<String> PushDataStatus(BingCommerceIngestion ingestionClient, string tenantId, string indexId, string pushDataUpdateId)
{
var pushResponse = await ingestionClient.PushDataStatusAsync(tenantId, indexId, pushDataUpdateId);
// returns the overall status for the push call.
//
// You can get the status for each record being updated by accessing status.Records list.
return pushResponse.Status;
}
You can use the Search SDK to do queries on your Bing for Commerce indexes given that you have an access token with the proper scope.
There are few cusomizations that you can still apply to the simple search query api by providing different values for different API arguments (like: market, language, field select, order configuration, pagination, facet discovery and query alteration toggle).
private async Task<ResponseItemsBase> SimpleSearch(BingCommerceSearch searchClient, string tenantId, string indexId, string queryTerm) {
var response = await searchClient.Search.GetAsync(queryTerm, tenandId, indexId);
return response.Items;
}
You can do a lot more customization (like filering, boosting, ...etc) to your advanced search query by providing a detailed search query description for how you want your results to be.
private async Task<ResponseItemsBase> AdvancedSearch(BingCommerceSearch searchClient, string tenantId, string indexId) {
// Prepare the Search request.
var request = new CommerceSearchPostRequest()
{
Query = new RequestQuery()
{
MatchAll = "Product",
Filter = new StringSetCondition()
{
Values = new List<string>() { "1", "2" },
Field = "ProductId"
}
},
Items = new RequestItems()
{
Select = new[] { "_itemId", "name" }
},
Aggregations = new List<RequestAggregationBase>()
{
new RequestDiscoverFacets()
{
Name = "discovered facets"
}
}
};
// Send the search request.
var response = await searchClient.Search.PostAsync(request, tenantId, indexId);
return response.Items;
}
You can upload a custom configuration that you might need applied to the data you push to your index automatically. Please refer to the Bing for Commerce docs for more details about how to create a valid transformation config.
string myScript = GetMyTransformationScript();
TransformationConfigResponse createScriptResponse = await ingestionClient.CreateOrUpdateTransformationConfigAsync(myScript, tenantId, indexId);
// Note that the getTransformationConfig will throw a 400 Bad Request if your index doesn't have a transformation config.
TransformationConfigResponse readScriptResponse = await ingestionClient.GetTransformationConfigAsync(tenantId, indexId);
string myScript = readScriptResponse.Value;
TransformationConfigResponse deleteScriptResponse = await ingestionClient.DeleteTransformationConfigAsync(tenantId, indexId);
Before you associate a transformation script to your index, you can use the transformation tryout apis to make sure that your index works with your data and the SDK before actually associating it to your index.
private async Task<string> UploadTransformationTryout(BingCommerceIngestion ingestionClient, string script)
{
var createScriptResponse = await ingestionClient.UploadTryOutConfigAsync(script);
return createScriptResponse.TryOutId;
}
private async Task<bool> ExecuteTransformationTryout(BingCommerceIngestion ingestionClient, string data, string tryoutId)
{
var executeResponse = await ingestionClient.ExecuteTryOutConfigAsync(data, tryoutId);
return executeResponse.Status == "Succeeded";
}
Please take a look at the sample for a quick example for how to use the SDK in order to manage your indexes, push data to your index catalog and perform search queries on your data.
For details on contributing to this repository, see the contributing guide.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.