-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rackover
committed
Mar 6, 2021
1 parent
105bb1f
commit 14d9f0d
Showing
10 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const maxSummaryLength = 40; | ||
const backwardLength = 8; | ||
|
||
module.exports = async function (req, response){ | ||
|
||
const body = req.query; | ||
const miss = utility.getMissingFields(body, ["term"]) | ||
|
||
if (miss.length > 0 || body.term.length <= 0){ | ||
response.error = { | ||
data: 'No search term was given' | ||
}; | ||
|
||
return response; | ||
} | ||
|
||
const results = await searchEngine.search(body.term); | ||
|
||
for (i in results){ | ||
let wholePage = results[i].content; | ||
let summary = wholePage | ||
.slice(Math.max(0, results[i].position-backwardLength)); | ||
|
||
const originalLength = summary.length; | ||
|
||
summary = summary | ||
.slice(0, Math.min(maxSummaryLength, summary.length)) | ||
.join(" "); | ||
|
||
if (summary.length < originalLength){ | ||
summary += "..."; | ||
} | ||
|
||
results[i].summary = summary; | ||
} | ||
|
||
response.searchTerm = body.term; | ||
response.results = results; | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
const minWordLength = 4; | ||
let index = {}; | ||
|
||
module.exports = { | ||
updateIndexForPage: updateIndexForPage, | ||
updateIndexForPageIfNecessary: updateIndexForPageIfNecessary, | ||
removePageFromIndex: removePageFromIndex, | ||
search: search | ||
} | ||
|
||
async function updateIndexForPageIfNecessary(virtualPath, title, content){ | ||
if (index[virtualPath] == undefined || index[virtualPath].length != content.length){ | ||
logger.debug(`Updating search index for virtual path ${virtualPath}`); | ||
await updateIndexForPage(virtualPath, title, content); | ||
} | ||
} | ||
|
||
async function updateIndexForPage(virtualPath, title, content){ | ||
if (index[virtualPath] != undefined){ | ||
removePageFromIndex(virtualPath); | ||
} | ||
|
||
let entry = { | ||
length: content.length, | ||
title: title, | ||
words: [] | ||
}; | ||
|
||
let pageWords = content | ||
.replace(/[^A-Za-zÀ-ÖØ-öø-ÿ ]/gi, '') | ||
.split(" ") | ||
.filter((o) => o.length >= minWordLength) | ||
.map((o) => o.toLowerCase()); | ||
|
||
entry.words = pageWords; | ||
|
||
index[virtualPath] = entry; | ||
} | ||
|
||
function removePageFromIndex(virtualPath){ | ||
logger.debug(`Removing search index for virtual path ${virtualPath}`); | ||
delete index[virtualPath]; | ||
} | ||
|
||
function search(term){ | ||
const microtime = (Date.now() % 1000) / 1000; | ||
logger.info(`Searching for term [${term}] in the index...`); | ||
|
||
return new Promise((resolve, reject) => { | ||
term = term.toLowerCase(); | ||
let results = []; | ||
for (path in index){ | ||
if (index[path].title.includes(term)){ | ||
|
||
results.push({ | ||
virtualPath: path, | ||
title: index[path].title, | ||
content: index[path].words, | ||
position: 0 | ||
}); | ||
|
||
continue; | ||
} | ||
|
||
for(i in index[path].words){ | ||
const word = index[path].words[i]; | ||
|
||
if (word.length < term.length){ | ||
continue; | ||
} | ||
|
||
if (word.includes(term)){ | ||
results.push({ | ||
virtualPath: path, | ||
title: index[path].title, | ||
content: index[path].words | ||
.map((o)=>{ return o == word ? `<b>${word}</b>` : o; }), | ||
position: i | ||
}); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
logger.info(`Found ${results.length} for search [${term}] after ${((Date.now() % 1000) / 1000) - microtime}ms`); | ||
resolve(results); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
extends ../layout/default.pug | ||
|
||
block head | ||
script(src="/res/js/loginForm.js") | ||
|
||
block content | ||
h1 Results for "#{searchTerm}" | ||
each result in results | ||
h2 | ||
a(href="/read/"+result.virtualPath) #{result.title} | ||
p !{result.summary} |
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