1 import { SearchSchema } from "@medusajs/types"
2 import { BooleanProperty } from "./properties/boolean"
3 import { DateProperty } from "./properties/date"
4 import { FloatProperty } from "./properties/float"
5 import { GeoProperty } from "./properties/geo"
6 import { IntegerProperty } from "./properties/integer"
7 import { KeywordProperty } from "./properties/keyword"
8 import { ObjectProperty } from "./properties/object"
9 import { TextProperty } from "./properties/text"
10 import { VectorProperty } from "./properties/vector"
11 import { SearchFieldsSchema } from "./search-fields-schema"
12
13 /**
14 * Builder that exposes the search fields DSL — analogous to DML's EntityBuilder,
15 * but only for index field schemas (registration stays on `defineSearchIndex`).
16 */
17 export class SearchBuilder {
18 /**
19 * Define a search fields schema. Pass the result as `fields` to
20 * `defineSearchIndex`. Does not register anything.
21 *
22 * @example
23 * defineSearchIndex({
24 * name: "product",
25 * entity: "product",
26 * fields: search.define({
27 * id: search.keyword().filterable(),
28 * title: search.text().searchable({ weight: 3 }),
29 * }),
30 * async *seed() {},
31 * })
32 */
33 define<Schema extends SearchSchema>(schema: Schema): SearchFieldsSchema<Schema> {
34 return new SearchFieldsSchema(schema)
35 }
36
37 keyword() {
38 return new KeywordProperty()
39 }
40
41 text() {
42 return new TextProperty()
43 }
44
45 integer() {
46 return new IntegerProperty()
47 }
48
49 float() {
50 return new FloatProperty()
51 }
52
53 boolean() {
54 return new BooleanProperty()
55 }
56
57 date() {
58 return new DateProperty()
59 }
60
61 geo() {
62 return new GeoProperty()
63 }
64
65 object<Schema extends SearchSchema>(schema: Schema) {
66 return new ObjectProperty(schema)
67 }
68
69 vector(dimensions: number) {
70 return new VectorProperty(dimensions)
71 }
72 }
73
74 /**
75 * Singleton used to define search index field schemas:
76 *
77 * ```ts
78 * import { defineSearchIndex, search } from "@medusajs/framework/utils"
79 *
80 * defineSearchIndex({
81 * name: "product",
82 * entity: "product",
83 * fields: search.define({
84 * id: search.keyword().filterable(),
85 * title: search.text().searchable({ weight: 3 }),
86 * }),
87 * async *seed() {},
88 * })
89 * ```
90 */
91 export const search = new SearchBuilder()