Skip to content

Commit

Permalink
Merge pull request #101 from reload/related-materials-exclude-empty-p…
Browse files Browse the repository at this point in the history
…rops

Exclude clauses with empty data when searching for related materials
  • Loading branch information
kasperg authored Jul 7, 2020
2 parents 93950fd + a9231e4 commit e0dc8bf
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/apps/related-materials/related-materials.entry.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,36 @@ function RelatedMaterialsEntry({
maxTries
}) {
const coverClient = new CoverService({ baseUrl: coverServiceUrl });
const subject = `term.subject any "${subjects}"`;
const category = `term.category any "${categories}"`;
const source = `term.acSource any "${sources}"`;
const excludeTitle = `phrase.title="${rawExcludeTitle}"`;
const query = `${subject} and ${category} and ${source} not ${excludeTitle}`;

// We may be passed empty strings which will lead to an invalid query.
// Compile query clauses using only arguments with actual values.
const includes = [];
if (subjects) {
includes.push(`term.subject any "${subjects}"`);
}
if (categories) {
includes.push(`term.category any "${categories}"`);
}
if (sources) {
includes.push(`term.acSource any "${sources}"`);
}
const excludes = [];
if (rawExcludeTitle) {
excludes.push(`not phrase.title="${rawExcludeTitle}"`);
}

// Use join to get spacing between clauses right. Includes must be separated
// by "and" while excludes must nut. Excludes should already have "not" prepended.
const query = [includes.join(" and "), excludes.join(" ")].join(" ");

const searchUrl = `${replacePlaceholders({
text: rawSearchUrl,
placeholders: {
query: encodeURI(query),
sort: encodeURI(sort)
}
})}`;

const relatedMaterials = useGetRelatedMaterials({
query,
fields: [
Expand Down

0 comments on commit e0dc8bf

Please sign in to comment.