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

Fix KQL autocomplete value suggestions #78676

Merged
merged 1 commit into from
Sep 30, 2020
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export function SuggestionComponent(props: Props) {
<div className="kbnSuggestionItem__type">
<EuiIcon type={getEuiIconType(props.suggestion.type)} />
</div>
<div className="kbnSuggestionItem__text">{props.suggestion.text}</div>
<div className="kbnSuggestionItem__text" data-test-subj="autoCompleteSuggestionText">
{props.suggestion.text}
</div>
{props.shouldDisplayDescription && (
<div className="kbnSuggestionItem__description">{props.suggestion.description}</div>
)}
Expand Down
5 changes: 5 additions & 0 deletions test/functional/services/query_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export function QueryBarProvider({ getService, getPageObjects }: FtrProviderCont
const queryLanguageButton = await testSubjects.find('switchQueryLanguageButton');
expect((await queryLanguageButton.getVisibleText()).toLowerCase()).to.eql(lang);
}

public async getSuggestions() {
const suggestions = await testSubjects.findAll('autoCompleteSuggestionText');
return Promise.all(suggestions.map((suggestion) => suggestion.getVisibleText()));
}
}

return new QueryBar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { escapeQuotes } from './lib/escape_kuery';
import { KqlQuerySuggestionProvider } from './types';
import { getAutocompleteService } from '../../../services';
import {
IFieldType,
IIndexPattern,
QuerySuggestion,
QuerySuggestionTypes,
} from '../../../../../../../src/plugins/data/public';
Expand All @@ -23,29 +25,27 @@ const wrapAsSuggestions = (start: number, end: number, query: string, values: st
end,
}));

export const setupGetValueSuggestions: KqlQuerySuggestionProvider = (core) => {
export const setupGetValueSuggestions: KqlQuerySuggestionProvider = () => {
return async (
{ indexPatterns, boolFilter, signal },
{ start, end, prefix, suffix, fieldName, nestedPath }
): Promise<QuerySuggestion[]> => {
const allFields = flatten(
indexPatterns.map((indexPattern) =>
indexPattern.fields.map((field) => ({
...field,
indexPattern,
}))
)
);

const fullFieldName = nestedPath ? `${nestedPath}.${fieldName}` : fieldName;
const fields = allFields.filter((field) => field.name === fullFieldName);

const indexPatternFieldEntries: Array<[IIndexPattern, IFieldType]> = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to make sure these are the correct types. They're necessary if someone is passing in js objects instead of classes instances but otherwise we should use IndexPattern and IndexPatternField

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could add that in a follow-up PR I think that'd be ideal since it will also include some refactoring of all the suggestors (value, conjunction, fields, etc.)

indexPatterns.forEach((indexPattern) => {
indexPattern.fields
.filter((field) => field.name === fullFieldName)
.forEach((field) => indexPatternFieldEntries.push([indexPattern, field]));
});

const query = `${prefix}${suffix}`.trim();
const { getValueSuggestions } = getAutocompleteService();

const data = await Promise.all(
fields.map((field) =>
indexPatternFieldEntries.map(([indexPattern, field]) =>
getValueSuggestions({
indexPattern: field.indexPattern,
indexPattern,
field,
query,
boolFilter,
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/functional/apps/discover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./async_scripted_fields'));
loadTestFile(require.resolve('./reporting'));
loadTestFile(require.resolve('./error_handling'));
loadTestFile(require.resolve('./value_suggestions'));
});
}
33 changes: 33 additions & 0 deletions x-pack/test/functional/apps/discover/value_suggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ getService, getPageObjects }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const queryBar = getService('queryBar');
const PageObjects = getPageObjects(['common']);

describe('value suggestions', function describeIndexTests() {
before(async function () {
await esArchiver.loadIfNeeded('logstash_functional');
await esArchiver.load('dashboard/drilldowns');
await PageObjects.common.navigateToApp('discover');
});

after(async () => {
await esArchiver.unload('dashboard/drilldowns');
});

it('show up', async () => {
await queryBar.setQuery('extension.raw : ');
const suggestions = await queryBar.getSuggestions();
expect(suggestions.length).to.be(5);
expect(suggestions).to.contain('"jpg"');
});
});
}