Skip to content

Commit

Permalink
search: Omit some fields from object to index
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkeyDo committed Feb 7, 2024
1 parent cc63f20 commit 2ff7709
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/common/helpers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function _fetchEntityModelsForESResults(orm, results) {
.fetch({withRelated: ['areaType']});

const areaJSON = area.toJSON();
const areaJSON = area.toJSON({omitPivot: true});
const areaParents = await area.parents();
areaJSON.defaultAlias = {
name: areaJSON.name
Expand All @@ -78,7 +79,7 @@ async function _fetchEntityModelsForESResults(orm, results) {
const editor = await Editor.forge({id: entityStub.bbid})
.fetch();

const editorJSON = editor.toJSON();
const editorJSON = editor.toJSON({omitPivot: true});
editorJSON.defaultAlias = {
name: editorJSON.name
};
Expand All @@ -90,7 +91,7 @@ async function _fetchEntityModelsForESResults(orm, results) {
const collection = await UserCollection.forge({id: entityStub.bbid})
.fetch();

const collectionJSON = collection.toJSON();
const collectionJSON = collection.toJSON({omitPivot: true});
collectionJSON.defaultAlias = {
name: collectionJSON.name
};
Expand All @@ -103,7 +104,7 @@ async function _fetchEntityModelsForESResults(orm, results) {
const entity = await model.forge({bbid: entityStub.bbid})
.fetch({require: false, withRelated: ['defaultAlias.language', 'disambiguation', 'aliasSet.aliases', 'identifierSet.identifiers',
'relationshipSet.relationships.source', 'relationshipSet.relationships.target', 'relationshipSet.relationships.type', 'annotation']});
const entityJSON = entity?.toJSON();
const entityJSON = entity?.toJSON({omitPivot: true});
if (entityJSON && entityJSON.relationshipSet) {
entityJSON.relationshipSet.relationships = await Promise.all(entityJSON.relationshipSet.relationships.map(async (rel) => {
rel.source = await commonUtils.getEntity(orm, rel.source.bbid, rel.source.type);
Expand Down Expand Up @@ -384,7 +385,7 @@ export async function generateIndex(orm) {
{model: EditionGroup, relations: []},
{model: Publisher, relations: ['area']},
{model: Series, relations: ['seriesOrderingType']},
{model: Work, relations: ['workType', 'relationshipSet.relationships.type']}
{model: Work, relations: ['relationshipSet.relationships.type']}
];

// Update the indexed entries for each entity type
Expand Down Expand Up @@ -427,15 +428,15 @@ export async function generateIndex(orm) {
});
// Index all the entities
for (const entityList of entityLists) {
const listArray = entityList.toJSON();
const listArray = entityList.toJSON({omitPivot: true});
listIndexes.push(_processEntityListForBulk(listArray));
}
await Promise.all(listIndexes);

const areaCollection = await Area.forge()
.fetchAll();

const areas = areaCollection.toJSON();
const areas = areaCollection.toJSON({omitPivot: true});

/** To index names, we use aliasSet.aliases.name and bbid, which Areas don't have.
* We massage the area to return a similar format as BB entities
Expand All @@ -455,7 +456,7 @@ export async function generateIndex(orm) {
// no bots
.where('type_id', 1)
.fetchAll();
const editors = editorCollection.toJSON();
const editors = editorCollection.toJSON({omitPivot: true});

/** To index names, we use aliasSet.aliases.name and bbid, which Editors don't have.
* We massage the editor to return a similar format as BB entities
Expand All @@ -473,7 +474,7 @@ export async function generateIndex(orm) {

const userCollections = await UserCollection.forge().where({public: true})
.fetchAll();
const userCollectionsJSON = userCollections.toJSON();
const userCollectionsJSON = userCollections.toJSON({omitPivot: true});

/** To index names, we use aliasSet.aliases.name and bbid, which UserCollections don't have.
* We massage the editor to return a similar format as BB entities
Expand Down
6 changes: 3 additions & 3 deletions src/server/routes/entity/entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export function handleDelete(
editorJSON.id,
body.note
);
return savedMainEntity.toJSON();
return savedMainEntity.toJSON({omitPivot: true});
});

return handler.sendPromiseResult(res, entityDeletePromise, search.deleteEntity);
Expand Down Expand Up @@ -1047,7 +1047,7 @@ export async function indexAutoCreatedEditionGroup(orm, newEdition, transacting)
transacting,
withRelated: 'aliasSet.aliases'
});
await search.indexEntity(editionGroup.toJSON());
await search.indexEntity(editionGroup.toJSON({omitPivot: true}));
}
catch (err) {
log.error('Could not reindex edition group after edition creation:', err);
Expand Down Expand Up @@ -1165,7 +1165,7 @@ export async function processSingleEntity(formBody, JSONEntity, reqSession,
await indexAutoCreatedEditionGroup(orm, savedMainEntity, transacting);
}

const entityJSON = savedMainEntity.toJSON();
const entityJSON = savedMainEntity.toJSON({omitPivot: true});
if (entityJSON && entityJSON.relationshipSet) {
entityJSON.relationshipSet.relationships = await Promise.all(entityJSON.relationshipSet.relationships.map(async (rel) => {
try {
Expand Down

0 comments on commit 2ff7709

Please sign in to comment.