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