Skip to content

Commit

Permalink
Merge pull request #147 from alan-wu/improve-search
Browse files Browse the repository at this point in the history
Improve searching, making it consistent with flatmapvuer.
  • Loading branch information
alan-wu authored Oct 31, 2024
2 parents 8807d5d + f4f793d commit e1105fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export default {
}
cb(
this.$refs.scaffold.fetchSuggestions(term).map((item) => {
const value = item.terms.length > 1 ? item.terms[1] : item.terms[0];
const value = item.suggestion;
return {
value: value,
label: value
Expand Down
130 changes: 60 additions & 70 deletions src/scripts/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,47 @@ export class SearchIndex
this._searchEngine = new MiniSearch({
fields: ['path', 'name'],
storeFields: ['path'],
tokenize: (string, _fieldName) => string.split('"'), // indexing tokenizer
tokenize: (string, _fieldName) => string.split(/[\s/]+/), // indexing tokenizer
});
this._featureIds = [];
this.zincObjects = [];
this.regions = [];
}

indexMetadata(featureId, metadata)
//================================
{
const textSeen = [];
for (const prop of indexedProperties) {
if (prop in metadata) {
const text = metadata[prop];
if (!textSeen.includes(text)) {
this.addTerm_(featureId, text);
textSeen.push(text);
}
}
}
this.idMaps = {};
}

addZincObject(zincObject, id)
//=======================
{
const path = zincObject.getRegion().getFullPath();
const fullPath = path ? `${path}/${zincObject.groupName}` : zincObject.groupName;
const item = { path: fullPath, name: zincObject.groupName, id };
this._searchEngine.add(item, {fields: ['path', 'name']});
this.zincObjects.push(zincObject);
let groupName = zincObject.groupName;
let fullPath = path ? `${path}/${zincObject.groupName}` : zincObject.groupName;
groupName = groupName.replaceAll('"', '');
fullPath = fullPath.replaceAll('"', '');
const item = { path: fullPath, name: groupName, id };
this._searchEngine.add(item);
this.idMaps[id] = { path: fullPath, zincObject };
}

removeZincObject(zincObject, id)
//=======================
{
const path = zincObject.getRegion().getFullPath();
const fullPath = path ? `${path}/${zincObject.groupName}` : zincObject.groupName;
const item = { path: fullPath, name: zincObject.groupName, id };
this._searchEngine.remove(item, {fields: ['path', 'name']});
for (let i = 0; i < this.zincObjects.length; i++) {
if (id === this.zincObjects[i].uuid) {
this.zincObjects.splice(i, 1);
return;
}
}

let groupName = zincObject.groupName;
let fullPath = path ? `${path}/${zincObject.groupName}` : zincObject.groupName;
groupName = groupName.replaceAll('"', '');
fullPath = fullPath.replaceAll('"', '');
const item = { path: fullPath, name: groupName, id };
this._searchEngine.remove(item);
delete this.idMaps[id];
}

addRegion(region, id)
//=======================
{
const item = { path: region.getFullPath(), name: region.getName(), id };
this._searchEngine.add(item, {fields: ['path', 'name']});
this.regions.push(region);
let path = region.getFullPath();
let regionName = region.getName();
path = path.replaceAll('"', '');
regionName = regionName.replaceAll('"', '');
const item = { path, name: regionName, id };
this._searchEngine.add(item);
this.idMaps[id] = { path, zincObject: region };
}

clearResults()
Expand All @@ -110,17 +97,32 @@ export class SearchIndex
//=======================
{
this._searchEngine.removeAll();
this.zincObjects.length = 0;
this.regions.length = 0;
//this.zincObjects.length = 0;
//this.regions.length = 0;
this.idMaps = {};
}

auto_suggest(text)
//================
{
const results = this._searchEngine.autoSuggest(text, {prefix: true});
return results;
auto_suggest(text) {
let results = [];
if (text.length > 2 && ["'", '"'].includes(text.slice(0, 1))) {
text = text.replaceAll(text.slice(0, 1), '')
results = this._searchEngine.search(text, {prefix: true, combineWith: 'AND'})
} else if (text.length > 1) {
results = this._searchEngine.search(text, {prefix: true})
}
const items = [];
results.forEach(r => {
if (r.id in this.idMaps) {
items.push(this.idMaps[r.id].path);
}
});
const unique = [...new Set(items)];
const returned = [];
unique.forEach(u => returned.push({suggestion: '"' + u + '"'}));
return returned;
}


processResults(zincObjects, searchText) {
const result = {
regionPath: undefined,
Expand All @@ -145,11 +147,21 @@ export class SearchIndex
}

search(text) {
const results = this._searchEngine.search(text, {prefix: true});
const zincResults = this.zincObjects.filter(zincObject => results.map(r => r.id).includes(zincObject.uuid));
const regionResults = this.regions.filter(region => results.map(r => r.id).includes(region.uuid));
zincResults.push(...regionResults);
return zincResults;
let results = undefined;
if (text.length > 2 && ["'", '"'].includes(text.slice(0, 1))) {
text = text.replaceAll(text.slice(0, 1), '')
console.log(text)
results = this._searchEngine.search(text, {prefix: true, combineWith: 'AND'})
} else if (text.length > 1) {
results = this._searchEngine.search(text, {prefix: true})
}
const zincResults = [];
results.forEach(r => {
if (r.id in this.idMaps) {
zincResults.push(this.idMaps[r.id].zincObject);
}
});
return zincResults;
}

searchTerms(terms) {
Expand All @@ -174,25 +186,3 @@ export class SearchIndex

}

//==============================================================================

class SearchResults
{
constructor(results)
{
this.__results = results.sort((a, b) => (b.score - a.score));
this.__featureIds = results.map(r => r.featureId);
}

get featureIds()
{
return this.__featureIds;
}

get results()
{
return this.__results;
}
}

//==============================================================================

0 comments on commit e1105fe

Please sign in to comment.