Search Database

https://{your-workspace-slug}.{region}.xata.sh/db/db_branch_name/search

This endpoint performs full text search across an entire database branch. You can filter down to particular table by using the tables parameter. The tables parameter accepts a mixed array of strings and objects. Using a string (the table name) selects the full table. Using an object allows one to specify a filter as well. The supported filters are the same as documented for the query endpoint with the following exceptions:

  • filters $contains, $startsWith, $endsWith don't work on columns of type text
  • filtering on columns of type multiple is currently unsupported

Expected Parameters

NameDescriptionInRequiredSchema
db_branch_nameThe DBBranchName matches the pattern `{db_name}:{branch_name}`. pathstring
POST
https://{your-workspace-slug}.{region}.xata.sh/db/db_branch_name/search

Run a free text search operation across the database branch.

Request Body Example
{
  "tables": [
    "users",
    {
      "table": "articles",
      "filter": {
        "author": "Abigail"
      }
    }
  ],
  "query": "after a long day"
}

Request Body Type Definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * @example {"tables":["users",{"table":"articles","filter":{"author":"Abigail"}}],"query":"after a long day"}
 */
type SearchBranch = {
    /*
     * An array with the tables in which to search. By default, all tables are included. Optionally, filters can be included that apply to each table.
     */
    tables?: (string | {
        /*
         * The name of the table.
         */
        table: string;
        filter?: FilterExpression;
        target?: TargetExpression;
        boosters?: BoosterExpression[];
    })[];
    /*
     * The query string.
     *
     * @minLength 1
     */
    query: string;
    fuzziness?: FuzzinessExpression;
    prefix?: PrefixExpression;
    highlight?: HighlightExpression;
    page?: SearchPageConfig;
};

/**
 * @minProperties 1
 */
type FilterExpression = {
    $exists?: string;
    $existsNot?: string;
    $any?: FilterList;
    $all?: FilterList;
    $none?: FilterList;
    $not?: FilterList;
} & {
    [key: string]: FilterColumn;
};

/**
 * The target expression is used to filter the search results by the target columns.
 */
type TargetExpression = (string | {
    /*
     * The name of the column.
     */
    column: string;
    /*
     * The weight of the column.
     *
     * @default 1
     * @maximum 10
     * @minimum 1
     */
    weight?: number;
})[];

/**
 * Booster Expression
 */
type BoosterExpression = {
    valueBooster?: ValueBooster;
} | {
    numericBooster?: NumericBooster;
} | {
    dateBooster?: DateBooster;
};

/**
 * Maximum [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) for the search terms. The Levenshtein
 * distance is the number of one character changes needed to make two strings equal. The default is 1, meaning that single
 * character typos per word are tollerated by search. You can set it to 0 to remove the typo tollerance or set it to 2
 * to allow two typos in a word.
 * 
 * @default 1
 * @maximum 2
 * @minimum 0
 */
type FuzzinessExpression = number;

/**
 * If the prefix type is set to "disabled" (the default), the search only matches full words. If the prefix type is set to "phrase", the search will return results that match prefixes of the search phrase.
 */
type PrefixExpression = "phrase" | "disabled";

type HighlightExpression = {
    /*
     * Set to `false` to disable highlighting. By default it is `true`.
     */
    enabled?: boolean;
    /*
     * Set to `false` to disable HTML encoding in highlight snippets. By default it is `true`.
     */
    encodeHTML?: boolean;
};

/**
 * Pagination settings for the search endpoints.
 */
type SearchPageConfig = {
    /*
     * Set page size.
     *
     * @default 25
     * @maximum 200
     */
    size?: number;
    /*
     * Use offset to skip entries. To skip pages set offset to a multiple of size.
     *
     * @default 0
     * @maximum 800
     */
    offset?: number;
};

type FilterList = FilterExpression | FilterExpression[];

type FilterColumn = FilterColumnIncludes | FilterPredicate | FilterList;

/**
 * Boost records with a particular value for a column.
 */
type ValueBooster = {
    /*
     * The column in which to look for the value.
     */
    column: string;
    /*
     * The exact value to boost.
     */
    value: string | number | boolean;
    /*
     * The factor with which to multiply the score of the record.
     */
    factor: number;
    /*
     * Only apply this booster to the records for which the provided filter matches.
     */
    ifMatchesFilter?: FilterExpression;
};

/**
 * Boost records based on the value of a numeric column.
 */
type NumericBooster = {
    /*
     * The column in which to look for the value.
     */
    column: string;
    /*
     * The factor with which to multiply the value of the column before adding it to the item score.
     */
    factor: number;
    /*
     * Modifier to be applied to the column value, before being multiplied with the factor. The possible values are:
     *   - none (default).
     *   - log: common logarithm (base 10)
     *   - log1p: add 1 then take the common logarithm. This ensures that the value is positive if the
     *     value is between 0 and 1.
     *   - ln: natural logarithm (base e)
     *   - ln1p: add 1 then take the natural logarithm. This ensures that the value is positive if the
     *     value is between 0 and 1.
     *   - square: raise the value to the power of two.
     *   - sqrt: take the square root of the value.
     *   - reciprocal: reciprocate the value (if the value is `x`, the reciprocal is `1/x`).
     */
    modifier?: "none" | "log" | "log1p" | "ln" | "ln1p" | "square" | "sqrt" | "reciprocal";
    /*
     * Only apply this booster to the records for which the provided filter matches.
     */
    ifMatchesFilter?: FilterExpression;
};

/**
 * Boost records based on the value of a datetime column. It is configured via "origin", "scale", and "decay". The further away from the "origin",
 * the more the score is decayed. The decay function uses an exponential function. For example if origin is "now", and scale is 10 days and decay is 0.5, it
 * should be interpreted as: a record with a date 10 days before/after origin will score 2 times less than a record with the date at origin.
 */
type DateBooster = {
    /*
     * The column in which to look for the value.
     */
    column: string;
    /*
     * The datetime (formatted as RFC3339) from where to apply the score decay function. The maximum boost will be applied for records with values at this time.
     * If it is not specified, the current date and time is used.
     */
    origin?: string;
    /*
     * The duration at which distance from origin the score is decayed with factor, using an exponential function. It is fromatted as number + units, for example: `5d`, `20m`, `10s`.
     *
     * @pattern ^(\d+)(d|h|m|s|ms)$
     */
    scale: string;
    /*
     * The decay factor to expect at "scale" distance from the "origin".
     */
    decay: number;
    /*
     * Only apply this booster to the records for which the provided filter matches.
     */
    ifMatchesFilter?: FilterExpression;
};

/**
 * @maxProperties 1
 * @minProperties 1
 */
type FilterColumnIncludes = {
    $includes?: FilterPredicate;
    $includesAny?: FilterPredicate;
    $includesAll?: FilterPredicate;
    $includesNone?: FilterPredicate;
};

type FilterPredicate = FilterValue | FilterPredicate[] | FilterPredicateOp | FilterPredicateRangeOp;

type FilterValue = number | string | boolean;

/**
 * @maxProperties 1
 * @minProperties 1
 */
type FilterPredicateOp = {
    $any?: FilterPredicate[];
    $all?: FilterPredicate[];
    $none?: FilterPredicate | FilterPredicate[];
    $not?: FilterPredicate | FilterPredicate[];
    $is?: FilterValue | FilterValue[];
    $isNot?: FilterValue | FilterValue[];
    $lt?: FilterRangeValue;
    $le?: FilterRangeValue;
    $gt?: FilterRangeValue;
    $ge?: FilterRangeValue;
    $contains?: string;
    $startsWith?: string;
    $endsWith?: string;
    $pattern?: string;
};

/**
 * @maxProperties 2
 * @minProperties 2
 */
type FilterPredicateRangeOp = {
    $lt?: FilterRangeValue;
    $le?: FilterRangeValue;
    $gt?: FilterRangeValue;
    $ge?: FilterRangeValue;
};

type FilterRangeValue = number | string;
Status CodeDescriptionExample Response/Type Definition
200OK
type SearchBranch = {
    records: Record[];
    warning?: string;
};

/**
 * Xata Table Record Metadata
 */
type Record = RecordMeta & {
    [key: string]: any;
};

/**
 * Xata Table Record Metadata
 */
type RecordMeta = {
    id: RecordID;
    xata: {
        /*
         * The record's version. Can be used for optimistic concurrency control.
         */
        version: number;
        /*
         * The record's table name. APIs that return records from multiple tables will set this field accordingly.
         */
        table?: string;
        /*
         * Highlights of the record. This is used by the search APIs to indicate which fields and parts of the fields have matched the search.
         */
        highlight?: {
            [key: string]: string[] | {
                [key: string]: any;
            };
        };
        /*
         * The record's relevancy score. This is returned by the search APIs.
         */
        score?: number;
        /*
         * Encoding/Decoding errors
         */
        warnings?: string[];
    };
};

/**
 * @pattern [a-zA-Z0-9_-~:]+
 */
type RecordID = string;
400Bad Request
type SearchBranch = {
    id?: string;
    message: string;
};
401Authentication Error
{
  "message": "invalid API key"
}
404Example response
type SearchBranch = {
    id?: string;
    message: string;
};
503Unexpected Error
5XXUnexpected Error