Skip to content

Commit

Permalink
Move FlexSearch construction and search codes
Browse files Browse the repository at this point in the history
  • Loading branch information
smori1983 committed Sep 20, 2023
1 parent 4d583b8 commit 685f5ed
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
48 changes: 7 additions & 41 deletions src/components/SearchBoxFlexSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<script>
import { Document } from 'flexsearch'
import data from '@dynamic/vuepress-plugin-flexsearch/data'
import Database from './database';
import excerpt from './excerpt'
import matcher from './matcher';
import ngram from '../tokenizer/ngram'
/* global FLEX_SEARCH_HOTKEYS */
Expand All @@ -66,8 +66,7 @@ export default {
data () {
return {
/** @type {Map<string, Document>} */
docs: new Map(),
database: new Database(),
query: '',
focused: false,
Expand Down Expand Up @@ -105,21 +104,10 @@ export default {
}
const localePath = this.$localePath;
if (!this.docs.has(localePath)) {
return [];
}
const queryForSearch = ngram.createForSearch(query, FLEX_SEARCH_NGRAM_SIZE);
const max = this.$site.themeConfig.searchMaxSuggestions || FLEX_SEARCH_MAX_SUGGESTIONS;
/**
* @type {string[]}
*/
const matchedKeys = this.docs.get(localePath).search(queryForSearch, {
pluck: 'dataForSearch',
limit: max,
});
const matchedKeys = this.database.search(localePath, queryForSearch, max);
return matchedKeys.map((key) => {
const page = data[localePath][key];
Expand Down Expand Up @@ -149,39 +137,17 @@ export default {
window.addEventListener('resize', this.resizeSuggestionsBox);
document.addEventListener('keydown', this.onHotkey);
this.setUpFlexSearchDocument();
for (const locale in data) {
// Consider the case locale defined but no pages created.
this.database.add(locale, data[locale] || {});
}
},
beforeDestroy () {
document.removeEventListener('keydown', this.onHotkey);
},
methods: {
setUpFlexSearchDocument () {
for (const locale in data) {
const doc = new Document({
matcher: matcher,
tokenize: 'forward',
id: 'key',
index: [
'dataForSearch',
],
});
// Consider the case locale defined but no pages created.
const localeData = data[locale] || {};
for (const key in localeData) {
doc.add({
key: key,
dataForSearch: localeData[key].dataForSearch,
});
}
this.docs.set(locale, doc);
}
},
resizeSuggestionsBox () {
if (this.$refs.suggestions) {
this.$refs.suggestions.style.maxHeight = (window.innerHeight - 100) + 'px';
Expand Down
58 changes: 58 additions & 0 deletions src/components/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { Document } = require('flexsearch');

const matcher = require('./matcher');

class Database {
constructor() {
/**
* Locale path specific FlexSearch document.
*
* @type {Map<string, Document>}
* @private
*/
this._docs = new Map();
}

/**
* @param {string} localePath
* @param {Object.<string, {dataForSearch: string}>} localeData
*/
add(localePath, localeData) {
if (!this._docs.has(localePath)) {
this._docs.set(localePath, new Document({
matcher: matcher,
tokenize: 'forward',
id: 'key',
index: [
'content',
],
}));
}

for (const key in localeData) {
this._docs.get(localePath).add({
key: key,
content: localeData[key].dataForSearch,
});
}
}

/**
* @param {string} localePath
* @param {string} query
* @param {number} limit
* @return {string[]} page key list.
*/
search(localePath, query, limit) {
if (!this._docs.has(localePath)) {
return [];
}

return this._docs.get(localePath).search(query, {
pluck: 'content',
limit: limit,
});
}
}

module.exports = Database;

0 comments on commit 685f5ed

Please sign in to comment.