How to create a Vector Profile in my Azure Search Service Index

Ho, Keith D 100 Reputation points
2024-08-21T21:53:39.96+00:00

Currently, I am trying to create an Azure Search Service Index with a Vector Profile and a Semantic Configuration. I am able to successfully create the Index with a Semantic Configuration, however, there is no Vector Profile in the index. I am using

azure-search-documents==11.4.0a20230509004

Here is my code below:

from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
    PrioritizedFields,
    SearchIndex,
    SearchField,
    SearchFieldDataType,
    SearchIndex,
    SemanticConfiguration,
    SemanticField,
    SearchField,
    SemanticSettings,
    VectorSearch,
    VectorSearchAlgorithmConfiguration,
)
from dotenv import load_dotenv
import os

def create_index(index_name):
    load_dotenv()

    # create azure search index
    client = SearchIndexClient(endpoint=os.getenv("endpoint"), credential=AzureKeyCredential(os.getenv("AZURE_SEARCH_ADMIN_KEY")))

    fields=[
        SearchField(name="id", type=SearchFieldDataType.String, key=True),
        SearchField(name="title", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
        SearchField(name="tag", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
        SearchField(name="metadata", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
        SearchField(name="content", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
        SearchField(name="content_vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), searchable=True, dimensions=1536, vector_search_configuration="default"),
    ]
    vector_search = VectorSearch(
        algorithm_configurations=[
            VectorSearchAlgorithmConfiguration(
                name="default",
                kind="hnsw",
                hnsw_parameters={
                    "m": 4,
                    "efConstruction": 400,
                    "efSearch": 500,
                    "metric": "cosine"
                }
            )
        ]
    )

    semantic_settings =  SemanticSettings(
        configurations=[SemanticConfiguration(
            name="semantic_configuration_name",
            prioritized_fields=PrioritizedFields(
                title_field=SemanticField(field_name="title"),
                prioritized_keywords_fields=[
                    SemanticField(field_name="tag")],
                prioritized_content_fields=[
                    SemanticField(field_name="content")]
            )
        )
        ]
    )

    # Create the search index with the semantic settings and vector search
    index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search, semantic_settings=semantic_settings)

    client.create_index(index)
Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
956 questions
{count} votes

Accepted answer
  1. ajkuma 25,786 Reputation points Microsoft Employee
    2024-08-22T04:00:19.6433333+00:00

    Following-up from the comments, to benefit the community, summarizing the answer shared by Ho, Keith D.

    Scenario:
    Working on Setting up an Azure Search Service Index that includes both a Semantic Configuration and a Vector Profile. Although you have successfully created the index with the Semantic Configuration, the Vector Profile is not appearing as expected.

    Question:
    How can you ensure that a Vector Profile is correctly created and included in your Azure Search Service Index using the provided code?

    Code:

    azure-search-documents==11.4.0a20230509004
    
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.indexes import SearchIndexClient
    from azure.search.documents.indexes.models import (
        PrioritizedFields,
        SearchIndex,
        SearchField,
        SearchFieldDataType,
        SearchIndex,
        SemanticConfiguration,
        SemanticField,
        SearchField,
        SemanticSettings,
        VectorSearch,
        VectorSearchAlgorithmConfiguration,
    )
    from dotenv import load_dotenv
    import os
    
    def create_index(index_name):
        load_dotenv()
    
        # create azure search index
        client = SearchIndexClient(endpoint=os.getenv("endpoint"), credential=AzureKeyCredential(os.getenv("AZURE_SEARCH_ADMIN_KEY")))
    
        fields=[
            SearchField(name="id", type=SearchFieldDataType.String, key=True),
            SearchField(name="title", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
            SearchField(name="tag", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
            SearchField(name="metadata", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
            SearchField(name="content", type=SearchFieldDataType.String, searchable=True, analyzer_name="standard.lucene"),
            SearchField(name="content_vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), searchable=True, dimensions=1536, vector_search_configuration="default"),
        ]
        vector_search = VectorSearch(
            algorithm_configurations=[
                VectorSearchAlgorithmConfiguration(
                    name="default",
                    kind="hnsw",
                    hnsw_parameters={
                        "m": 4,
                        "efConstruction": 400,
                        "efSearch": 500,
                        "metric": "cosine"
                    }
                )
            ]
        )
    
        semantic_settings =  SemanticSettings(
            configurations=[SemanticConfiguration(
                name="semantic_configuration_name",
                prioritized_fields=PrioritizedFields(
                    title_field=SemanticField(field_name="title"),
                    prioritized_keywords_fields=[
                        SemanticField(field_name="tag")],
                    prioritized_content_fields=[
                        SemanticField(field_name="content")]
                )
            )
            ]
        )
    
        # Create the search index with the semantic settings and vector search
        index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search, semantic_settings=semantic_settings)
    
        client.create_index(index)
    
    
    

    Answer shared by Ho, Keith D.

    Resolution:

    Following the tutorial and sample worked: Vector search in Python (Azure AI Search)

    --The notebook demonstrates how to use Azure Cognitive Search for vector search with Python. It covers the basic setup, indexing, and querying of vector data. The document demonstrates a basic vector search workflow using Azure AI Search with a sample.


    > please click Accept Answer - it will benefit the community/users to find the answer quickly.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.