Azure search AI - connecting to the service and search our Index

Besides hooking up your search service to an AI, you might want to connect to your search service to retrieve data you already indexed. We will be creating index for our json data stored in a storage account via portal - it will automatically create index and indexer for us. 

Besides JSON, you can index other data such as SQL database, mark down and even Apache Spark.

Index allow our document to be 'structure and define for faster search'.

Indexer perform actual indexing to our data and we can specify or schedule when we wants it.

Pre-requisite 

AI search 

Storage acccount with hierarchical name enabled

python code 

First we click on "Import data" and select "Data Lake"



And this is what our storage account looks like :-



And then let's configure our json:-


click "next" :-

and then accept the default :-



And then you can configure the schedule for our index to run. Use the default and configure it to run "Once".


And finally provide a name for your index. Please give it to "testdemo".



And you can see our indexer being run :-



Now we are going to use our code to retrieve it. 

After we created our index, we can use python code to perform query on it. You can use the following code to do so:-




from azure.identity import DefaultAzureCredential
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.search.documents.indexes.models import (
    ComplexField,
    SimpleField,
    SearchFieldDataType,
    SearchableField,
    SearchIndex
)

credential = DefaultAzureCredential()
index_name: str = "testdemo"
search_endpoint: str = "https://your-service-service.search.windows.net"

index_client = SearchIndexClient(
    endpoint=search_endpoint, credential=credential)


indexes = index_client.list_index_names()
print(f"Available indexes: {list(indexes)}")

# Check if your specific index exists
try:
    index = index_client.get_index("testdemo")
    print(f"Index found with {index.fields} fields")
except:
    print("Index 'testdemo' not found")


search_endpoint = "https://mysearchservicedev1.search.windows.net"
api_key = "your-api-key-here"  # Replace with your actual API key

# Setting up search client

search_client = SearchClient(
    endpoint=search_endpoint,
credential=AzureKeyCredential(api_key),
index_name="testdemo")

# Try without include_total_count first

try:
    results = search_client.search(
        query_type='simple',
        search_text="Stay-Kay City Hotel",
        select=['id', 'title', 'content']
    )
   
    print("Iterating through results:")
    result_count = 0

    for result in results:
        result_count += 1
        print(f"Document {result_count}: {result['id']}")
   
    print(f"Total results found: {result_count}")
   
except Exception as e:
    print(f"Error during search: {e}")
    import traceback
    traceback.print_exc()











Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20