|
| 1 | +--- |
| 2 | +title: Semantic Search |
| 3 | +--- |
| 4 | +# :material-file-search: Semantic Search |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +The `BasicMemoryVectorDatabase` and `MemoryVectorDatabase<>` classes both contain `.Search` and `.SearchAsync` methods that can be used to perform semantic search on the database: |
| 9 | + |
| 10 | +=== "Sync" |
| 11 | + |
| 12 | + ```csharp |
| 13 | + var query = "some text to search"; |
| 14 | + var results = vdb.Search(query); |
| 15 | + ``` |
| 16 | + |
| 17 | +=== "Async" |
| 18 | + |
| 19 | + ```csharp |
| 20 | + var query = "some text to search"; |
| 21 | + var results = await vdb.SearchAsync(query); |
| 22 | + ``` |
| 23 | + |
| 24 | +## Metadata Filters |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +Here are a couple examples of using the `filter` parameter to perform `Metadata` filtering when performing semantic searches: |
| 29 | + |
| 30 | +=== "Sync" |
| 31 | + |
| 32 | + ```csharp |
| 33 | + var vdb = new BasicMemoryVectorDatabase(); |
| 34 | + |
| 35 | + // load text and metadata into database |
| 36 | + |
| 37 | + var query = "some text to search"; |
| 38 | + var results = vdb.Search( |
| 39 | + query, |
| 40 | + filter: (metadata) => { |
| 41 | + // perform some operation to check metadata |
| 42 | + // return true or false |
| 43 | + return metadata.Contains("B59"); |
| 44 | + } |
| 45 | + ); |
| 46 | + ``` |
| 47 | + |
| 48 | +=== "Async" |
| 49 | + |
| 50 | + ```csharp |
| 51 | + var vdb = new MemoryVectorDatabase<Person>(); |
| 52 | + |
| 53 | + // load text and metadata into database |
| 54 | + |
| 55 | + var query = "some text to search"; |
| 56 | + var results = vdb.SearchAsync( |
| 57 | + query, |
| 58 | + filter: async (metadata) => { |
| 59 | + // perform some operation to check metadata |
| 60 | + // return true or false |
| 61 | + return metadata.LastName == "Pietschmann"; |
| 62 | + } |
| 63 | + ); |
| 64 | + ``` |
| 65 | + |
| 66 | +!!! info "OpenAI and Ollama Support" |
| 67 | + |
| 68 | + This functionality works the same with both [:simple-openai: OpenAI and :simple-ollama: Ollama supported vector databases](../../embeddings/index.md) too. |
| 69 | + |
| 70 | +## Paging |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +Here are a couple examples of using the `pageCount` and `pageIndex` parameters to perform paging with the semantic search results: |
| 75 | + |
| 76 | +=== "Sync" |
| 77 | + |
| 78 | + ```csharp |
| 79 | + var vdb = new BasicMemoryVectorDatabase(); |
| 80 | + |
| 81 | + // load text and metadata into database |
| 82 | + |
| 83 | + var query = "some text to search"; |
| 84 | + var results = vdb.Search( |
| 85 | + query, |
| 86 | + pageIndex: 0, // return first page of results (default: 0) |
| 87 | + pageCount: 6 // limit length of this page of results (default: unlimited) |
| 88 | + ); |
| 89 | + ``` |
| 90 | + |
| 91 | +=== "Async" |
| 92 | + |
| 93 | + ```csharp |
| 94 | + var vdb = new MemoryVectorDatabase<Person>(); |
| 95 | + |
| 96 | + // load text and metadata into database |
| 97 | + |
| 98 | + var query = "some text to search"; |
| 99 | + var results = vdb.SearchAsync( |
| 100 | + query, |
| 101 | + pageIndex: 0, // return first page of results (default: 0) |
| 102 | + pageCount: 6 // limit length of this page of results (default: unlimited) |
| 103 | + ); |
| 104 | + ``` |
| 105 | + |
| 106 | +The `pageIndex` and `pageIndex` paramters are optional, and can be used individually or together. |
0 commit comments