Diversify vector query results with a topic graph (preview)

Note

Foundry IQ (Azure AI Search) provides direct indexing and query APIs alongside higher-level knowledge source and knowledge base APIs for managed retrieval. Use the Azure portal, REST APIs, or Azure SDKs to configure these capabilities, or connect agents to knowledge bases in the Microsoft Foundry portal.

Important

Topic graph retrieval is in public preview and is subject to the Supplemental Terms of Use for Microsoft Azure Previews. For preview terms specific to this feature, see Preview functionality.

A vector query returns the k nearest neighbors to the query vector. When many documents cover the same theme, those near matches can crowd out relevant documents about other themes. Topic graph sampling (preview) uses a topic index to spread vector query results across the themes in your collection while it keeps the most relevant results.

In this article, you link a vector field in a content index to a topic index, and then run vector queries that use the stored default or override topic graph sampling for a request. To use topic graph sampling through a knowledge base instead, see Configure topic graph retrieval.

Usage support

Azure portal Microsoft Foundry portal .NET SDK Python SDK Java SDK JavaScript SDK REST API
❌ ❌ ✔️ ✔️ ✔️ ✔️ ✔️

Prerequisites

  • A populated topic index on the same service. The Foundry IQ skills plugin prepares a topic index for your content. For the schema that the service requires, see Topic index requirements.

  • An existing, populated content index on the same service. It needs a vector field that uses the same type, dimensions, and similarity metric as the topic index. The examples use text queries, so the vector field needs a vectorizer.

  • Permission to update and query the content index. Configure keyless authentication with the Search Service Contributor role to update the index and the Search Index Data Reader role to query it. The query identity must be able to read both the content index and the topic index.

  • The latest Azure.Search.Documents preview package: dotnet add package Azure.Search.Documents --prerelease

  • For keyless authentication, the Azure.Identity package: dotnet add package Azure.Identity

How topic graph sampling works

A topic index stores one document per topic. Each document has a topic identifier and a centroid vector that represents the topic's content. When a vector query uses topic graph sampling, Foundry IQ:

  1. Retrieves an oversampled pool of candidate documents from the content index. The pool has at least the minimum diversity pool size, or twice k if that number is larger.
  2. Finds the topics in the topic index whose centroids are most similar to the query vector, and assigns each candidate to its closest topic.
  3. Selects results across topics. In each round, the topic with the most relevant remaining candidate contributes that candidate. Each other topic contributes its best remaining candidate if that candidate's relevance is within the diversity factor of the leading topic's next candidate. The diversity factor is a relative drop-off from 0 to 1. A higher factor admits less relevant candidates from more topics.

You choose one of three sampling kinds:

Kind Behavior
balanced Interleaves topic-sampled results with the most similar results. Uses a fixed diversity factor of 0.2. Use it as a starting point to add diversity while you keep the closest matches.
diverse Returns the topic-sampled results. Set diversityFactor to control how far results can spread across topics.
none Turns off topic graph sampling. The query returns the nearest neighbors in similarity order.

Sampled results keep their original @search.score values, and the response has the same shape as other vector query responses. Because sampling reorders results across topics, the results aren't necessarily sorted by score.

Topic index requirements

The service checks the topic index when you link it to a content index. The topic index must meet these requirements:

Requirement Details
Location An existing index on the same service. It can't be the content index.
subject_id field A top-level string field that holds the topic identifier.
object_vector field A top-level vector field that holds the topic centroid. It can't set stored to false. It must match the content index vector field's type, dimensions, and similarity metric.
Permissions Neither the topic index nor the content index can use document-level access control.

Add a topic graph profile to the content index and assign it to the vector field. The profile names the topic index and the default sampling kind that applies to vector queries on that field. Create the index first, then add the topic graph profile in a separate update. A create request that includes a profile fails.

The examples in this article use a content index with an id key field, a searchable content field, and an embedding vector field.

using Azure.Identity;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;

var endpoint = new Uri("<search-endpoint>");
var credential = new DefaultAzureCredential();
var indexName = "<content-index-name>";
var topicIndexName = "<topic-index-name>";

var indexClient = new SearchIndexClient(endpoint, credential);
SearchIndex index = await indexClient.GetIndexAsync(indexName);
index.VectorSearch.TopicGraphProfiles.Add(
    new VectorSearchTopicGraphProfile(
        "content-topics", topicIndexName, VectorSearchTopicGraphKind.Balanced));
index.Fields.Single(field => field.Name == "embedding").TopicGraphProfile =
    "content-topics";
await indexClient.CreateOrUpdateIndexAsync(index);

Reference: SearchIndexClient

from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import VectorSearchTopicGraphProfile
from azure.search.documents.models import VectorSearchTopicGraphKind

endpoint = "<search-endpoint>"
credential = DefaultAzureCredential()
index_name = "<content-index-name>"
topic_index_name = "<topic-index-name>"

index_client = SearchIndexClient(endpoint, credential)
index = index_client.get_index(index_name)
index.vector_search.topic_graph_profiles = [
    VectorSearchTopicGraphProfile(
        name="content-topics",
        index_name=topic_index_name,
        default_kind=VectorSearchTopicGraphKind.BALANCED,
    )
]
vector_field = next(f for f in index.fields if f.name == "embedding")
vector_field.topic_graph_profile = "content-topics"
index_client.create_or_update_index(index)

Reference: SearchIndexClient

A PUT request replaces the whole index definition. Get your index's current definition with a GET request on the same URL, add the topicGraphProfile property to the vector field and the topicGraphProfiles array to vectorSearch, and send the complete definition. The following example shows the complete definition of an index whose embedding field uses a text-embedding-3-small vectorizer.

@api-version = 2026-11-01-preview
@index-url = {{search-endpoint}}/indexes/{{content-index-name}}

PUT {{index-url}}?api-version={{api-version}}
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
  "name": "{{content-index-name}}",
  "fields": [
    {"name": "id", "type": "Edm.String", "key": true},
    {"name": "content", "type": "Edm.String", "searchable": true},
    {
      "name": "embedding",
      "type": "Collection(Edm.Single)",
      "searchable": true,
      "dimensions": 1536,
      "vectorSearchProfile": "vector-profile",
      "topicGraphProfile": "content-topics"
    }
  ],
  "vectorSearch": {
    "algorithms": [
      {"name": "hnsw", "kind": "hnsw", "hnswParameters": {"metric": "cosine"}}
    ],
    "vectorizers": [
      {
        "name": "embedding-vectorizer",
        "kind": "azureOpenAI",
        "azureOpenAIParameters": {
          "resourceUri": "{{aoai-endpoint}}",
          "deploymentId": "text-embedding-3-small",
          "modelName": "text-embedding-3-small"
        }
      }
    ],
    "profiles": [
      {
        "name": "vector-profile",
        "algorithm": "hnsw",
        "vectorizer": "embedding-vectorizer"
      }
    ],
    "topicGraphProfiles": [
      {
        "name": "content-topics",
        "indexName": "{{topic-index-name}}",
        "defaultKind": "balanced"
      }
    ]
  }
}

Reference: Indexes - Create Or Update

The request returns 204 No Content.

The request succeeds when the topic index meets the requirements. To confirm the link, get the index definition and check that the vector field's topic graph profile is content-topics.

Query with the profile default

When a vector query targets a field that has a topic graph profile, the profile's default sampling kind applies automatically. You don't need to change existing vector queries.

using Azure.Identity;
using Azure.Search.Documents;
using Azure.Search.Documents.Models;

var endpoint = new Uri("<search-endpoint>");
var credential = new DefaultAzureCredential();
var indexName = "<content-index-name>";

var searchClient = new SearchClient(endpoint, indexName, credential);
var options = new SearchOptions
{
    VectorSearch = new VectorSearchOptions
    {
        Queries =
        {
            new VectorizableTextQuery(
                "What do employees say about the hybrid work program?")
            {
                KNearestNeighborsCount = 10,
                Fields = { "embedding" }
            }
        }
    },
    Select = { "id", "content" },
    Size = 10
};
SearchResults<SearchDocument> results =
    await searchClient.SearchAsync<SearchDocument>(options);

Reference: SearchClient

from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from azure.search.documents.models import VectorizableTextQuery

endpoint = "<search-endpoint>"
credential = DefaultAzureCredential()
index_name = "<content-index-name>"

search_client = SearchClient(endpoint, index_name, credential)
vector_query = VectorizableTextQuery(
    text="What do employees say about the hybrid work program?",
    k_nearest_neighbors=10,
    fields="embedding",
)
results = search_client.search(
    vector_queries=[vector_query], select=["id", "content"], top=10
)

Reference: SearchClient

@api-version = 2026-11-01-preview
@index-url = {{search-endpoint}}/indexes/{{content-index-name}}

POST {{index-url}}/docs/search?api-version={{api-version}}
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
  "select": "id, content",
  "top": 10,
  "vectorQueries": [
    {
      "kind": "text",
      "text": "What do employees say about the hybrid work program?",
      "fields": "embedding",
      "k": 10
    }
  ]
}

Reference: Documents - Search Post

With the balanced default, the results include documents from more than one topic when the topics are relevant to the query. The response doesn't show topic assignments. To see the effect, compare the returned documents with the results of the query in Turn off sampling for a query. Sampled results can include different documents, and their @search.score values aren't necessarily in descending order.

Override sampling for a query

To change sampling for one request, set the topic graph option on the vector query. An explicit setting replaces the profile default. The following example uses diverse with a diversity factor of 0.5 to spread results across more topics.

using Azure.Identity;
using Azure.Search.Documents;
using Azure.Search.Documents.Models;

var endpoint = new Uri("<search-endpoint>");
var credential = new DefaultAzureCredential();
var indexName = "<content-index-name>";

var searchClient = new SearchClient(endpoint, indexName, credential);
var options = new SearchOptions
{
    VectorSearch = new VectorSearchOptions
    {
        Queries =
        {
            new VectorizableTextQuery(
                "What do employees say about the hybrid work program?")
            {
                KNearestNeighborsCount = 10,
                Fields = { "embedding" },
                TopicGraph = new DiverseVectorQueryTopicGraph { DiversityFactor = 0.5f }
            }
        }
    },
    Select = { "id", "content" },
    Size = 10
};
SearchResults<SearchDocument> results =
    await searchClient.SearchAsync<SearchDocument>(options);

Reference: VectorizableTextQuery

To use the balanced kind for a request instead, set TopicGraph to new BalancedVectorQueryTopicGraph().

from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from azure.search.documents.models import (
    DiverseVectorQueryTopicGraph,
    VectorizableTextQuery,
)

endpoint = "<search-endpoint>"
credential = DefaultAzureCredential()
index_name = "<content-index-name>"

search_client = SearchClient(endpoint, index_name, credential)
vector_query = VectorizableTextQuery(
    text="What do employees say about the hybrid work program?",
    k_nearest_neighbors=10,
    fields="embedding",
    topic_graph=DiverseVectorQueryTopicGraph(diversity_factor=0.5),
)
results = search_client.search(
    vector_queries=[vector_query], select=["id", "content"], top=10
)

Reference: VectorizableTextQuery

To use the balanced kind for a request instead, set topic_graph to BalancedVectorQueryTopicGraph().

@api-version = 2026-11-01-preview
@index-url = {{search-endpoint}}/indexes/{{content-index-name}}

POST {{index-url}}/docs/search?api-version={{api-version}}
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
  "select": "id, content",
  "top": 10,
  "vectorQueries": [
    {
      "kind": "text",
      "text": "What do employees say about the hybrid work program?",
      "fields": "embedding",
      "k": 10,
      "topicGraph": {"kind": "diverse", "diversityFactor": 0.5}
    }
  ]
}

Reference: Documents - Search Post

To use the balanced kind for a request instead, set topicGraph to {"kind": "balanced"}.

When several topics are relevant, the results alternate between topics instead of mixing in the most similar results.

Turn off sampling for a query

To compare results or return pure similarity results, set the kind to none.

using Azure.Identity;
using Azure.Search.Documents;
using Azure.Search.Documents.Models;

var endpoint = new Uri("<search-endpoint>");
var credential = new DefaultAzureCredential();
var indexName = "<content-index-name>";

var searchClient = new SearchClient(endpoint, indexName, credential);
var options = new SearchOptions
{
    VectorSearch = new VectorSearchOptions
    {
        Queries =
        {
            new VectorizableTextQuery(
                "What do employees say about the hybrid work program?")
            {
                KNearestNeighborsCount = 10,
                Fields = { "embedding" },
                TopicGraph = new NoneVectorQueryTopicGraph()
            }
        }
    },
    Select = { "id", "content" },
    Size = 10
};
SearchResults<SearchDocument> results =
    await searchClient.SearchAsync<SearchDocument>(options);

Reference: VectorizableTextQuery

from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from azure.search.documents.models import (
    NoneVectorQueryTopicGraph,
    VectorizableTextQuery,
)

endpoint = "<search-endpoint>"
credential = DefaultAzureCredential()
index_name = "<content-index-name>"

search_client = SearchClient(endpoint, index_name, credential)
vector_query = VectorizableTextQuery(
    text="What do employees say about the hybrid work program?",
    k_nearest_neighbors=10,
    fields="embedding",
    topic_graph=NoneVectorQueryTopicGraph(),
)
results = search_client.search(
    vector_queries=[vector_query], select=["id", "content"], top=10
)

Reference: VectorizableTextQuery

@api-version = 2026-11-01-preview
@index-url = {{search-endpoint}}/indexes/{{content-index-name}}

POST {{index-url}}/docs/search?api-version={{api-version}}
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
  "select": "id, content",
  "top": 10,
  "vectorQueries": [
    {
      "kind": "text",
      "text": "What do employees say about the hybrid work program?",
      "fields": "embedding",
      "k": 10,
      "topicGraph": {"kind": "none"}
    }
  ]
}

Reference: Documents - Search Post

The results are the nearest neighbors in similarity order, the same as a vector query on a field without a topic graph profile.

Tune topic graph sampling settings

Set defaults in the profile, and override them per query.

The profile settings are the DefaultKind, DefaultDiversityFactor, and DefaultMinDiversityPoolSize properties of VectorSearchTopicGraphProfile. The query settings are properties of the class that you assign to TopicGraph.

The profile settings are the default_kind, default_diversity_factor, and default_min_diversity_pool_size parameters of VectorSearchTopicGraphProfile. The query settings are parameters of the class that you assign to topic_graph.

The profile settings are properties of the topicGraphProfiles entry. The query settings are properties of the vector query's topicGraph object.

Profile setting Query setting Values and defaults
defaultKind kind Required: none, diverse, or balanced.
defaultDiversityFactor diversityFactor Only for diverse. Inclusive range: 0 through 1. If you omit it in the query, the profile's value applies. If neither sets it, the default is 0.2.
defaultMinDiversityPoolSize minDiversityPoolSize Only for diverse or balanced. Inclusive range: 250 through 10000. If you omit it in the query, the profile's value applies. If neither sets it, the default is 1000.

Start with balanced. If results still concentrate on too few topics, use diverse and increase the diversity factor. If results include too many weakly relevant documents, lower the factor. A factor of 0 returns results in similarity order.

Limitations

  • An index can have one topic graph profile.
  • Topic graph profiles and the topicGraph query parameter require the 2026-11-01-preview REST API or a preview client library that supports it. Queries that use the 2026-10-01 API version ignore the profile and return the nearest neighbors in similarity order.
  • You can't delete a topic index while a topic graph profile refers to it. Remove the profile from the content index, or delete the content index, first. To remove the profile, update the content index without the field's topicGraphProfile property and without the topicGraphProfiles entry.

Troubleshooting

Error message Action
The index must be created before topicGraph can be configured. Create the content index without the profile, and then update it to add the profile.
Topic graph index 'name' does not exist or is not available for use. Check the profile's indexName, and make sure that the topic index exists on the same service.
Topic graph index 'name' must define a top-level string field named 'subject_id'. Use a topic index that meets the requirements. Errors about the object_vector field or document-level permissions also mean that the topic index doesn't meet the requirements.
Only one topic graph profile can be configured for an index. Keep one profile in vectorSearch.topicGraphProfiles.
None of the vector fields have a topicGraphProfile configured. Query a vector field that has a topicGraphProfile, or remove topicGraph from the query.
The vector query's 'topicGraph.diversityFactor' parameter can only be used when 'topicGraph.kind' is set to 'diverse'. Remove diversityFactor, or set kind to diverse.
Cannot delete index 'name' because one or more indexes reference it in topic graph profiles. Please remove or update those references... Remove the profile from the content index, or delete the content index, before you delete the topic index.