Skip to content

Commit e0f5b00

Browse files
committed
Lint, and add support for passing search options into the endpoint
1 parent 5ac7e49 commit e0f5b00

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

controllers/search.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ function buildDualIndexQueries(searchText, operator, limit, skip) {
263263
const searchAsWords = async function (req, res, next) {
264264
res.set("Content-Type", "application/json; charset=utf-8")
265265
let searchText = req.body?.searchText ?? req.body
266+
const searchOptions = req.body?.options ?? {}
266267
if (!searchText) {
267268
let err = {
268269
message: "You did not provide text to search for in the search request.",
@@ -273,15 +274,12 @@ const searchAsWords = async function (req, res, next) {
273274
}
274275
const limit = parseInt(req.query.limit ?? 100)
275276
const skip = parseInt(req.query.skip ?? 0)
276-
277-
const [queryPresi3, queryPresi2] = buildDualIndexQueries(searchText, { type: "text", options: {} }, limit, skip)
278-
277+
const [queryPresi3, queryPresi2] = buildDualIndexQueries(searchText, { type: "text", options: searchOptions }, limit, skip)
279278
try {
280279
const [resultsPresi3, resultsPresi2] = await Promise.all([
281280
db.aggregate(queryPresi3).toArray().catch((err) => { console.error("Presi3 error:", err.message); return [] }),
282281
db.aggregate(queryPresi2).toArray().catch((err) => { console.error("Presi2 error:", err.message); return [] })
283282
])
284-
285283
const merged = mergeSearchResults(resultsPresi3, resultsPresi2)
286284
let results = merged.slice(skip, skip + limit)
287285
results = results.map(o => idNegotiation(o))
@@ -350,6 +348,10 @@ const searchAsWords = async function (req, res, next) {
350348
const searchAsPhrase = async function (req, res, next) {
351349
res.set("Content-Type", "application/json; charset=utf-8")
352350
let searchText = req.body?.searchText ?? req.body
351+
const phraseOptions = req.body?.options ??
352+
{
353+
slop: 2
354+
}
353355
if (!searchText) {
354356
let err = {
355357
message: "You did not provide text to search for in the search request.",
@@ -360,10 +362,6 @@ const searchAsPhrase = async function (req, res, next) {
360362
}
361363
const limit = parseInt(req.query.limit ?? 100)
362364
const skip = parseInt(req.query.skip ?? 0)
363-
364-
const phraseOptions = {
365-
slop: 2
366-
}
367365
const [queryPresi3, queryPresi2] = buildDualIndexQueries(searchText, { type: "phrase", options: phraseOptions }, limit, skip)
368366
try {
369367
const [resultsPresi3, resultsPresi2] = await Promise.all([
@@ -426,6 +424,14 @@ const searchAsPhrase = async function (req, res, next) {
426424
const searchFuzzily = async function (req, res, next) {
427425
res.set("Content-Type", "application/json; charset=utf-8")
428426
let searchText = req.body?.searchText ?? req.body
427+
const fuzzyOptions = req.body?.options ??
428+
{
429+
fuzzy: {
430+
maxEdits: 1,
431+
prefixLength: 2,
432+
maxExpansions: 50
433+
}
434+
}
429435
if (!searchText) {
430436
let err = {
431437
message: "You did not provide text to search for in the search request.",
@@ -436,13 +442,6 @@ const searchFuzzily = async function (req, res, next) {
436442
}
437443
const limit = parseInt(req.query.limit ?? 100)
438444
const skip = parseInt(req.query.skip ?? 0)
439-
const fuzzyOptions = {
440-
fuzzy: {
441-
maxEdits: 1,
442-
prefixLength: 2,
443-
maxExpansions: 50
444-
}
445-
}
446445
const [queryPresi3, queryPresi2] = buildDualIndexQueries(searchText, { type: "text", options: fuzzyOptions }, limit, skip)
447446
try {
448447
const [resultsPresi3, resultsPresi2] = await Promise.all([
@@ -517,6 +516,10 @@ const searchFuzzily = async function (req, res, next) {
517516
const searchWildly = async function (req, res, next) {
518517
res.set("Content-Type", "application/json; charset=utf-8")
519518
let searchText = req.body?.searchText ?? req.body
519+
const wildcardOptions = req.body?.options ??
520+
{
521+
allowAnalyzedField: true
522+
}
520523
if (!searchText) {
521524
let err = {
522525
message: "You did not provide text to search for in the search request.",
@@ -536,9 +539,6 @@ const searchWildly = async function (req, res, next) {
536539
}
537540
const limit = parseInt(req.query.limit ?? 100)
538541
const skip = parseInt(req.query.skip ?? 0)
539-
const wildcardOptions = {
540-
allowAnalyzedField: true
541-
}
542542
const [queryPresi3, queryPresi2] = buildDualIndexQueries(searchText, { type: "wildcard", options: wildcardOptions }, limit, skip)
543543
try {
544544
const [resultsPresi3, resultsPresi2] = await Promise.all([

0 commit comments

Comments
 (0)