Skip to content

Commit

Permalink
adjust Lens title and do not match title if Visualize is hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Sep 22, 2020
1 parent 4360f31 commit 54d2d9c
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions x-pack/plugins/lens/public/search_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import levenshtein from 'js-levenshtein';
import { ApplicationStart } from 'kibana/public';
import { from, EMPTY } from 'rxjs';
import { from } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public';
import { GlobalSearchResultProvider } from '../../global_search/public';
import { getFullPath } from '../common';
Expand All @@ -26,49 +27,53 @@ export const getSearchProvider: (
) => GlobalSearchResultProvider = (uiCapabilities) => ({
id: 'lens',
find: (term) => {
const title = 'Visualize: Lens';
const searchableTitle = title.toLowerCase();
return from(
uiCapabilities.then(({ navLinks: { visualize: visualizeNavLink } }) => {
if (!visualizeNavLink) {
return [];
}
const title = i18n.translate('xpack.lens.searchTitle', {
defaultMessage: 'Lens: create visualizations',
description: 'Lens is a product name and should not be translated',
});
const searchableTitle = title.toLowerCase();

term = term.toLowerCase();
let score = 0;
term = term.toLowerCase();
let score = 0;

// shortcuts to avoid calculating the distance when there is an exact match somewhere.
if (searchableTitle === term) {
score = 100;
} else if (searchableTitle.startsWith(term)) {
score = 90;
} else if (searchableTitle.includes(term)) {
score = 75;
} else {
const length = Math.max(term.length, searchableTitle.length);
const distance = levenshtein(term, searchableTitle);
// shortcuts to avoid calculating the distance when there is an exact match somewhere.
if (searchableTitle === term) {
score = 100;
} else if (searchableTitle.startsWith(term)) {
score = 90;
} else if (searchableTitle.includes(term)) {
score = 75;
} else {
const length = Math.max(term.length, searchableTitle.length);
const distance = levenshtein(term, searchableTitle);

// maximum lev distance is length, we compute the match ratio (lower distance is better)
const ratio = Math.floor((1 - distance / length) * 100);
if (ratio >= 60) {
score = ratio;
}
}
if (score === 0) return EMPTY;
return from(
uiCapabilities.then(({ navLinks: { visualize: visualizeNavLink } }) =>
visualizeNavLink
? [
{
id: 'lens',
title,
type: 'application',
icon: 'logoKibana',
meta: {
categoryId: DEFAULT_APP_CATEGORIES.kibana.id,
categoryLabel: DEFAULT_APP_CATEGORIES.kibana.label,
},
score,
url: getFullPath(),
},
]
: []
)
// maximum lev distance is length, we compute the match ratio (lower distance is better)
const ratio = Math.floor((1 - distance / length) * 100);
if (ratio >= 60) {
score = ratio;
}
}
if (score === 0) return [];
return [
{
id: 'lens',
title,
type: 'application',
icon: 'logoKibana',
meta: {
categoryId: DEFAULT_APP_CATEGORIES.kibana.id,
categoryLabel: DEFAULT_APP_CATEGORIES.kibana.label,
},
score,
url: getFullPath(),
},
];
})
);
},
});

0 comments on commit 54d2d9c

Please sign in to comment.