EZQMS-106: add elastic search by refs support (#3629)

Signed-off-by: Anna No <anna.no@xored.com>
This commit is contained in:
Anna No 2023-08-24 21:00:32 +07:00 committed by GitHub
parent 2235310f91
commit 98a43a98bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 2 deletions

View File

@ -94,7 +94,15 @@ export interface Type<T extends PropertyType> extends UXObject {}
* @public
*/
export enum IndexKind {
/**
* Attribute with this index annotation should be added to elastic for search
* Could be added to string or Ref attribute
* TODO: rename properly for better code readability
*/
FullText,
/**
* For attribute with this annotation should be created an index in mongo database
*/
Indexed
}

View File

@ -14,12 +14,15 @@
//
import core, {
AnyAttribute,
ArrOf,
Class,
Doc,
DocIndexState,
DocumentQuery,
DocumentUpdate,
extractDocKey,
IndexKind,
MeasureContext,
Ref,
ServerStorage,
@ -95,6 +98,43 @@ export class FullTextPushStage implements FullTextPipelineStage {
return { docs: [], pass: true }
}
async indexRefAttributes (
attributes: Map<string, AnyAttribute>,
doc: DocIndexState,
elasticDoc: IndexedDoc,
metrics: MeasureContext
): Promise<void> {
for (const attribute in doc.attributes) {
const { attr } = extractDocKey(attribute)
const attrObj = attributes.get(attr)
if (
attrObj !== null &&
attrObj !== undefined &&
attrObj.index === IndexKind.FullText &&
(attrObj.type._class === core.class.RefTo ||
(attrObj.type._class === core.class.ArrOf && (attrObj.type as ArrOf<any>).of._class === core.class.RefTo))
) {
const attrStringValue = doc.attributes[attribute]
if (attrStringValue !== undefined && attrStringValue !== null && attrStringValue !== '') {
const refs = attrStringValue.split(',')
const refDocs = await metrics.with(
'ref-docs',
{},
async (ctx) =>
await this.dbStorage.findAll(ctx, core.class.DocIndexState, {
_id: { $in: refs }
})
)
if (refDocs.length > 0) {
refDocs.forEach((c) => {
updateDoc2Elastic(c.attributes, elasticDoc, c._id)
})
}
}
}
}
}
async collect (toIndex: DocIndexState[], pipeline: FullTextPipeline, metrics: MeasureContext): Promise<void> {
const bulk: IndexedDoc[] = []
@ -162,6 +202,11 @@ export class FullTextPushStage implements FullTextPipelineStage {
}
}
const allAttributes = pipeline.hierarchy.getAllAttributes(elasticDoc._class)
// Include child ref attributes
await this.indexRefAttributes(allAttributes, doc, elasticDoc, metrics)
this.checkIntegrity(elasticDoc)
bulk.push(elasticDoc)
} catch (err: any) {

View File

@ -534,7 +534,7 @@ export class FullTextIndexPipeline implements FullTextPipeline {
async checkIndexConsistency (dbStorage: ServerStorage): Promise<void> {
if (process.env.MODEL_VERSION !== undefined) {
const modelVersion = await (await this.model.findAll(core.class.Version, {})).shift()
const modelVersion = (await this.model.findAll(core.class.Version, {})).shift()
if (modelVersion !== undefined) {
const modelVersionString = versionToString(modelVersion)
if (modelVersionString !== process.env.MODEL_VERSION) {

View File

@ -107,4 +107,4 @@ export const fieldStateId = 'fld-v5'
/**
* @public
*/
export const fullTextPushStageId = 'fts-v4'
export const fullTextPushStageId = 'fts-v5'