mirror of
https://github.com/alexankitty/Myrient-Search-Engine.git
synced 2026-01-15 16:33:15 -03:00
improve search to be next word predictive
This commit is contained in:
@@ -2,4 +2,10 @@ export default function debugPrint(string){
|
||||
if(process.env.DEBUG == "1"){
|
||||
console.log(string)
|
||||
}
|
||||
}
|
||||
|
||||
export function debugPrintDir(string){
|
||||
if(process.env.DEBUG == "1"){
|
||||
console.dir(string)
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,73 @@ export default class Searcher{
|
||||
getIndexSize(){
|
||||
return this.miniSearch._storedFields.size
|
||||
}
|
||||
getSuggestions(query){
|
||||
return this.miniSearch.autoSuggest(query)
|
||||
async getSuggestions(query, options){
|
||||
options.fields = ['filename', 'category'] //reduce field search
|
||||
let matches = await this.findAllMatches(query, options)
|
||||
let results = matches.items
|
||||
console.log(results)
|
||||
let suggestions = []
|
||||
for(let result = 0; result < results.length; result++){
|
||||
let currentResult = results[result]
|
||||
let fileString = String(currentResult.filename).toLowerCase()
|
||||
let categoryString = String(currentResult.category).toLowerCase()
|
||||
let fileSplit = fileString.split(query)
|
||||
let categorySplit = categoryString.split(query)
|
||||
console.log(fileSplit)
|
||||
console.log(categorySplit)
|
||||
if(fileSplit.length > 1){
|
||||
let wordSplit = String(fileSplit[1]).split(' ')
|
||||
if(!wordSplit[0]){
|
||||
wordSplit.shift()
|
||||
}
|
||||
console.log(wordSplit)
|
||||
let prediction = ''
|
||||
let prefixMatch = String(fileSplit[1]).substring(0,1) != ' '
|
||||
let prefixSpace = prefixMatch ? '' : ' '
|
||||
if(wordSplit.length > 1){
|
||||
prediction = `${prefixSpace}${wordSplit[0]} ${wordSplit[1]}`
|
||||
}
|
||||
else if (wordSplit.length == 1){
|
||||
prediction = `${prefixSpace}${wordSplit[0]}`
|
||||
}
|
||||
else {
|
||||
//bad result discard
|
||||
continue
|
||||
}
|
||||
suggestions.push(`${query}${prediction}`)
|
||||
continue
|
||||
}
|
||||
if(categorySplit.length > 1){
|
||||
let wordSplit = String(categorySplit[1]).split(' ')
|
||||
if(!wordSplit[0]){
|
||||
wordSplit.shift()
|
||||
}
|
||||
console.log(wordSplit)
|
||||
let prediction = ''
|
||||
let prefixMatch = String(categorySplit[1]).substring(0,1) != ' '
|
||||
let prefixSpace = prefixMatch ? '' : ' '
|
||||
if(wordSplit.length > 1){
|
||||
prediction = `${prefixSpace}${wordSplit[0]} ${wordSplit[1]}`
|
||||
}
|
||||
else if (wordSplit.length == 1){
|
||||
prediction = `${prefixSpace}${wordSplit[0]}`
|
||||
}
|
||||
else {
|
||||
//bad result discard
|
||||
continue
|
||||
}
|
||||
suggestions.push(`${query}${prediction}`)
|
||||
continue
|
||||
}
|
||||
}
|
||||
let dedupe = [...new Set(suggestions)]
|
||||
let dedupeLimit = dedupe.length >= 10 ? 10 : dedupe.length
|
||||
let arr = []
|
||||
for(let x = 0; x < dedupeLimit; x++){
|
||||
arr.push({
|
||||
suggestion: dedupe[x]
|
||||
})
|
||||
}
|
||||
return arr
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,8 @@
|
||||
["pc88", "pc-88", "pc 88"],
|
||||
["dvd", "digital video disc", "digital versatile disc"],
|
||||
["mastersystem", "ms", "master system"],
|
||||
["wii", "revolution"]
|
||||
["wii", "revolution"],
|
||||
["bros", "brothers", "bros."],
|
||||
["bros.", "brothers", "bros"]
|
||||
]
|
||||
}
|
||||
@@ -196,14 +196,15 @@ app.get("/settings", function (req, res) {
|
||||
res.render(indexPage, options);
|
||||
});
|
||||
|
||||
app.post("/suggest", function(req, res){
|
||||
app.post("/suggest", async function(req, res){
|
||||
if(!req.body){
|
||||
return
|
||||
}
|
||||
if(typeof req.body.query == 'undefined'){
|
||||
return
|
||||
}
|
||||
let suggestions = search.getSuggestions(req.body.query)
|
||||
let suggestions = await search.getSuggestions(req.body.query, defaultSettings)
|
||||
debugPrint(suggestions)
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({ suggestions }));
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user