Skip to content

Commit

Permalink
remote indices support
Browse files Browse the repository at this point in the history
Signed-off-by: Seth Muthukaruppan <[email protected]>
  • Loading branch information
smuthukaruppannp authored and AWSHurneyt committed Jan 30, 2024
1 parent 7cef11c commit 65264e7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export function filterSystemIndices(indices, isIncludingSystemIndices) {
const acceptableIndices = isIncludingSystemIndices
? indices
: // All system indices begin with a period.
indices.filter((index) => !index.label.startsWith('.'));
indices.filter(
(index) =>
!index.label.startsWith('.') &&
(!index.label.includes(':') || !index.label.split(':')[1].startsWith('.'))
);

return acceptableIndices.slice(0, MAX_NUMBER_OF_MATCHING_INDICES);
}
Expand Down
85 changes: 73 additions & 12 deletions server/services/OpensearchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import _ from 'lodash';

export default class OpensearchService {
constructor(esDriver) {
this.esDriver = esDriver;
Expand Down Expand Up @@ -37,11 +39,24 @@ export default class OpensearchService {
try {
const { index } = req.body;
const { callAsCurrentUser } = this.esDriver.asScoped(req);
const indices = await callAsCurrentUser('cat.indices', {
index,
format: 'json',
h: 'health,index,status',
});
let indices = [];
if (index.includes(':')) {
const resolve_resp = await callAsCurrentUser('transport.request', {
method: 'GET',
path: '/_resolve/index/' + index,
});
indices = resolve_resp.indices.map((item) => ({
index: item.name,
status: item.attributes[0],
health: 'undefined',
}));
} else {
indices = await callAsCurrentUser('cat.indices', {
index,
format: 'json',
h: 'health,index,status',
});
}
return res.ok({
body: {
ok: true,
Expand Down Expand Up @@ -73,11 +88,22 @@ export default class OpensearchService {
try {
const { alias } = req.body;
const { callAsCurrentUser } = this.esDriver.asScoped(req);
const aliases = await callAsCurrentUser('cat.aliases', {
alias,
format: 'json',
h: 'alias,index',
});
let aliases = [];
if (alias.includes(':')) {
const resolve_resp = await callAsCurrentUser('transport.request', {
method: 'GET',
path: '/_resolve/index/' + alias,
});
aliases = resolve_resp.aliases.flatMap((alias_item) =>
alias_item.indices.map((index) => ({ alias: alias_item.name, index: index }))
);
} else {
aliases = await callAsCurrentUser('cat.aliases', {
alias,
format: 'json',
h: 'alias,index',
});
}
return res.ok({
body: {
ok: true,
Expand All @@ -99,11 +125,46 @@ export default class OpensearchService {
try {
const { index } = req.body;
const { callAsCurrentUser } = this.esDriver.asScoped(req);
const mappings = await callAsCurrentUser('indices.getMapping', { index });
let local_mappings = {};
let remote_mappings = {};
let local_indices = index.filter((e) => !e.includes(':'));
let remote_indices = index.filter((e) => e.includes(':'));
if (remote_indices.length) {
const fc_resp = await callAsCurrentUser('transport.request', {
method: 'GET',
path: remote_indices.toString() + '/_field_caps?fields=*&include_unmapped',
});
fc_resp.indices.forEach((index_name) => {
remote_mappings[index_name] = {
mappings: {
properties: {},
},
};
});
for (const [k1, v1] of Object.entries(fc_resp.fields)) {
if (k1.startsWith('_')) {
continue;
}
for (const [k2, v2] of Object.entries(v1)) {
if (k2 == 'unmapped') {
continue;
}
let mapped_indices = _.get(v2, 'indices', fc_resp.indices);
mapped_indices.forEach((mapped_index) => {
remote_mappings[mapped_index]['mappings']['properties'][k1] = {
type: v2.type,
};
});
}
}
}
if (local_indices.length) {
local_mappings = await callAsCurrentUser('indices.getMapping', { index });
}
return res.ok({
body: {
ok: true,
resp: mappings,
resp: { ...local_mappings, ...remote_mappings },
},
});
} catch (err) {
Expand Down

0 comments on commit 65264e7

Please sign in to comment.