mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
feat: update to of elasticsearch 8.4
This commit is contained in:
committed by
Rainer Killinger
parent
515a6eeea5
commit
c9b83b5d71
63
src/storage/elasticsearch/util/index.ts
Normal file
63
src/storage/elasticsearch/util/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {SCBulkResponse, SCThingType, SCUuid} from '@openstapps/core';
|
||||
|
||||
/**
|
||||
* Length of the index UID used for generation of its name
|
||||
*/
|
||||
export const INDEX_UID_LENGTH = 8;
|
||||
|
||||
/**
|
||||
* A string which matches all indices
|
||||
*/
|
||||
export const ALL_INDICES_QUERY = 'stapps_*_*_*';
|
||||
|
||||
/**
|
||||
* Matches index names such as stapps_<type>_<source>_<random suffix>
|
||||
*/
|
||||
export const VALID_INDEX_REGEX = /^stapps_([A-z0-9_]+)_([a-z0-9-_]+)_([-a-z0-9^_]+)$/;
|
||||
|
||||
export interface ParsedIndexName {
|
||||
type: SCThingType;
|
||||
source: string;
|
||||
randomSuffix: string;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export function parseIndexName(index: string): ParsedIndexName {
|
||||
const match = VALID_INDEX_REGEX.exec(index);
|
||||
if (!match) {
|
||||
throw new SyntaxError(`Invalid index name ${index}!`);
|
||||
}
|
||||
|
||||
return {
|
||||
type: match[1] as SCThingType,
|
||||
source: match[2],
|
||||
randomSuffix: match[3],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index name in elasticsearch for one SCThingType
|
||||
*
|
||||
* @param type SCThingType of data in the index
|
||||
* @param source source of data in the index
|
||||
* @param bulk bulk process which created this index
|
||||
*/
|
||||
export function getThingIndexName(type: SCThingType, source: string, bulk: SCBulkResponse) {
|
||||
let out = type.toLowerCase();
|
||||
while (out.includes(' ')) {
|
||||
out = out.replace(' ', '_');
|
||||
}
|
||||
|
||||
return `stapps_${out}_${source}_${getIndexUID(bulk.uid)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the index UID (for its name) from the bulk UID
|
||||
*
|
||||
* @param uid Bulk UID
|
||||
*/
|
||||
export function getIndexUID(uid: SCUuid) {
|
||||
return uid.slice(0, Math.max(0, INDEX_UID_LENGTH));
|
||||
}
|
||||
Reference in New Issue
Block a user