-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update getIndices to support cross-cluster search.
- Loading branch information
Showing
3 changed files
with
46 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |