Skip to content

Commit

Permalink
Make datastore search POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Sep 24, 2024
1 parent 965cdbe commit 62f9099
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
25 changes: 20 additions & 5 deletions src/api/rest/v1/search/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,37 @@ class SearchController {
const did = await account.did()

const schemaName = Utils.getSchemaFromParams(req.params[0])
const query = req.query.keywords.toString()
const searchOptions = req.query.options ? JSON.parse(req.query.options.toString()) : {}
const indexFields = req.query.fields ? req.query.fields.toString().split(',') : []
let storeFields = req.query.store ? req.query.store.toString().split(',') : []
const query = req.body.keywords ? req.body.keywords.toString() : undefined

if (!query) {
throw new Error(`Keywords are required`)
}

const indexFields = req.body.index ? req.body.index : []

if (!indexFields) {
throw new Error(`Index fields are required`)
}

const searchOptions = req.body.options ? req.body.options : {}
let storeFields = req.body.store ? req.body.store : []
const permissions = Utils.buildPermissions(req)
const limit = req.query.limit ? parseInt(req.query.limit.toString()) : DEFAULT_LIMIT
const limit = req.body.limit ? parseInt(req.body.limit.toString()) : DEFAULT_LIMIT

const searchResults = await MinisearchService.searchDs(context, did, schemaName, query, searchOptions, indexFields, storeFields, limit, permissions)
const datastore = await context.openDatastore(schemaName)
const items: SchemaRecordSearchResult[] = []

for (const searchResult of searchResults.results) {
const item = await datastore.get(searchResult.id, {})
items.push({
...item,
_match: searchResult
})

if (items.length >= limit) {
break
}
}

return res.json({
Expand Down
2 changes: 1 addition & 1 deletion src/api/rest/v1/search/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const router = express.Router()

router.get("/universal", controller.universal)
router.get("/chatThreads", controller.chatThreads)
router.get(/datastore\/(.*)$/, controller.datastore)
router.post(/datastore\/(.*)$/, controller.datastore)

export default router
32 changes: 19 additions & 13 deletions src/web/developer/api/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Defaults to \`"emails,chat-messages"\`.
}
},
"/search/datastore/{schemaUrl}": {
"method": "GET",
"method": "POST",
"path": `${apiPrefix}/search/datastore/{schemaUrl}`,
"documentation": `Execute a keyword search on a datastore.
Expand All @@ -196,38 +196,44 @@ Returns:
"default": "robert gray",
"required": true
},
"index": {
"type": "object",
"documentation": "Array of fields to include in the search index",
"default": JSON.stringify(`["name", "description", "favouriteType", "sourceApplication"]`),
"required": true
},
"options": {
"type": "object",
"documentation": `Search options that match the options availalbe from the [minisearch documentation](https://www.npmjs.com/package/minisearch).
"documentation": `Search options that match the options available from the [minisearch documentation](https://www.npmjs.com/package/minisearch).
**Example:**
\`\`\`
{
fields: ['title', 'text'],
searchOptions: {
boost: { title: 2 },
fuzzy: 0.2
"fields": ["name", "description"],
"searchOptions": {
"boost": { "name": 2 },
"fuzzy": 0.2
}
}
\`\`\`,
\`\`\`
`,
"default": "{}"
"default": JSON.stringify("{}")
},
"limit": {
"type": "number",
"documentation": "Limit how many chat threads to return. Defaults to `20`.",
"default": 20
},
"fields": {
"type": "string",
"documentation": "Comma separated list of fields to inlude in search index (ie: `name,description`)",
"default": "name,description"
"type": "object",
"documentation": "Comma separated list of fields to include in search index (ie: `name,description`)",
"default": JSON.stringify(`["name", "description"]`)
},
"store": {
"type": "string",
"type": "object",
"documentation": "Comma separated list of fields to store in the index and return with results (ie: `name,description`)",
"default": "name,description"
"default": JSON.stringify(`["name", "description"]`)
}
}
},
Expand Down

0 comments on commit 62f9099

Please sign in to comment.