Skip to main content

Microsoft Azure Cognitive Search Client Library for Python

Project description

Azure Cognitive Search client library for Python

Azure Cognitive Search is a fully managed cloud search service that provides a rich search experience to custom applications.

Source code | Package (PyPI) | API reference documentation | Product documentation | Samples

Getting started

Install the package

Install the Azure Cognitive Search client library for Python with pip:

pip install azure-search-documents --pre

Prerequisites

If you need to create the resource, you can use the Azure Portal or Azure CLI.

If you use the Azure CLI, replace <your-resource-group-name> and <your-resource-name> with your own unique names:

az search service create --resource-group <your-resource-group-name> --name <your-resource-name> --sku S

The above creates a resource with the "Standard" pricing tier. See choosing a pricing tier for more information.

Authenticate the client

In order to interact with the Cognitive Search service you'll need to create an instance of the Search Client class. To make this possible you will need an api-key of the Cognitive Search service.

The SDK provides two clients.

  1. SearchClient for all document operations.
  2. SearchServiceClient for all CRUD operations on service resources.

Create a SearchClient

To create a SearchClient, you will need an existing index name as well as the values of the Cognitive Search Service service endpoint and api key. Note that you will need an admin key to index documents (query keys only work for queries).

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient

credential = AzureKeyCredential("<api key>")

client = SearchClient(endpoint="<service endpoint>",
                      index_name="<index name>",
                      credential=credential)

Create a SearchServiceClient

Once you have the values of the Cognitive Search Service service endpoint and api key you can create the Search Service client:

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchServiceClient

credential = AzureKeyCredential("<api key>")

client = SearchServiceClient(endpoint="<service endpoint>"
                             credential=credential)

Send your first search request

You can use the SearchClient you created in the first section above to make a basic search request:

results = client.search(query="spa")

print("Hotels containing 'spa' in the name (or other fields):")
for result in results:
    print("    Name: {} (rating {})".format(result["HotelName"], result["Rating"]))

Key concepts

Azure Cognitive Search has the concepts of search services and indexes and documents, where a search service contains one or more indexes that provides persistent storage of searchable data, and data is loaded in the form of JSON documents. Data can be pushed to an index from an external data source, but if you use an indexer, it's possible to crawl a data source to extract and load data into an index.

There are several types of operations that can be executed against the service:

  • Index management operations. Create, delete, update, or configure a search index.
  • Document operations. Add, update, or delete documents in the index, query the index, or look up specific documents by ID.
  • Indexer operations. Automate aspects of an indexing operation by configuring a data source and an indexer that you can schedule or run on demand. This feature is supported for a limited number of data source types.
  • Skillset operations. Part of a cognitive search workload, a skillset defines a series of a series of enrichment processing steps. A skillset is consumed by an indexer.
  • Synonym map operations. A synonym map is a service-level resource that contains user-defined synonyms. This resource is maintained independently from search indexes. Once uploaded, you can point any searchable field to the synonym map (one per field).

Examples

Create an index

Create a new index

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchServiceClient, CorsOptions, Index, ScoringProfile
client = SearchServiceClient("<service endpoint>", AzureKeyCredential("<api key>")).get_indexes_client()
name = "hotels"
fields = [
    {
        "name": "hotelId",
        "type": "Edm.String",
        "key": True,
        "searchable": False
    },
    {
        "name": "baseRate",
        "type": "Edm.Double"
    }
]
cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)
scoring_profiles = []

index = Index(
    name=name,
    fields=fields,
    scoring_profiles=scoring_profiles,
    cors_options=cors_options)

result = client.create_index(index)

Upload documents to an index

Add documents (or update existing ones), e.g add a new document for a new hotel:

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))

DOCUMENT = {
    'Category': 'Hotel',
    'HotelId': '1000',
    'Rating': 4.0,
    'Rooms': [],
    'HotelName': 'Azure Inn',
}

result = client.upload_documents(documents=[DOCUMENT])

print("Upload of new document succeeded: {}".format(result[0].succeeded))

Retrieve a specific document from an index

Get a specific document from the index, e.f. obtain the document for hotel "23":

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))

result = client.get_document(key="23")

print("Details for hotel '23' are:")
print("        Name: {}".format(result["HotelName"]))
print("      Rating: {}".format(result["Rating"]))
print("    Category: {}".format(result["Category"]))

Perform a simple text search on documents

Search the entire index or documents matching a simple search text, e.g. find hotels with the text "spa":

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))

results = client.search(query="spa")

print("Hotels containing 'spa' in the name (or other fields):")
for result in results:
    print("    Name: {} (rating {})".format(result["HotelName"], result["Rating"]))

Get search suggestions

Get search suggestions for related terms, e.g. find search suggestions for the term "coffee":

from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient, SuggestQuery
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"))

query = SuggestQuery(search_text="coffee", suggester_name="sg")

results = client.suggest(query=query)

print("Search suggestions for 'coffee'")
for result in results:
    hotel = client.get_document(key=result["HotelId"])
    print("    Text: {} for Hotel: {}".format(repr(result["text"]), hotel["HotelName"]))

Troubleshooting

General

The Azure Cognitive Search client will raise exceptions defined in Azure Core.

Logging

This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.

Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the logging_enable keyword argument:

import sys
import logging
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient

# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

# This client will log detailed information about its HTTP sessions, at DEBUG level
client = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"), logging_enable=True)

Similarly, logging_enable can enable detailed logging for a single operation, even when it isn't enabled for the client:

result =  client.search(query="spa", logging_enable=True)

Next steps

Additional documentation

For more extensive documentation on Cognitive Search, see the Azure Cognitive Search documentation on docs.microsoft.com.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Related projects

Impressions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-search-documents-1.0.0b3.zip (263.0 kB view details)

Uploaded Source

Built Distribution

azure_search_documents-1.0.0b3-py2.py3-none-any.whl (197.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file azure-search-documents-1.0.0b3.zip.

File metadata

  • Download URL: azure-search-documents-1.0.0b3.zip
  • Upload date:
  • Size: 263.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for azure-search-documents-1.0.0b3.zip
Algorithm Hash digest
SHA256 3329aef39f35b10a3a0fa9c6c70b69e182dad91a3997b7c561477fb6726c3bf1
MD5 2dcd0b0ac7b724751d994d643f491e5e
BLAKE2b-256 2e99a7554527e70582cb9a5db94bbb57ebfdac62f1f86e33714eee154215b236

See more details on using hashes here.

File details

Details for the file azure_search_documents-1.0.0b3-py2.py3-none-any.whl.

File metadata

  • Download URL: azure_search_documents-1.0.0b3-py2.py3-none-any.whl
  • Upload date:
  • Size: 197.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for azure_search_documents-1.0.0b3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 86539080d4fd5941a647dd78ea0ca3b0ff57e77e6c42e1d853eed68fb8cb52e8
MD5 4ca08b42456f9a5a58537afb2494dca2
BLAKE2b-256 54084dec43496b51f948d04994940aa7b0050ed09611a412563c95e077ad1ae6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page