Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/search/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type Doc = {
tokenizedBox: string,
functionNames: string,
tokenizedFunctionNames: string,
description: string
description: string,
minDescription: string
};

let localIndex;
Expand All @@ -21,19 +22,30 @@ function tokenizeStr(str: string): string {
return str.trim().split(/(?=[A-Z])/).join(' ').toLowerCase().split(/ |_|-/).join(' ');
}

/**
* returns the first sentence of the description.
* @param {string} desc
* @return {string}
*/
function minimizeDescription(desc: string = ''): string {
return desc.split(/\.|;/)[0]; // split by a dot or a semicolon
}

function prepareDoc(docs: Object, component: Component): Doc {
const name = component.name;
const box = component.box;
const functionNames = docs.map(doc => doc.name).join(' ');
return {
id: `${box}_${name}`,
name,
tokenizedName: tokenizeStr(name),
box,
tokenizedNameExtra: tokenizeStr(name), // TODO: remove it when possible
tokenizedName: tokenizeStr(name),
tokenizedBox: tokenizeStr(box),
functionNames,
tokenizedFunctionNames: tokenizeStr(functionNames),
description: docs.map(doc => doc.description).join(' ')
description: docs.map(doc => doc.description).join(' '),
minDescription: docs.map(doc => minimizeDescription(doc.description)).join(' ')
};
}

Expand Down
37 changes: 25 additions & 12 deletions src/search/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import serverlessIndex from './serverless-index';
import indexer from './indexer';
import type Doc from './indexer';

const boost = {
box: 3,
tokenizedBox: 2,
name: 5,
tokenizedName: 4,
functionNames: 2,
tokenizedFunctionNames: 2,
minDescription: 1
};

function totalHits(index: Promise<any>, query: string) {
return new Promise((resolve, reject) => {
return index.then((indexInstance) => {
Expand Down Expand Up @@ -41,23 +51,26 @@ function formatSearchResult(doc: Doc): string {
return `> ${doc.box}/${doc.name}`;
}

function queryItem(field, query, boost = 1) {
return {
AND: { [field]: query.toLowerCase().split(' ') },
BOOST: boost
function queryItem(field, queryStr): Object {
const query = {
AND: { [field]: queryStr.toLowerCase().split(' ') },
BOOST: boost[field],
NOT: {}
};

return query;
}

function buildQuery(queryStr: string) {
function buildQuery(queryStr: string): Array<Object> {
const tokenizedQuery = indexer.tokenizeStr(queryStr);
const query = [];
query.push(queryItem('box', queryStr, 4));
query.push(queryItem('tokenizedBox', queryStr, 3));
query.push(queryItem('name', queryStr, 4));
query.push(queryItem('tokenizedName', tokenizedQuery, 3));
query.push(queryItem('functionNames', queryStr, 3));
query.push(queryItem('tokenizedFunctionNames', tokenizedQuery, 2));
query.push(queryItem('description', queryStr));
query.push(queryItem('box', queryStr));
query.push(queryItem('tokenizedBox', queryStr));
query.push(queryItem('name', queryStr));
query.push(queryItem('tokenizedName', tokenizedQuery));
query.push(queryItem('functionNames', queryStr));
query.push(queryItem('tokenizedFunctionNames', tokenizedQuery));
query.push(queryItem('minDescription', queryStr));
return query;
}

Expand Down
3 changes: 2 additions & 1 deletion src/search/serverless-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function deleteDb(scopePath: string) {
function initializeIndex(scopePath: string): Promise<any> {
const indexOptions = {
indexPath: getIndexPath(scopePath),
logLevel
logLevel,
stopwords: []
};

if (!initializeIndex.index) { // static var to make sure the index is not instantiated twice
Expand Down