Skip to content

Commit

Permalink
Add bool filter to suggestion endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed May 28, 2018
1 parent cfbaf9d commit 23b8631
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export function registerValueSuggestions(server) {
method: ['POST'],
handler: async function (req, reply) {
const { index } = req.params;
const { field, query } = req.payload;
const { field, query, boolFilter } = req.payload;
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const body = getBody({ field, query });
const body = getBody({ field, query, boolFilter });
try {
const response = await callWithRequest(req, 'search', { index, body });
const buckets = get(response, 'aggregations.suggestions.buckets') || [];
Expand All @@ -22,7 +22,7 @@ export function registerValueSuggestions(server) {
});
}

function getBody({ field, query }) {
function getBody({ field, query, boolFilter }) {
// Helps ensure that the regex is not evaluated eagerly against the terms dictionary
const executionHint = 'map';

Expand All @@ -38,6 +38,11 @@ function getBody({ field, query }) {
size: 0,
timeout: '1s',
terminate_after: terminateAfter,
query: {
bool: {
filter: boolFilter,
}
},
aggs: {
suggestions: {
terms: {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/kuery/suggestions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { getSuggestionsProvider as conjunction } from './conjunction';

const cursorSymbol = '@kuery-cursor@';

export function getSuggestionsProvider({ config, indexPatterns }) {
export function getSuggestionsProvider({ config, indexPatterns, boolFilter }) {
const getSuggestionsByType = mapValues({ field, value, operator, conjunction }, provider => {
return provider({ config, indexPatterns });
return provider({ config, indexPatterns, boolFilter });
});

return function getSuggestions({ query, selectionStart, selectionEnd }) {
Expand Down
12 changes: 6 additions & 6 deletions src/ui/public/kuery/suggestions/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { kfetch } from '../../kfetch';

const type = 'value';

const requestSuggestions = memoize((query, field) => {
const requestSuggestions = memoize((query, field, boolFilter) => {
return kfetch({
pathname: `/api/kibana/suggestions/values/${field.indexPatternTitle}`,
method: 'POST',
body: JSON.stringify({ query, field: field.name }),
body: JSON.stringify({ query, field: field.name, boolFilter }),
});
}, resolver);

export function getSuggestionsProvider({ config, indexPatterns }) {
export function getSuggestionsProvider({ config, indexPatterns, boolFilter }) {
const allFields = flatten(
indexPatterns.map(indexPattern => {
return indexPattern.fields.map(field => ({
Expand Down Expand Up @@ -45,7 +45,7 @@ export function getSuggestionsProvider({ config, indexPatterns }) {
return [];
}

return requestSuggestions(query, field).then(data => {
return requestSuggestions(query, field, boolFilter).then(data => {
const quotedValues = data.map(value => `"${escapeQuotes(value)}"`);
return wrapAsSuggestions(start, end, query, quotedValues);
});
Expand All @@ -66,8 +66,8 @@ function wrapAsSuggestions(start, end, query, values) {
});
}

function resolver(query, field) {
function resolver(query, field, boolFilter) {
// Only cache results for a minute
const ttl = Math.floor(Date.now() / 1000 / 60);
return [ttl, query, field.indexPatternTitle, field.name].join('|');
return [ttl, query, field.indexPatternTitle, field.name, JSON.stringify(boolFilter)].join('|');
}

0 comments on commit 23b8631

Please sign in to comment.