Semantic Search¶
Once text items and their associated metadata have been added to the vector database, the database can be used for semantic search to find matching text items for a given query.
The BasicMemoryVectorDatabase and MemoryVectorDatabase<> classes both contain .Search and .SearchAsync methods that can be used to perform semantic search on the database:
| C# | |
|---|---|
1 2 | |
| C# | |
|---|---|
1 2 | |
Metadata Filters¶
The .Search and .SearchAsync methods also include the ability to pre-filter the search results based on a boolean evaluation of the Metadata for the text item. This check is run before the vector similarity search is performed, and can help increase search performance on large datasets.
Here are a couple examples of using the filter parameter to perform Metadata filtering when performing semantic searches:
| C# | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
| C# | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
OpenAI and Ollama Support
This functionality works the same with both OpenAI and Ollama supported vector databases too.
Paging¶
The .Search and .SearchAsync methods also include the ability to perform paging on the text items returned from the semantic search. This is performed after the similarity search and the filter has been applied to the search results. This is done using the optional pageCount and pageIndex paramters.
Here are a couple examples of using the pageCount and pageIndex parameters to perform paging with the semantic search results:
| C# | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| C# | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
The pageIndex and pageIndex paramters are optional, and can be used individually or together.