|
|
@ -10,6 +10,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
import { processSearchText, ProcessSearchTextOptions } from '../helpers/cleanSearchText'; |
|
|
|
import { processSearchText, ProcessSearchTextOptions } from '../helpers/cleanSearchText'; |
|
|
|
|
|
|
|
import flatten from '../helpers/array/flatten'; |
|
|
|
|
|
|
|
|
|
|
|
export default class SearchIndex<SearchWhat> { |
|
|
|
export default class SearchIndex<SearchWhat> { |
|
|
|
private fullTexts: Map<SearchWhat, string> = new Map(); |
|
|
|
private fullTexts: Map<SearchWhat, string> = new Map(); |
|
|
@ -49,15 +50,9 @@ export default class SearchIndex<SearchWhat> { |
|
|
|
}); */ |
|
|
|
}); */ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public search(query: string) { |
|
|
|
private _search(query: string) { |
|
|
|
const fullTexts = this.fullTexts; |
|
|
|
|
|
|
|
//const shortIndexes = searchIndex.shortIndexes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(this.options) { |
|
|
|
|
|
|
|
query = processSearchText(query, this.options); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newFoundObjs: Array<{fullText: string, fullTextLength: number, what: SearchWhat, foundChars: number}> = []; |
|
|
|
const newFoundObjs: Array<{fullText: string, fullTextLength: number, what: SearchWhat, foundChars: number}> = []; |
|
|
|
|
|
|
|
const fullTexts = this.fullTexts; |
|
|
|
const queryWords = query.split(' '); |
|
|
|
const queryWords = query.split(' '); |
|
|
|
const queryWordsLength = queryWords.length; |
|
|
|
const queryWordsLength = queryWords.length; |
|
|
|
fullTexts.forEach((fullText, what) => { |
|
|
|
fullTexts.forEach((fullText, what) => { |
|
|
@ -66,7 +61,7 @@ export default class SearchIndex<SearchWhat> { |
|
|
|
for(let i = 0; i < queryWordsLength; ++i) { // * verify that all words are found
|
|
|
|
for(let i = 0; i < queryWordsLength; ++i) { // * verify that all words are found
|
|
|
|
const word = queryWords[i]; |
|
|
|
const word = queryWords[i]; |
|
|
|
const idx = fullText.indexOf(word); |
|
|
|
const idx = fullText.indexOf(word); |
|
|
|
if(idx === -1 || (idx !== 0 && fullText[idx - 1] !== ' ')) { // * search only from word beginning
|
|
|
|
if(idx === -1 || (idx !== 0 && fullText[idx - 1] !== ' '/* && !badCharsRe.test(fullText[idx - 1]) */)) { // * search only from word beginning
|
|
|
|
found = false; |
|
|
|
found = false; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -83,39 +78,21 @@ export default class SearchIndex<SearchWhat> { |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
newFoundObjs.sort((a, b) => a.fullTextLength - b.fullTextLength || b.foundChars - a.foundChars); |
|
|
|
return newFoundObjs; |
|
|
|
|
|
|
|
} |
|
|
|
//newFoundObjs.sort((a, b) => a.fullText.localeCompare(b.fullText));
|
|
|
|
|
|
|
|
const newFoundObjs2: Set<SearchWhat> = new Set(newFoundObjs.map(o => o.what)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* const queryWords = query.split(' '); |
|
|
|
public search(query: string) { |
|
|
|
let foundArr: number[]; |
|
|
|
if(this.options) { |
|
|
|
for(let i = 0; i < queryWords.length; i++) { |
|
|
|
query = processSearchText(query, this.options); |
|
|
|
const newFound = shortIndexes[queryWords[i].substr(0, 3)]; |
|
|
|
|
|
|
|
if(!newFound) { |
|
|
|
|
|
|
|
foundArr = []; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(foundArr === undefined || foundArr.length > newFound.length) { |
|
|
|
|
|
|
|
foundArr = newFound; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for(let j = 0; j < foundArr.length; j++) { |
|
|
|
const queries = query.split('\x01'); |
|
|
|
let found = true; |
|
|
|
const newFoundObjs = flatten(queries.map(query => this._search(query))); |
|
|
|
let searchText = fullTexts[foundArr[j]]; |
|
|
|
|
|
|
|
for(let i = 0; i < queryWords.length; i++) { |
|
|
|
|
|
|
|
if(searchText.indexOf(queryWords[i]) === -1) { |
|
|
|
|
|
|
|
found = false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(found) { |
|
|
|
newFoundObjs.sort((a, b) => a.fullTextLength - b.fullTextLength || b.foundChars - a.foundChars); |
|
|
|
newFoundObjs[foundArr[j]] = true; |
|
|
|
|
|
|
|
} |
|
|
|
//newFoundObjs.sort((a, b) => a.fullText.localeCompare(b.fullText));
|
|
|
|
} */ |
|
|
|
const newFoundObjs2: Set<SearchWhat> = new Set(newFoundObjs.map(o => o.what)); |
|
|
|
|
|
|
|
|
|
|
|
return newFoundObjs2; |
|
|
|
return newFoundObjs2; |
|
|
|
} |
|
|
|
} |
|
|
|