-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from sooluh/maximizing-search-algorithm
feat: maximizing search algorithm
- Loading branch information
Showing
11 changed files
with
88 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
import { KeywordOptions } from '../../types' | ||
import { createSpecResponse } from '../helpers/spec' | ||
import { createSpecResponse, sendBadRequest } from '../helpers/spec' | ||
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' | ||
|
||
export const search = (app: FastifyInstance) => { | ||
return async (request: FastifyRequest<{ Querystring: KeywordOptions }>, reply: FastifyReply) => { | ||
const { q } = request.query | ||
// TODO: search by province, regency, or district | ||
const data = app.fuse.search(q).sort((a, b) => (a.score || 0) - (b.score || 0)) | ||
|
||
reply.header('Cache-Control', 's-maxage=86400, stale-while-revalidate=604800') | ||
if (!q) { | ||
return sendBadRequest(reply) | ||
} | ||
|
||
const keywords = q | ||
// remove duplicate spaces | ||
.replace(/\s+/g, ' ') | ||
.split(' ') | ||
// add extended search per word | ||
// https://www.fusejs.io/examples.html#extended-search | ||
.map((i) => `'${i}`) | ||
.join(' ') | ||
|
||
const result = data.map(({ item }) => item) | ||
const data = app.fuse.search(keywords) | ||
const result = data.map(({ item: { fulltext, ...rest } }) => rest) | ||
const response = createSpecResponse(result) | ||
|
||
reply.header('Cache-Control', 's-maxage=86400, stale-while-revalidate=604800') | ||
return reply.send(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters