Skip to content

Instantly share code, notes, and snippets.

@dennisseah
Created November 24, 2022 05:55
Show Gist options
  • Save dennisseah/9706f41449d06b3bd4743f05fdf6c326 to your computer and use it in GitHub Desktop.
Save dennisseah/9706f41449d06b3bd4743f05fdf6c326 to your computer and use it in GitHub Desktop.
Azure Search Python SDK
# azure-search-documents==11.3.0
# faker=15.3.3
import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
CorsOptions,
ComplexField,
SearchIndex,
ScoringProfile,
SearchFieldDataType,
SimpleField,
SearchableField
)
endpoint = os.environ["AZURE_SEARCH_ENDPOINT"]
key = os.environ["AZURE_SEARCH_API_KEY"]
# index name - for organizing the search fields, indices and the documents.
index_name = "profiles"
# Python client for managing index in Azure Search
search_index_client = SearchIndexClient(endpoint, AzureKeyCredential(key))
# Python client for performing search
search_client = SearchClient(endpoint, index_name, AzureKeyCredential(key))
# Search Fields
fields = [
SimpleField(name="username",
type=SearchFieldDataType.String, key=True),
SearchableField(name="name", type=SearchFieldDataType.String),
SearchableField(name="mail", type=SearchFieldDataType.String),
ComplexField(name="address", fields=[
SearchableField(name="address1", type=SearchFieldDataType.String),
SearchableField(name="address2", type=SearchFieldDataType.String),
])
]
# Search index object
index = SearchIndex(
name=index_name,
fields=fields,
scoring_profiles=[],
cors_options=CorsOptions(allowed_origins=["*"], max_age_in_seconds=60))
def add_documents(count: int) -> list[str]:
"""Add documents to the search index
Args:
count (int): number of document to add. Essentially, using Faker to
generate the number of profiles.
Returns:
list[str]: list of user names.
"""
import faker
faker_inst = faker.Faker()
def create_profile(profile):
del profile["sex"]
del profile["birthdate"]
addresses = profile["address"].split("\n")
profile["address"] = {
"address1": addresses[0],
"address2": addresses[1],
}
return profile
profiles = [create_profile(faker_inst.simple_profile())
for _ in range(count)]
search_client.upload_documents(documents=profiles)
return [p["name"] for p in profiles]
if __name__ == "__main__":
# 0. remove index if it exists
# search_index_client.delete_index(index)
# 1. create index and data
search_index_client.create_index(index)
names = add_documents(100)
print(names) # for search reference
# 2. search
# enter a search text, can use the names as reference
# results = list(search_client.search("some text"))
# print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment