Skip to content

Commit

Permalink
fix: resolve inaccurate search results
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthyadms committed Nov 21, 2023
1 parent d8d494a commit 93badd2
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions components/input-search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</template>

<script>
// import { distance } from "fastest-levenshtein";
import distance from "jaro-winkler";
import { distance } from "fastest-levenshtein";
// import distance from "jaro-winkler";
export default {
data() {
Expand Down Expand Up @@ -73,10 +73,11 @@ export default {
});
},
updateResults(section, input) {
const isIncluded = this.results.some(
(result) => result.title === section.title
);
if (isIncluded) return;
// WARNING: this causes the results not to be updated
// const isIncluded = this.results.some(
// (result) => result.title === section.title
// );
// if (isIncluded) return;
const sectionTitle = section.title;
const lowerSectionTitle = sectionTitle.toLowerCase();
const lowerInput = input.toLowerCase();
Expand All @@ -88,13 +89,16 @@ export default {
},
};
this.results.push(result);
this.sortResults();
}
},
sortResults() {
this.results.sort((a, b) => b.distance - a.distance);
// levenstein distance sorting: a - b (ascending)
// jaro-winkler distance sorting: b - a (descending)
this.results.sort((a, b) => a.distance - b.distance);
},
quickSearch(event) {
if (this.results.length > 0) this.sortResults();
this.results = []; // reset results to get new results
const input = event.target.value;
if (input.length > 0) {
this.sections.forEach((section) => {
Expand All @@ -107,8 +111,6 @@ export default {
this.updateResults(section, input);
}
});
} else {
this.results = [];
}
},
},
Expand Down

0 comments on commit 93badd2

Please sign in to comment.