Skip to content

Commit

Permalink
Merge pull request #740 from tr1ten/inlinesearch
Browse files Browse the repository at this point in the history
BB-528: Index author in ElasticSearch
  • Loading branch information
MonkeyDo authored Sep 22, 2023
2 parents 5f67420 + a9c5a0a commit fd9d3ee
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/client/components/pages/parts/search-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import * as bootstrap from 'react-bootstrap';

import {differenceBy as _differenceBy, kebabCase as _kebabCase, startCase as _startCase} from 'lodash';
import {differenceBy as _differenceBy, kebabCase as _kebabCase, startCase as _startCase, toLower} from 'lodash';

import AddToCollectionModal from './add-to-collection-modal';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -185,6 +185,10 @@ class SearchResults extends React.Component {
<a href={link}>
{name} {disambiguation}
</a>
{
toLower(result.type) === 'work' && Boolean(result.authors?.length) &&
<span className="small text-muted"><i>{result.authors.join(', ')}</i></span>
}
</td>
{
!this.props.condensed &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class EntitySearchFieldOption extends React.Component {
);
const entityOption = {
...entity,
authors: entity.authors?.join(', ') ?? null,
disambiguation: _.get(entity, ['disambiguation', 'comment']),
id,
language: language && language.label,
Expand Down
7 changes: 6 additions & 1 deletion src/client/entity-editor/common/entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {genEntityIconHTMLElement} from '../../helpers/entity';


type EntityProps = {
authors: string | null,
disambiguation?: string | null | undefined,
language?: string,
link?: string | false,
Expand All @@ -31,7 +32,7 @@ type EntityProps = {
};

function Entity(
{disambiguation, language, link, text, type, unnamedText}: EntityProps
{disambiguation, language, link, text, type, unnamedText, authors}: EntityProps
) {
const nameComponent = text || <i>{unnamedText}</i>;
const contents = (
Expand All @@ -44,6 +45,10 @@ function Entity(
disambiguation &&
<span className="disambig margin-left-0-3"><small>({disambiguation})</small></span>
}
{
authors &&
<span className="small text-muted"><i>{authors}</i></span>
}
{
language &&
<span className="text-muted small margin-left-0-3">{language}</span>
Expand Down
12 changes: 8 additions & 4 deletions src/client/entity-editor/common/linked-entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class LinkedEntity extends React.Component<any, any> {

render() {
const option = this.getSafeOptionValue(this.props.data);
const {disambiguation, text, type, unnamedText, language, __isNew__} = option;
const {disambiguation, text, type, unnamedText, language, authors, __isNew__} = option;
const nameComponent = text || <i>{unnamedText}</i>;
const externalLinkComponent = !__isNew__ &&
<a onClick={this.handleChildEvent}>
Expand All @@ -97,10 +97,14 @@ class LinkedEntity extends React.Component<any, any> {
}
&nbsp;
{nameComponent}
&nbsp;&nbsp;
{disambiguation &&
<span className="disambig">
&nbsp;<small>({disambiguation})</small>
</span>
}
{
disambiguation &&
<span className="disambig"><small>({disambiguation})</small></span>
authors &&
<span className="small text-muted"><i>{authors}</i></span>
}
{' '}
{externalLinkComponent}
Expand Down
43 changes: 39 additions & 4 deletions src/common/helpers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ async function _fetchEntityModelsForESResults(orm, results) {
return rel;
}));
}
if (entityStub.authors) {
entityJSON.authors = entityStub.authors;
}
return entityJSON;
})).catch(err => log.error(err));
return processedResults;
Expand Down Expand Up @@ -291,6 +294,10 @@ export async function generateIndex(orm) {
},
type: 'object'
},
authors: {
analyzer: 'trigrams',
type: 'text'
},
disambiguation: {
analyzer: 'trigrams',
type: 'text'
Expand Down Expand Up @@ -377,7 +384,7 @@ export async function generateIndex(orm) {
{model: EditionGroup, relations: []},
{model: Publisher, relations: ['area']},
{model: Series, relations: ['seriesOrderingType']},
{model: Work, relations: []}
{model: Work, relations: ['workType', 'relationshipSet.relationships.type']}
];

// Update the indexed entries for each entity type
Expand All @@ -392,8 +399,33 @@ export async function generateIndex(orm) {
})
);
const entityLists = await Promise.all(behaviorPromise);

/* eslint-disable @typescript-eslint/no-unused-vars */
const [authorsCollection,
editionCollection,
editionGroupCollection,
publisherCollection,
seriesCollection,
workCollection] = entityLists;
/* eslint-enable @typescript-eslint/no-unused-vars */
const listIndexes = [];

workCollection.forEach(workEntity => {
const relationshipSet = workEntity.related('relationshipSet');
if (relationshipSet) {
const authorWroteWorkRels = relationshipSet.related('relationships')?.filter(relationshipModel => relationshipModel.get('typeId') === 8);
const authorNames = authorWroteWorkRels.map(relationshipModel => {
// Search for the Author in the already fetched BookshelfJS Collection
const source = authorsCollection.get(relationshipModel.get('sourceBbid'));
if (!source) {
// This shouldn't happen, but just in case
return null;
}
return source.toJSON().defaultAlias.name;
});
workEntity.set('authors', authorNames);
}
});
// Index all the entities
for (const entityList of entityLists) {
const listArray = entityList.toJSON();
listIndexes.push(_processEntityListForBulk(listArray));
Expand Down Expand Up @@ -496,6 +528,7 @@ export async function checkIfExists(orm, name, type) {
}

export function searchByName(orm, name, type, size, from) {
const sanitizedEntityType = sanitizeEntityType(type);
const dslQuery = {
body: {
from,
Expand All @@ -515,8 +548,11 @@ export function searchByName(orm, name, type, size, from) {
size
},
index: _index,
type: sanitizeEntityType(type)
type: sanitizedEntityType
};
if (sanitizedEntityType === 'work' || (Array.isArray(sanitizedEntityType) && sanitizedEntityType.includes('work'))) {
dslQuery.body.query.multi_match.fields.push('authors');
}

return _searchForEntities(orm, dslQuery);
}
Expand All @@ -534,7 +570,6 @@ export async function init(orm, options) {
if (mainIndexExists) {
return null;
}

return generateIndex(orm);
}
catch (error) {
Expand Down

0 comments on commit fd9d3ee

Please sign in to comment.