|
1
|
import { Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Store Search API Route`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn how to expose your search indexes to a storefront through the [Store Search API route](!api!/store/search/search-indexes), and how to control what a storefront can find in them. |
|
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
|
## What is the Store Search API Route? |
|
21
|
|
|
22
|
Medusa exposes a single `POST /store/search` route that searches any index of your application, so you don't create an API route per index. It answers with the search engine's own results, which is the contract the [InstantSearch adapter](../../../instantsearch/page.mdx) is built on. |
|
23
|
|
|
24
|
The route exposes nothing by default. An index is only searchable through it once the `configureStoreSearch` middleware allows it, and a request for any other index is answered as if the index didn't exist. That way, the route never tells a storefront what your application holds. |
|
25
|
|
|
26
|
--- |
|
27
|
|
|
28
|
## Allow Indexes on the Route |
|
29
|
|
|
30
|
To allow an index to be searched through the route, apply the `configureStoreSearch` middleware to `/store/search` in `src/api/middlewares.ts`: |
|
31
|
|
|
32
|
export const allowHighlights = [ |
|
33
|
["9", "matcher", "Apply the middleware to the Store Search API route."], |
|
34
|
["12", "allowed_indexes", "The indexes a storefront can search."], |
|
35
|
] |
|
36
|
|
|
37
|
```ts title="src/api/middlewares.ts" highlights={allowHighlights} |
|
38
|
import { |
|
39
|
configureStoreSearch, |
|
40
|
defineMiddlewares, |
|
41
|
} from "@medusajs/framework/http" |
|
42
|
|
|
43
|
export default defineMiddlewares({ |
|
44
|
routes: [ |
|
45
|
{ |
|
46
|
matcher: "/store/search", |
|
47
|
middlewares: [ |
|
48
|
configureStoreSearch({ |
|
49
|
allowed_indexes: { |
|
50
|
product: true, |
|
51
|
product_category: true, |
|
52
|
}, |
|
53
|
}), |
|
54
|
], |
|
55
|
}, |
|
56
|
], |
|
57
|
}) |
|
58
|
``` |
|
59
|
|
|
60
|
A storefront can now post a query with `"entity": "product"` or `"entity": "product_category"`. Every other index still answers with a `404` error, and Medusa logs why for whoever configured the route. |
|
61
|
|
|
62
|
You can apply the middleware more than once. Each one merges its `allowed_indexes` into the ones before it, so a plugin exposes its own index without discarding what your application allowed. |
|
63
|
|
|
64
|
<Note title="Tip"> |
|
65
|
|
|
66
|
Only allow indexes whose documents are public. An index holding internal data, such as one you built for [Medusa Admin's search](../admin-search/page.mdx), stays unreachable from a storefront as long as you don't allow it. |
|
67
|
|
|
68
|
</Note> |
|
69
|
|
|
70
|
--- |
|
71
|
|
|
72
|
## How Medusa Narrows a Product Index |
|
73
|
|
|
74
|
A storefront must never find a draft product, or a product that isn't in the request's sales channel. So, for an index whose `entity` is `product`, Medusa adds the following filters to every query: |
|
75
|
|
|
76
|
<Table> |
|
77
|
<Table.Header> |
|
78
|
<Table.Row> |
|
79
|
<Table.HeaderCell> |
|
80
|
|
|
81
|
Filter |
|
82
|
|
|
83
|
</Table.HeaderCell> |
|
84
|
<Table.HeaderCell> |
|
85
|
|
|
86
|
Applied when |
|
87
|
|
|
88
|
</Table.HeaderCell> |
|
89
|
</Table.Row> |
|
90
|
</Table.Header> |
|
91
|
<Table.Body> |
|
92
|
<Table.Row> |
|
93
|
<Table.Cell> |
|
94
|
|
|
95
|
`status` is `published`. |
|
96
|
|
|
97
|
</Table.Cell> |
|
98
|
<Table.Cell> |
|
99
|
|
|
100
|
The index declares a `status` field with the [`filterable` modifier](../index-definitions/modifiers/page.mdx#filterable). |
|
101
|
|
|
102
|
</Table.Cell> |
|
103
|
</Table.Row> |
|
104
|
<Table.Row> |
|
105
|
<Table.Cell> |
|
106
|
|
|
107
|
`sales_channel_ids` is one of the publishable API key's sales channels. |
|
108
|
|
|
109
|
</Table.Cell> |
|
110
|
<Table.Cell> |
|
111
|
|
|
112
|
The index declares a `sales_channel_ids` field with the [`filterable` modifier](../index-definitions/modifiers/page.mdx#filterable). |
|
113
|
|
|
114
|
</Table.Cell> |
|
115
|
</Table.Row> |
|
116
|
</Table.Body> |
|
117
|
</Table> |
|
118
|
|
|
119
|
So, to have Medusa scope your product index for you, declare both fields in the index definition. `sales_channel_ids` isn't a field of the product itself, so select the product's `sales_channels.id` and flatten them onto the document with a [`transform`](../index-definitions/page.mdx#index-part-of-an-entity): |
|
120
|
|
|
121
|
export const scopeHighlights = [ |
|
122
|
["8", "status", "Read by the route, never returned in a hit."], |
|
123
|
["9", "sales_channel_ids", "Read by the route, never returned in a hit."], |
|
124
|
["18", `"sales_channels.id"`, "Selected to fill `sales_channel_ids`."], |
|
125
|
] |
|
126
|
|
|
127
|
```ts title="src/search/product.ts" highlights={scopeHighlights} |
|
128
|
const productFields = search.define({ |
|
129
|
id: search.keyword().filterable().retrievable(), |
|
130
|
title: search |
|
131
|
.text() |
|
132
|
.searchable({ weight: 3 }) |
|
133
|
.retrievable(), |
|
134
|
// Read by `/store/search` to scope the index. |
|
135
|
status: search.keyword().filterable().retrievable(false), |
|
136
|
sales_channel_ids: search |
|
137
|
.keyword() |
|
138
|
.array() |
|
139
|
.filterable() |
|
140
|
.retrievable(false), |
|
141
|
// ... |
|
142
|
}) |
|
143
|
|
|
144
|
const source = { |
|
145
|
fields: ["id", "title", "status", "sales_channels.id"], |
|
146
|
transform: (products) => { |
|
147
|
return products.map((product) => ({ |
|
148
|
id: product.id, |
|
149
|
title: product.title, |
|
150
|
status: product.status, |
|
151
|
sales_channel_ids: product.sales_channels.map( |
|
152
|
(salesChannel) => salesChannel.id |
|
153
|
), |
|
154
|
})) |
|
155
|
}, |
|
156
|
} |
|
157
|
|
|
158
|
export default defineSearchIndex({ |
|
159
|
name: "product", |
|
160
|
entity: "product", |
|
161
|
fields: productFields, |
|
162
|
consume: graphConsume(source), |
|
163
|
seed: graphSeed(source), |
|
164
|
// ... |
|
165
|
}) |
|
166
|
``` |
|
167
|
|
|
168
|
The `status` and `sales_channel_ids` fields use [`retrievable(false)`](../index-definitions/modifiers/page.mdx#retrievable), so the route can filter on them without ever returning them in a hit. |
|
169
|
|
|
170
|
Since the route applies the `status` filter itself, index draft products as well, rather than leaving them out in the `transform`. A product then enters and leaves the storefront's results as its status changes, without waiting for a reindex. |
|
171
|
|
|
172
|
<Note title="Tip"> |
|
173
|
|
|
174
|
The index in this snippet is simplified to show one feature at a time. For examples of indexing prices in multiple currencies, option values, categories, and other product data, refer to the [Product Index Examples guide](../product-index-examples/page.mdx). |
|
175
|
|
|
176
|
</Note> |
|
177
|
|
|
178
|
If the index is missing either field, Medusa logs a warning once for that index and skips that filter. Then, either add the missing field to the definition, or scope the index yourself as explained in the next section. |
|
179
|
|
|
180
|
--- |
|
181
|
|
|
182
|
## Filter an Index for Your Storefront |
|
183
|
|
|
184
|
To constrain an index beyond what Medusa applies, pass a `filters` function for it instead of `true`. The function receives the request, so a constraint can depend on who is asking, and it can be asynchronous. |
|
185
|
|
|
186
|
For example, to only find active product categories: |
|
187
|
|
|
188
|
export const filtersHighlights = [ |
|
189
|
["15", "filters", "Applied to every query on this index."], |
|
190
|
] |
|
191
|
|
|
192
|
```ts title="src/api/middlewares.ts" highlights={filtersHighlights} |
|
193
|
import { |
|
194
|
configureStoreSearch, |
|
195
|
defineMiddlewares, |
|
196
|
} from "@medusajs/framework/http" |
|
197
|
|
|
198
|
export default defineMiddlewares({ |
|
199
|
routes: [ |
|
200
|
{ |
|
201
|
matcher: "/store/search", |
|
202
|
middlewares: [ |
|
203
|
configureStoreSearch({ |
|
204
|
allowed_indexes: { |
|
205
|
product: true, |
|
206
|
product_category: { |
|
207
|
filters: (req) => ({ is_active: true }), |
|
208
|
}, |
|
209
|
}, |
|
210
|
}), |
|
211
|
], |
|
212
|
}, |
|
213
|
// ... |
|
214
|
], |
|
215
|
}) |
|
216
|
``` |
|
217
|
|
|
218
|
Medusa combines your filters with the ones it applies itself and the ones the storefront posts using an `$and` operator. So, a storefront can narrow its own results but never widen them past what you allowed. |
|
219
|
|
|
220
|
--- |
|
221
|
|
|
222
|
## Search Documents in the Storefront |
|
223
|
|
|
224
|
Once an index is allowed, send a `POST` request to `/store/search` with the index's name in `entity`: |
|
225
|
|
|
226
|
```bash |
|
227
|
curl -X POST "http://localhost:9000/store/search" \ |
|
228
|
-H "x-publishable-api-key: pk_123" \ |
|
229
|
-H "Content-Type: application/json" \ |
|
230
|
--data '{ |
|
231
|
"entity": "product", |
|
232
|
"filters": { "q": "shirt" } |
|
233
|
}' |
|
234
|
``` |
|
235
|
|
|
236
|
Each hit's `document` holds every field of the index that's retrievable. Pass `fields` to narrow the hits to some of those fields. The route rejects a field the index can't return, so a storefront can't retrieve data the index doesn't hold. To keep a field out of the hits, mark it with [`retrievable(false)`](../index-definitions/modifiers/page.mdx#retrievable) in the index definition. |
|
237
|
|
|
238
|
Learn more about the accepted request body and the response in the [Store Search API reference](!api!/store/search/search-indexes). |
|
239
|
|
|
240
|
<Note title="Tip"> |
|
241
|
|
|
242
|
To build a full search experience with facets, pagination, and sorting in your storefront, use the [InstantSearch adapter](../../../instantsearch/page.mdx), which posts to this route for you. |
|
243
|
|
|
244
|
</Note> |