|
| 1 | +/* |
| 2 | +Copyright 2024 The Spice.ai OSS Authors |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +using System.Text.Json; |
| 24 | +using System.Text.Json.Serialization; |
| 25 | + |
| 26 | +namespace Spice.Search; |
| 27 | + |
| 28 | +/// <summary> |
| 29 | +/// A search over datasets that have an embedding column and a loaded embedding model. |
| 30 | +/// </summary> |
| 31 | +/// <remarks> |
| 32 | +/// Only <see cref="Text"/> is required. Leave <see cref="Datasets"/> unset to search every |
| 33 | +/// dataset with an embedding column, and set <see cref="Keywords"/> to pre-filter the |
| 34 | +/// embedding column with a lexical search first, making the search hybrid. |
| 35 | +/// </remarks> |
| 36 | +public class SearchRequest |
| 37 | +{ |
| 38 | + /// <summary> |
| 39 | + /// Creates a search request. |
| 40 | + /// </summary> |
| 41 | + /// <param name="text">The query to find similar documents for</param> |
| 42 | + public SearchRequest(string text) |
| 43 | + { |
| 44 | + Text = text; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The query to find similar documents for. |
| 49 | + /// </summary> |
| 50 | + [JsonPropertyName("text")] |
| 51 | + public string Text { get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Restricts the search to these datasets. Null searches every dataset with an |
| 55 | + /// embedding column. |
| 56 | + /// </summary> |
| 57 | + [JsonPropertyName("datasets")] |
| 58 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 59 | + public IReadOnlyList<string>? Datasets { get; set; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Maximum matches to return per dataset. Null uses the runtime's default. |
| 63 | + /// </summary> |
| 64 | + [JsonPropertyName("limit")] |
| 65 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 66 | + public int? Limit { get; set; } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// An SQL predicate applied before the search, without the WHERE keyword — |
| 70 | + /// for example <c>city = 'Tokyo'</c>. |
| 71 | + /// </summary> |
| 72 | + [JsonPropertyName("where")] |
| 73 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 74 | + public string? Where { get; set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Extra dataset columns to return. A column that is part of the primary key is |
| 78 | + /// returned in <see cref="SearchMatch.PrimaryKey"/> rather than <see cref="SearchMatch.Data"/>. |
| 79 | + /// </summary> |
| 80 | + [JsonPropertyName("additional_columns")] |
| 81 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 82 | + public IReadOnlyList<string>? AdditionalColumns { get; set; } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Pre-filters the embedding column with a lexical search before the vector search |
| 86 | + /// runs, making the search hybrid. |
| 87 | + /// </summary> |
| 88 | + [JsonPropertyName("keywords")] |
| 89 | + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| 90 | + public IReadOnlyList<string>? Keywords { get; set; } |
| 91 | +} |
| 92 | + |
| 93 | +/// <summary> |
| 94 | +/// A single document matched by a search. |
| 95 | +/// </summary> |
| 96 | +/// <remarks> |
| 97 | +/// The runtime omits <see cref="Data"/>, <see cref="PrimaryKey"/> and <see cref="Metadata"/> |
| 98 | +/// when they are empty; they default to empty dictionaries so they can be read without a |
| 99 | +/// null check. |
| 100 | +/// </remarks> |
| 101 | +public class SearchMatch |
| 102 | +{ |
| 103 | + /// <summary> |
| 104 | + /// The dataset the match was found in. |
| 105 | + /// </summary> |
| 106 | + [JsonPropertyName("dataset")] |
| 107 | + public string Dataset { get; set; } = string.Empty; |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Similarity of the match to the query text. Higher is closer. |
| 111 | + /// </summary> |
| 112 | + /// <remarks>The runtime serializes this as <c>_score</c>.</remarks> |
| 113 | + [JsonPropertyName("_score")] |
| 114 | + public double Score { get; set; } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// The matched values of each searched column. |
| 118 | + /// </summary> |
| 119 | + [JsonPropertyName("matches")] |
| 120 | + public IReadOnlyDictionary<string, IReadOnlyList<JsonElement>> Matches { get; set; } |
| 121 | + = new Dictionary<string, IReadOnlyList<JsonElement>>(); |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Primary key identifying the matched row, if the dataset declares one. |
| 125 | + /// </summary> |
| 126 | + [JsonPropertyName("primary_key")] |
| 127 | + public IReadOnlyDictionary<string, JsonElement> PrimaryKey { get; set; } |
| 128 | + = new Dictionary<string, JsonElement>(); |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Columns requested via <see cref="SearchRequest.AdditionalColumns"/>. |
| 132 | + /// </summary> |
| 133 | + [JsonPropertyName("data")] |
| 134 | + public IReadOnlyDictionary<string, JsonElement> Data { get; set; } |
| 135 | + = new Dictionary<string, JsonElement>(); |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Any additional metadata the runtime attached to the match. |
| 139 | + /// </summary> |
| 140 | + [JsonPropertyName("metadata")] |
| 141 | + public IReadOnlyDictionary<string, JsonElement> Metadata { get; set; } |
| 142 | + = new Dictionary<string, JsonElement>(); |
| 143 | +} |
| 144 | + |
| 145 | +/// <summary> |
| 146 | +/// The result of a search. |
| 147 | +/// </summary> |
| 148 | +public class SearchResponse |
| 149 | +{ |
| 150 | + /// <summary> |
| 151 | + /// Matches, ordered by descending score. |
| 152 | + /// </summary> |
| 153 | + [JsonPropertyName("results")] |
| 154 | + public IReadOnlyList<SearchMatch> Results { get; set; } = new List<SearchMatch>(); |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// How long the runtime took to run the search. |
| 158 | + /// </summary> |
| 159 | + [JsonPropertyName("duration_ms")] |
| 160 | + public long DurationMs { get; set; } |
| 161 | +} |
0 commit comments