Skip to content

Commit

Permalink
Merge pull request #1061 from metabrainz/fix-author-indexing
Browse files Browse the repository at this point in the history
Fix author indexing
  • Loading branch information
MonkeyDo committed Feb 6, 2024
2 parents 6967892 + 2540333 commit a6aef08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/common/helpers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ async function _fetchEntityModelsForESResults(orm, results) {
const entityJSON = entity?.toJSON();
if (entityJSON && entityJSON.relationshipSet) {
entityJSON.relationshipSet.relationships = await Promise.all(entityJSON.relationshipSet.relationships.map(async (rel) => {
rel.source = await commonUtils.getEntityAlias(orm, rel.source.bbid, rel.source.type);
rel.target = await commonUtils.getEntityAlias(orm, rel.target.bbid, rel.target.type);
rel.source = await commonUtils.getEntity(orm, rel.source.bbid, rel.source.type);
rel.target = await commonUtils.getEntity(orm, rel.target.bbid, rel.target.type);
return rel;
}));
}
Expand Down
9 changes: 6 additions & 3 deletions src/common/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,15 @@ export async function getEntityByBBID(orm, bbid:string, otherRelations:Array<str
return entityData;
}

export async function getEntityAlias(orm, bbid:string, type:EntityType):Promise<any> {
export async function getEntity(orm, bbid:string, type:EntityType, fetchOptions?:Record<string, any>):Promise<any> {
if (!isValidBBID(bbid)) {
return null;
}
const entityData = await orm.func.entity.getEntity(orm, upperFirst(type), bbid, []);
return entityData;
const finalBBID = await orm.func.entity.recursivelyGetRedirectBBID(orm, bbid);
const Model = getEntityModelByType(orm, upperFirst(type));
const entity = await new Model({bbid: finalBBID})
.fetch({require: true, ...fetchOptions});
return entity && entity.toJSON();
}

export function getAliasLanguageCodes(entity: LazyLoadedEntityT) {
Expand Down
32 changes: 20 additions & 12 deletions src/server/routes/entity/entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1156,32 +1156,40 @@ export async function processSingleEntity(formBody, JSONEntity, reqSession,
editorJSON.id, body.note
);

/* We need to load the aliases for search reindexing and refresh it*/
await savedMainEntity.load(['aliasSet.aliases', 'defaultAlias.language', 'relationshipSet.relationships.source',
'relationshipSet.relationships.target', 'relationshipSet.relationships.type', 'annotation'], {transacting});

/* New entities will lack some attributes like 'type' required for search indexing */
if (isNew) {
await savedMainEntity.refresh({transacting});
/* We need to load the aliases for search reindexing and refresh it (otherwise 'type' is missing for new entities)*/
await savedMainEntity.refresh({transacting, withRelated: ['aliasSet.aliases', 'defaultAlias.language',
'relationshipSet.relationships.source', 'relationshipSet.relationships.target', 'relationshipSet.relationships.type', 'annotation']});

if (isNew && savedMainEntity.get('type') === 'Edition') {
/* fetch and reindex EditionGroups that may have been created automatically by the ORM and not indexed */
if (savedMainEntity.get('type') === 'Edition') {
await indexAutoCreatedEditionGroup(orm, savedMainEntity, transacting);
}
await indexAutoCreatedEditionGroup(orm, savedMainEntity, transacting);
}

const entityJSON = savedMainEntity.toJSON();
if (entityJSON && entityJSON.relationshipSet) {
entityJSON.relationshipSet.relationships = await Promise.all(entityJSON.relationshipSet.relationships.map(async (rel) => {
try {
rel.source = await commonUtils.getEntityAlias(orm, rel.source.bbid, rel.source.type);
rel.target = await commonUtils.getEntityAlias(orm, rel.target.bbid, rel.target.type);
rel.source = await commonUtils.getEntity(orm, rel.source.bbid, rel.source.type, {require: false, transacting});
rel.target = await commonUtils.getEntity(orm, rel.target.bbid, rel.target.type, {require: false, transacting});
}
catch (err) {
log.error(err);
}
return rel;
}));
// Attach a work's authors for search indexing
if (savedMainEntity.get('type') === 'Work') {
const authorsOfWork = entityJSON.relationshipSet.relationships
.filter(
// "Author wrote Work" relationship
(relation) => relation.typeId === 8
)
.map((relation) => {
const {source} = relation;
return source.name;
});
entityJSON.authors = authorsOfWork;
}
}
return entityJSON;
}
Expand Down

0 comments on commit a6aef08

Please sign in to comment.