Skip to content

Commit

Permalink
Update getIndices to support cross-cluster search.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Jul 11, 2017
1 parent caaf63d commit a86fddd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ uiModules.get('apps/management')
Private,
Promise
) {
const MAX_NUMBER_OF_MATCHING_INDICES = 3;
const indicesService = $injector.get('indices');
const notify = new Notifier();
const refreshKibanaIndex = Private(RefreshKibanaIndex);
Expand Down Expand Up @@ -92,7 +93,7 @@ uiModules.get('apps/management')

// All system indices begin with a period.
return indices.filter(index => (
index.indexOf('.') !== 0
index.name.indexOf('.') !== 0
));
};

Expand Down Expand Up @@ -123,8 +124,8 @@ uiModules.get('apps/management')
}

const thisFetchMatchingIndicesRequest = mostRecentFetchMatchingIndicesRequest = Promise.all([
indicesService.getIndices(exactSearchQuery),
indicesService.getIndices(partialSearchQuery),
indicesService.getIndices(exactSearchQuery, MAX_NUMBER_OF_MATCHING_INDICES),
indicesService.getIndices(partialSearchQuery, MAX_NUMBER_OF_MATCHING_INDICES),
createReasonableWait(),
])
.then(([
Expand All @@ -137,20 +138,26 @@ uiModules.get('apps/management')
updateWhiteListedIndices();
this.isFetchingMatchingIndices = false;
}
}).catch(error => {
notify.error(error);
});
};

this.fetchExistingIndices = () => {
this.isFetchingExistingIndices = true;
const allExistingLocalAndRemoteIndicesPattern = '*,*:*';

Promise.all([
indicesService.getIndices('*'),
indicesService.getIndices(allExistingLocalAndRemoteIndicesPattern, MAX_NUMBER_OF_MATCHING_INDICES),
createReasonableWait(),
])
.then(([allIndicesResponse]) => {
// Cache all indices.
allIndices = allIndicesResponse.sort();
updateWhiteListedIndices();
this.isFetchingExistingIndices = false;
}).catch(error => {
notify.error(error);
});
};

Expand Down Expand Up @@ -226,8 +233,8 @@ uiModules.get('apps/management')
.then(([fields]) => {
this.timeFieldOptions = extractTimeFieldsFromFields(fields);
})
.catch(err => {
notify.error(err);
.catch(error => {
notify.error(error);
})
.finally(() => {
this.isFetchingTimeFieldOptions = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
ng-repeat="index in matchingIndicesList.pageOfIndices"
>
<p class="kuiText">
{{index}}
{{index.name}}
</p>
</li>
</ul>
Expand Down
57 changes: 32 additions & 25 deletions src/ui/public/indices/get_indices.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { pluck, reduce, size } from 'lodash';

export function IndicesGetIndicesProvider(esAdmin) {
const getIndexNamesFromAliasesResponse = json => {
// Assume this function won't be called in the event of a 404.
return reduce(json, (list, { aliases }, indexName) => {
list.push(indexName);
if (size(aliases) > 0) {
list.push(...Object.keys(aliases));
}
return list;
}, []);
};

const getIndexNamesFromIndicesResponse = json => {
if (json.status === 404) {
return [];
return async function getIndices(indexPattern, maxNumberOfMatchingIndices) {
if (!indexPattern) {
throw new Error('Please provide an indexPattern string to getIndices().');
}

return pluck(json, 'index');
};
if (!maxNumberOfMatchingIndices || maxNumberOfMatchingIndices < 0) {
throw new Error('Please provide a maxNumberOfMatchingIndices value greater than 0 to getIndices().');
}

return async function getIndices(query) {
const aliases = await esAdmin.indices.getAlias({ index: query, allowNoIndices: true, ignore: 404 });
const result = await esAdmin.search({
index: indexPattern,
ignore: [404],
body: {
size: 0, // no hits
aggs: {
indices: {
terms: {
field: '_index',
size: maxNumberOfMatchingIndices,
}
}
}
}
});

// If aliases return 200, they'll include matching indices, too.
if (aliases.status === 404) {
const indices = await esAdmin.cat.indices({ index: query, format: 'json', ignore: 404 });
return getIndexNamesFromIndicesResponse(indices);
if (
result.status === 404
|| !result.aggregations
) {
return [];
}

return getIndexNamesFromAliasesResponse(aliases);
const indices = result.aggregations.indices.buckets.map(bucket => ({
name: bucket.key,
docCount: bucket.doc_count,
}));

return indices;
};
}

0 comments on commit a86fddd

Please sign in to comment.