|
1
|
import { TypeList, Prerequisites, Kbd, getOsShortcut } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Medusa Admin Dashboard Search`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn how the Medusa Admin dashboard's global search works, and how to make the records of a custom entity searchable in it. |
|
10
|
|
|
11
|
<Prerequisites |
|
12
|
items={[ |
|
13
|
{ |
|
14
|
text: "Medusa v2.21.1+", |
|
15
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.21.1" |
|
16
|
} |
|
17
|
]} |
|
18
|
/> |
|
19
|
|
|
20
|
## How Admin Search Works |
|
21
|
|
|
22
|
The Medusa Admin dashboard has a global search that admin users open with <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>. It searches records, such as orders or products, and it also lists the dashboard's keyboard shortcuts and navigation entries. |
|
23
|
|
|
24
|
The route searches one entity at a time and returns a group of results per entity: |
|
25
|
|
|
26
|
- For an entity that has a [search index definition](../index-definitions/page.mdx), the route searches the index through the [Search Module](../page.mdx). The index's search provider ranks the results, so the entity benefits from typo tolerance, weights, and the rest of the engine's features. |
|
27
|
- For an entity that has no index, the route queries the entity with [Query](!docs!/learn/fundamentals/query) using the same free-text `q` filter that the entity's list API route accepts. |
|
28
|
|
|
29
|
The route never merges the groups, since relevance scores are only comparable within one index. Each group has its own `count`, `offset`, and `limit`. |
|
30
|
|
|
31
|
--- |
|
32
|
|
|
33
|
## Make a Custom Entity Searchable in Medusa Admin |
|
34
|
|
|
35
|
To show the records of a custom entity within the Medusa Admin search, such as a `brand` data model of a custom module, in the dashboard's search, you: |
|
36
|
|
|
37
|
1. Declare a search index for the entity, which is what the route searches. |
|
38
|
2. Register the entity in the dashboard, which renders its search results in the search panel. |
|
39
|
|
|
40
|
### 1. Declare a Search Index |
|
41
|
|
|
42
|
Create a search index definition for the entity in a file under the `src/search` directory. |
|
43
|
|
|
44
|
For example, to make brands searchable, create the file `src/search/brand.ts` with the following content: |
|
45
|
|
|
46
|
export const definitionHighlights = [ |
|
47
|
["11", "name", "The name the dashboard requests results for."], |
|
48
|
["15", "name", "The field the query matches on."], |
|
49
|
["16", "website", "Shown as the result's subtitle later."], |
|
50
|
] |
|
51
|
|
|
52
|
```ts title="src/search/brand.ts" highlights={definitionHighlights} |
|
53
|
import { |
|
54
|
defineSearchIndex, |
|
55
|
graphConsume, |
|
56
|
graphSeed, |
|
57
|
search, |
|
58
|
} from "@medusajs/framework/utils" |
|
59
|
|
|
60
|
const fields = ["id", "name", "website"] |
|
61
|
|
|
62
|
export default defineSearchIndex({ |
|
63
|
name: "brand", |
|
64
|
entity: "brand", |
|
65
|
fields: search.define({ |
|
66
|
id: search.keyword().filterable(), |
|
67
|
name: search.text().searchable({ weight: 3 }), |
|
68
|
website: search.keyword().searchable(), |
|
69
|
}), |
|
70
|
events: [ |
|
71
|
"brand.created", |
|
72
|
"brand.updated", |
|
73
|
"brand.deleted", |
|
74
|
], |
|
75
|
consume: graphConsume({ fields }), |
|
76
|
seed: graphSeed({ fields }), |
|
77
|
}) |
|
78
|
``` |
|
79
|
|
|
80
|
Make sure every field that you show in a search result, such as the title and the subtitle, is `searchable` or stored in the index. |
|
81
|
|
|
82
|
Learn more about index definitions and their fields in the [Search Index Definitions guide](../index-definitions/page.mdx). |
|
83
|
|
|
84
|
Then, run the migrations to create the physical index: |
|
85
|
|
|
86
|
```bash npm2yarn |
|
87
|
npx medusa db:migrate |
|
88
|
``` |
|
89
|
|
|
90
|
### 2. Register the Entity in the Dashboard |
|
91
|
|
|
92
|
To register a custom entity in the Medusa Admin search, create the file `src/admin/search-entities.tsx` in your Medusa application or plugin. Medusa loads this file into the dashboard, so any entity you register in it becomes part of the search. |
|
93
|
|
|
94
|
In the file, use `defineSearchEntity` from the dashboard SDK to register the entity: |
|
95
|
|
|
96
|
export const entityHighlights = [ |
|
97
|
["3", `"brand"`, "The name of the search index to show results for."], |
|
98
|
["4", "groupLabel", "The heading shown above the entity's results."], |
|
99
|
["6", "keys", "The keys that jump to the brands page."], |
|
100
|
["10", "transform", "Turns a search result into a palette item."], |
|
101
|
["13", "subtitle", "Shown next to the title, in a muted color."], |
|
102
|
["14", "to", "The page the dashboard opens on selection."], |
|
103
|
["15", "value", "A unique value that identifies the item."], |
|
104
|
] |
|
105
|
|
|
106
|
```tsx title="src/admin/search-entities.tsx" highlights={entityHighlights} |
|
107
|
import { defineSearchEntity } from "@medusajs/dashboard/lib" |
|
108
|
|
|
109
|
defineSearchEntity("brand", { |
|
110
|
groupLabel: "Brands", |
|
111
|
shortcut: { |
|
112
|
keys: { Mac: ["G", "B"] }, |
|
113
|
label: "Go to Brands", |
|
114
|
to: "/brands", |
|
115
|
}, |
|
116
|
transform: (brand: any) => ({ |
|
117
|
id: brand.id, |
|
118
|
title: brand.name, |
|
119
|
subtitle: brand.website, |
|
120
|
to: `/brands/${brand.id}`, |
|
121
|
value: `brand:${brand.id}`, |
|
122
|
}), |
|
123
|
}) |
|
124
|
``` |
|
125
|
|
|
126
|
`defineSearchEntity` accepts the entity's name as a first parameter, which must match the search index's `name`, and the entity's definition as a second parameter. |
|
127
|
|
|
128
|
The definition accepts the following properties: |
|
129
|
|
|
130
|
<TypeList |
|
131
|
types={[ |
|
132
|
{ |
|
133
|
name: "transform", |
|
134
|
type: "`(item: any) => object`", |
|
135
|
optional: true, |
|
136
|
description: "A function that receives a result from the [Search Admin API route](!api!/admin/search/search-admin) and returns the item to show in the palette. Omit it to register a navigation shortcut alone.", |
|
137
|
expandable: false, |
|
138
|
children: [ |
|
139
|
{ |
|
140
|
name: "id", |
|
141
|
type: "`string`", |
|
142
|
optional: false, |
|
143
|
description: "The item's unique ID." |
|
144
|
}, |
|
145
|
{ |
|
146
|
name: "title", |
|
147
|
type: "`string`", |
|
148
|
optional: false, |
|
149
|
description: "The item's heading in the palette." |
|
150
|
}, |
|
151
|
{ |
|
152
|
name: "subtitle", |
|
153
|
type: "`string`", |
|
154
|
optional: true, |
|
155
|
description: "A line shown under the item's title." |
|
156
|
}, |
|
157
|
{ |
|
158
|
name: "to", |
|
159
|
type: "`string`", |
|
160
|
optional: false, |
|
161
|
description: "The path the dashboard navigates to when you choose the item." |
|
162
|
}, |
|
163
|
{ |
|
164
|
name: "thumbnail", |
|
165
|
type: "`string`", |
|
166
|
optional: true, |
|
167
|
description: "The URL of an image shown next to the item." |
|
168
|
}, |
|
169
|
{ |
|
170
|
name: "value", |
|
171
|
type: "`string`", |
|
172
|
optional: false, |
|
173
|
description: "The text the palette matches the typed query against." |
|
174
|
} |
|
175
|
] |
|
176
|
}, |
|
177
|
{ |
|
178
|
name: "groupLabel", |
|
179
|
type: "`string` \\| `((t: SearchLabelTranslator) => string)`", |
|
180
|
optional: true, |
|
181
|
defaultValue: "The entity's name", |
|
182
|
description: "The heading shown above the entity's results. Pass a string, or a function that receives the `t` translation function and returns a string." |
|
183
|
}, |
|
184
|
{ |
|
185
|
name: "shortcut", |
|
186
|
type: "`object`", |
|
187
|
optional: true, |
|
188
|
description: "A \"Jump to\" entry that shows in the palette's navigation group. Omit it for an entity that has no page of its own.", |
|
189
|
expandable: false, |
|
190
|
children: [ |
|
191
|
{ |
|
192
|
name: "keys", |
|
193
|
type: "`object`", |
|
194
|
optional: false, |
|
195
|
description: "The keys to press, such as `{ Mac: [\"G\", \"Z\"] }`." |
|
196
|
}, |
|
197
|
{ |
|
198
|
name: "to", |
|
199
|
type: "`string`", |
|
200
|
optional: false, |
|
201
|
description: "The path the shortcut navigates to." |
|
202
|
}, |
|
203
|
{ |
|
204
|
name: "label", |
|
205
|
type: "`string` \\| `((t: SearchLabelTranslator) => string)`", |
|
206
|
optional: true, |
|
207
|
defaultValue: "The entity's group label", |
|
208
|
description: "The label of the \"Jump to\" entry." |
|
209
|
}, |
|
210
|
{ |
|
211
|
name: "type", |
|
212
|
type: "`\"pageShortcut\"` \\| `\"settingShortcut\"`", |
|
213
|
optional: true, |
|
214
|
defaultValue: "pageShortcut", |
|
215
|
description: "Whether the entry is grouped with the page or the settings shortcuts." |
|
216
|
} |
|
217
|
] |
|
218
|
} |
|
219
|
]} |
|
220
|
sectionTitle="Register a Custom Entity" |
|
221
|
/> |
|
222
|
|
|
223
|
Finally, start the Medusa application: |
|
224
|
|
|
225
|
```bash npm2yarn |
|
226
|
npm run dev |
|
227
|
``` |
|
228
|
|
|
229
|
Open the dashboard, press <Kbd>{getOsShortcut()}</Kbd> + <Kbd>K</Kbd>, and type a brand's name. The results show under a "Brands" heading, and selecting one opens the brand's page. |
|
230
|
|
|
231
|
### Override or Remove Built-In Entities |
|
232
|
|
|
233
|
The dashboard registers the built-in commerce entities before it loads your file, so you can change or remove them in the same file. |
|
234
|
|
|
235
|
To change how a built-in entity's results show, register it again with the same name. For example, to show a product's handle as its subtitle: |
|
236
|
|
|
237
|
```tsx title="src/admin/search-entities.tsx" |
|
238
|
import { defineSearchEntity } from "@medusajs/dashboard/lib" |
|
239
|
|
|
240
|
defineSearchEntity("product", { |
|
241
|
groupLabel: "Products", |
|
242
|
transform: (product: any) => ({ |
|
243
|
id: product.id, |
|
244
|
title: product.title, |
|
245
|
subtitle: product.handle, |
|
246
|
thumbnail: product.thumbnail, |
|
247
|
to: `/products/${product.id}`, |
|
248
|
value: `product:${product.id}`, |
|
249
|
}), |
|
250
|
}) |
|
251
|
``` |