Azure / Azure/azure-search-vector-samples
BUG: KNearestNeighbors Not Working
- Dominant language
- Jupyter Notebook
- Stars
- 910
- Forks
- 377
- PR merge metrics
- No merged PRs in 30d
Description
Using the .NET example code it appears that the KNearestNeighbors is not returning more than a single result.
Example Code:
```c#
internal static async Task SingleVectorSearch(SearchClient searchClient, OpenAIClient openAIClient, string query, int k = 3, int nearestNeighbors = 3)
{
// Generate the embedding for the query
var queryEmbeddings = await SemanticFunctions.GenerateEmbeddings(query, openAIClient);
// Perform the vector similarity search
var searchOptions = new SearchOptions
{
Vectors = { new() { Value = queryEmbeddings.ToArray(), KNearestNeighborsCount = nearestNeighbors, Fields = { "contentVector" } } },
Size = k,
Select = { "id", "title", "content", "category", "url" },
};
SearchResults response = await searchClient.SearchAsync(null, searchOptions);
int count = 0;
await foreach (SearchResult result in response.GetResultsAsync())
{
count++;
// for (int i = 0; i < nearestNeighbors; i++)
// {
Console.WriteLine($"Id: {result.Document["id"]}");
Console.WriteLine($"Title: {result.Document["title"]}");
Console.WriteLine($"Score: {result.Score}\n");
Console.WriteLine($"Content: {result.Document["content"]}");
Console.WriteLine($"Category: {result.Document["category"]}\n\n");
// }
}
Console.WriteLine($"Total Results: {count}");
}
```
The index# I am using is a bit non-typical and I wonder if that may be the cause. Here is an example of a result:

I have confirmed that that is indeed a single chunk of text in this particular record. I would expect the KNearestNeighbors to add n+ record "content" field values before and after into a single string return for this record. Perhaps that is not the intent or the search options are not setup correctly. Please advise, thank you!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the .NET example's SingleVectorSearch entry point and inspect SearchOptions, especially Vectors, KNearestNeighborsCount, and Size. Compare the returned SearchResult documents with the expectation that neighboring content chunks are combined; done means determining whether the observed single result is expected and documenting the required behavior or configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- search
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100