#25 - quoted terms and exclusions

This commit is contained in:
Simon Cambier
2022-04-29 22:56:41 +02:00
parent 317aeefc0a
commit 0f7036abae
5 changed files with 416 additions and 13 deletions

View File

@@ -13,6 +13,7 @@ import {
stripMarkdownCharacters,
wait,
} from './utils'
import { Query } from './query'
let minisearchInstance: MiniSearch<IndexedNote>
@@ -60,12 +61,12 @@ export async function initGlobalSearchIndex(): Promise<void> {
/**
* Searches the index for the given query,
* and returns an array of raw results
* @param query
* @param text
* @returns
*/
async function search(query: string): Promise<SearchResult[]> {
if (!query) return []
let results = minisearchInstance.search(query, {
async function search(query: Query): Promise<SearchResult[]> {
if (!query.getWordsStr()) return []
let results = minisearchInstance.search(query.getWordsStr(), {
prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false),
combineWith: 'AND',
@@ -78,13 +79,24 @@ async function search(query: string): Promise<SearchResult[]> {
})
// If the search query contains quotes, filter out results that don't have the exact match
const quoted = splitQuotes(query.toLowerCase())
if (quoted.length) {
const exactTerms = query.getExactTerms()
if (exactTerms.length) {
results = results.filter(r => {
const content = stripMarkdownCharacters(
indexedNotes[r.id]?.content ?? '',
).toLowerCase()
return quoted.every(q => content.includes(q))
return exactTerms.every(q => content.includes(q))
})
}
// // If the search query contains exclude terms, filter out results that have them
const exclusions = query.exclusions
if (exclusions.length) {
results = results.filter(r => {
const content = stripMarkdownCharacters(
indexedNotes[r.id]?.content ?? '',
).toLowerCase()
return exclusions.every(q => !content.includes(q.value))
})
}
return results
@@ -115,11 +127,11 @@ export function getMatches(text: string, reg: RegExp): SearchMatch[] {
* @returns
*/
export async function getSuggestions(
query: string,
queryStr: string,
options?: Partial<{ singleFilePath: string | null }>,
): Promise<ResultNote[]> {
query = query.toLowerCase()
// Get the raw results
const query = new Query(queryStr)
let results = await search(query)
if (!results.length) return []
@@ -143,8 +155,8 @@ export async function getSuggestions(
// Clean search matches that match quoted expresins,
// and inject those expressions instead
const quoted = splitQuotes(query)
let words = Object.keys(result.match)
const quoted = splitQuotes(query.getWordsStr())
for (const quote of quoted) {
for (const q of quote.toLowerCase()) {
words = words.filter(w => !w.toLowerCase().startsWith(q))