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.
- Python 2.7, or 3.5 or later is required to use this package.
- Bing for Commerce Account.
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 add imports for the required implementation as part of the sdk, besides any other dependencies needed.
from microsoft.bing.commerce.ingestion import BingCommerceIngestion
import microsoft.bing.commerce.ingestion.models
from microsoft.bing.commerce.search import BingCommerceSearch
import microsoft.bing.commerce.search.models
import microsoft.bing.commerce.search.operations
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.
def create_ingestion_client():
creds = BasicTokenAuthentication({ 'access_token': access_token })
ingest_client = BingCommerceIngestion(creds)
return ingest_client
def create_search_client():
creds = BasicTokenAuthentication({ 'access_token': access_token })
search_client = BingCommerceSearch(creds)
return search_client
You can create and manage you index using the Bing for Commerce portal. However, you could also use the SDK to manage your indexes.
def ensure_index(ingestion_client, tenant_id, index_name):
id_field = IndexField(
name = 'ProductId',
type = IndexFieldType.product_id, # Exactly one Product Id field is required while creating an index.
filterable = True,
retrievable = True)
title_field = IndexField(
name = 'ProductTitle',
type = IndexFieldType.title,
filterable = True,
retrievable = True)
desc_field = IndexField(
name = 'ProductDescription',
type = IndexFieldType.description,
filterable = True,
retrievable = True)
new_index = Index(
name = index_name,
description = 'my sample index',
fields = [id_field, title_field, desc_field])
create_response = ingestion_client.create_index(tenant_id, body=new_index)
return create_response.indexes[0]
all_indexes = ingestion_client.get_all_indexes(tenant_id);
var myIndex = ingestion_client.get_index(tenant_i_d, index_id);
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.
def push_data(ingestion_client, tenant_id, index_id, content):
push_response = ingestion_client.push_data_update(content, tenant_id, index_id)
return push_response.update_id
def push_data_status(ingestion_client, tenant_id, index_id, push_data_update_id):
push_response = ingestion_client.push_data_status(tenant_id, index_id, push_data_update_id)
# returns the overall status for the push call.
#
# You can get the status for each record being updated by accessing status.records list.
return push_response.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).
def simple_search(search_client, tenant_id, index_id, query_term):
response = search_client.search.get(query_term, tenant_id, index_id)
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.
def advanced_search(search_client, tenant_id, index_id):
# Prepare the Search request.
request = CommerceSearchPostRequest(
query = RequestQuery(
filter = StringSetCondition(
values = [ '1', '2'],
field = 'ProductId'
)
),
items = RequestItems(
select = [ '*' ]
),
aggregations = [ RequestDiscoverFacets( name = "discovered facets" ) ]
)
# Send the search request
response = search_client.search.post(request, tenant_id, index_id)
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.
my_script = get_my_transformation_script()
create_script_response = ingestion_client.create_or_update_transformation_config(my_script, tenant_id, index_id)
# Note that the getTransformationConfig will throw a 400 Bad Request if your index doesn't have a transformation config.
read_script_response = ingestion_client.get_transformation_config(tenant_id, index_id)
myScript = read_script_response.value
delete_script_response = ingestion_client.delete_transformation_config(tenant_id, index_id);
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.
def upload_transformation_tryout(ingestion_client, script):
create_script_response = ingestion_client.upload_try_out_config(script)
return create_script_response.try_out_id
def execute_transformation_tryout(ingestion_client, data, try_out_id):
execute_response = ingestion_client.execute_try_out_config(data, try_out_id)
return execute_response.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.