Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flexsearch #393

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/.vuepress/theme/components/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@mousedown="go(i)"
@mouseenter="focus(i)"
>
<a :href="s.path + s.slug" @click.prevent>
<a :href="s.page.path + s.slug" @click.prevent>
<!-- <div
v-if="s.parentPageTitle"
class="parent-page-title"
Expand All @@ -49,8 +49,8 @@
class="page-title"
v-html="
s.match == 'title'
? highlight(s.title || s.path)
: s.title || s.path
? highlight(s.page.title || s.page.path)
: s.page.title || s.page.path
"
></div>
<div class="suggestion-content">
Expand Down Expand Up @@ -217,7 +217,7 @@ export default {
if (!this.showSuggestions) {
return;
}
this.$router.push(this.suggestions[i].path + this.suggestions[i].slug);
this.$router.push(this.suggestions[i].page.path + this.suggestions[i].slug);
this.query = "";
this.$refs.input.blur();
this.focusIndex = 0;
Expand Down
128 changes: 71 additions & 57 deletions docs/.vuepress/theme/util/flexsearch-service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Flexsearch from "flexsearch";
import { Document } from "flexsearch";
// Use when flexSearch v0.7.0 will be available
// import cyrillicCharset from 'flexsearch/dist/lang/cyrillic/default.min.js'
// import cjkCharset from 'flexsearch/dist/lang/cjk/default.min.js'
import _ from "lodash";
import FlexSearch from "flexsearch";

const defaultLang = "en-US";

Expand All @@ -16,37 +16,26 @@ const cjkRegex = /[\u3131-\u314e|\u314f-\u3163|\uac00-\ud7a3]|[\u4E00-\u9FCC\u34

export default {
buildIndex(pages) {

const indexSettings = {
async: true,
doc: {
id: "key",
// fields we want to index
field: ["title", "keywords", "headersStr", "content"]
// fields to be stored (acts as explicit allow list; `page` object otherwise stored)
// store: [
// "key",
// "title",
// "headers",
// "headersStr",
// "content",
// "contentLowercase",
// "path",
// "lang",
// "docSetHandle",
// "docSetTitle",
// "isPrimary",
// "version"
// ]
optimize: true,
minlength: 3,
document: {
id: "path",
index: ["title", "keywords", "headersStr", "content"]
}
};

const globalIndex = new Flexsearch(indexSettings);
globalIndex.add(
pages.filter(page => {
// default language, primary set, and primary version (designated or versionless set)
return page.lang === defaultLang && page.isPrimary;
})
);
let pagesToAdd = pages.filter(page => {
// default language, primary set, and primary version (designated or versionless set)
return page.lang === defaultLang && page.isPrimary;
});

const globalIndex = new Document(indexSettings);

pagesToAdd.forEach(page => {
globalIndex.add(page)
});

indexes["global"] = globalIndex;

Expand Down Expand Up @@ -90,7 +79,7 @@ export default {
const language = languages[k];
let currentIndexSettings = indexSettings;

const setIndex = new FlexSearch(currentIndexSettings);
const setIndex = new Document(currentIndexSettings);
const setKey = `${docSet}|${version}|${language}`;
const setPages = pages.filter(page => {
return (
Expand All @@ -101,31 +90,42 @@ export default {
});

//console.log(setKey, setPages);
setIndex.add(setPages);
setPages.forEach(page => {
setIndex.add(page)
});

indexes[setKey] = setIndex;

const cyrillicSetPages = setPages.filter(p => p.charsets.cyrillic);
const cjkSetPages = setPages.filter(p => p.charsets.cjk);

if (cyrillicSetPages.length) {
const setCyrillicIndex = new Flexsearch({
const setCyrillicIndex = new Document({
...indexSettings,
encode: false,
split: /\s+/,
tokenize: "forward"
});
setCyrillicIndex.add(cyrillicPages);

cyrillicPages.forEach(page => {
setCyrillicIndex.add(page)
});

cyrillicIndexes[setKey] = setCyrillicIndex;
}
if (cjkSetPages.length) {
const setCjkIndex = new Flexsearch({
const setCjkIndex = new Document({
...indexSettings,
encode: false,
tokenize: function(str) {
return str.replace(/[\x00-\x7F]/g, "").split("");
}
});
setCjkIndex.add(cjkSetPages);

cjkSetPages.forEach(page => {
setCjkIndex.add(page)
});

cjkIndexes[setKey] = setCjkIndex;
}
}
Expand All @@ -137,7 +137,7 @@ export default {

let currentIndexSettings = indexSettings;

const setIndex = new FlexSearch(currentIndexSettings);
const setIndex = new Document(currentIndexSettings);
const setKey = `${docSet}|${language}`;
const setPages = pages.filter(page => {
return page.docSetHandle === docSet && page.lang === language;
Expand All @@ -162,55 +162,61 @@ export default {
);
const cjkIndex = resolveSearchIndex(docSet, version, language, cjkIndexes);

const searchParams = [
{
const searchOptions = {
index: [{
field: "keywords",
query: queryString,
boost: 8,
suggest: false,
bool: "or",
},
{
field: "title",
query: queryString,
boost: 10,
suggest: false,
bool: "or",
},
{
field: "headersStr",
query: queryString,
boost: 7,
suggest: false,
bool: "or",
},
{
field: "content",
query: queryString,
boost: 0,
suggest: false,
bool: "or",
}
];
const searchResult1 = await index.search(searchParams, limit);
}]
};

const searchResult1 = await index.search(queryString, limit, searchOptions);
const searchResult2 = cyrillicIndex
? await cyrillicIndex.search(searchParams, limit)
? await cyrillicIndex.search(queryString, limit, searchOptions)
: [];
const searchResult3 = cjkIndex
? await cjkIndex.search(searchParams, limit)
? await cjkIndex.search(queryString, limit, searchOptions)
: [];
const searchResult = _.uniqBy(
const combinedResults = _.uniqBy(
[...searchResult1, ...searchResult2, ...searchResult3],
"path"
);

const result = searchResult.map(page => ({
...page,
parentPageTitle: getParentPageTitle(page),
...getAdditionalInfo(page, queryString, queryTerms)
}));
let searchResult = [];

combinedResults.forEach(resultSet => {
resultSet.result.forEach(path => {
let page = getPageFromPath(path)
if (page) {
searchResult.push({
docSetTitle: page.docSetTitle,
match: resultSet.field,
page: page,
parentPageTitle: getParentPageTitle(page),
...getAdditionalInfo(page, queryString, queryTerms)
});
}
});
});

const resultBySet = _.groupBy(searchResult, "docSetTitle");

const resultBySet = _.groupBy(result, "docSetTitle");
//console.log(resultBySet);

return _.values(resultBySet)
.map(arr =>
Expand Down Expand Up @@ -442,3 +448,11 @@ function getBeginningContent(page) {

return getContentStr(page, { charIndex: 0, termLength: 0 });
}

function getPageFromPath(path) {
if (path in pagesByPath) {
return pagesByPath[path];
}

return null;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"test": "node ./node_modules/.bin/jest"
},
"dependencies": {
"flexsearch": "^0.6.32",
"flexsearch": "^0.7.2",
"html-to-text": "^7.1.1"
}
}