Skip to content

Commit

Permalink
added 'materials' facet
Browse files Browse the repository at this point in the history
  • Loading branch information
sdevalk committed Oct 2, 2023
1 parent 29d9a1c commit 9b4caf3
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/researcher/src/lib/api/objects/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export type SearchResult = {
types: SearchResultFilter[];
subjects: SearchResultFilter[];
locations: SearchResultFilter[];
materials: SearchResultFilter[];
};
};
140 changes: 140 additions & 0 deletions apps/researcher/src/lib/api/objects/searcher.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,146 @@ describe('search', () => {
name: 'Suriname',
},
],
materials: [
{
totalCount: 2,
id: 'Canvas',
name: 'Canvas',
},
{
totalCount: 1,
id: 'Ink',
name: 'Ink',
},
{
totalCount: 2,
id: 'Oilpaint',
name: 'Oilpaint',
},
{
totalCount: 2,
id: 'Paper',
name: 'Paper',
},
],
},
});
});

it('finds heritage objects if "owners" filter matches', async () => {
const result = await heritageObjectSearcher.search({
filters: {
owners: ['Library'],
},
});

expect(result).toMatchObject({
totalCount: 1,
filters: {
owners: [
{totalCount: 1, id: 'Library', name: 'Library'},
{totalCount: 0, id: 'Museum', name: 'Museum'},
{
totalCount: 0,
id: 'Research Organisation',
name: 'Research Organisation',
},
],
},
});
});

it('finds heritage objects if "types" filter matches', async () => {
const result = await heritageObjectSearcher.search({
filters: {
types: ['Painting'],
},
});

expect(result).toMatchObject({
totalCount: 1,
filters: {
types: [
{totalCount: 0, id: 'Canvas Painting', name: 'Canvas Painting'},
{totalCount: 0, id: 'Drawing', name: 'Drawing'},
{totalCount: 1, id: 'Painting', name: 'Painting'},
{totalCount: 0, id: 'Photo', name: 'Photo'},
],
},
});
});

it('finds heritage objects if "subjects" filter matches', async () => {
const result = await heritageObjectSearcher.search({
filters: {
subjects: ['Castle'],
},
});

expect(result).toMatchObject({
totalCount: 1,
filters: {
subjects: [
{totalCount: 1, id: 'Castle', name: 'Castle'},
{totalCount: 0, id: 'Celebrations', name: 'Celebrations'},
{totalCount: 1, id: 'Cottage', name: 'Cottage'},
{totalCount: 0, id: 'Palace', name: 'Palace'},
],
},
});
});

it('finds heritage objects if "locations" filter matches', async () => {
const result = await heritageObjectSearcher.search({
filters: {
locations: ['Malaysia'],
},
});

expect(result).toMatchObject({
totalCount: 1,
filters: {
locations: [
{totalCount: 0, id: 'Indonesia', name: 'Indonesia'},
{totalCount: 1, id: 'Malaysia', name: 'Malaysia'},
{totalCount: 0, id: 'Suriname', name: 'Suriname'},
],
},
});
});

it('finds heritage objects if "materials" filter matches', async () => {
const result = await heritageObjectSearcher.search({
filters: {
materials: ['Canvas'],
},
});

expect(result).toMatchObject({
totalCount: 2,
filters: {
materials: [
{
totalCount: 2,
id: 'Canvas',
name: 'Canvas',
},
{
totalCount: 0,
id: 'Ink',
name: 'Ink',
},
{
totalCount: 2,
id: 'Oilpaint',
name: 'Oilpaint',
},
{
totalCount: 0,
id: 'Paper',
name: 'Paper',
},
],
},
});
});
Expand Down
13 changes: 11 additions & 2 deletions apps/researcher/src/lib/api/objects/searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ enum RawKeys {
Image = 'https://colonialcollections nl/schema#image',
Owner = 'https://colonialcollections nl/schema#owner',
Publisher = 'https://colonialcollections nl/schema#publisher',
DateCreated = 'https://colonialcollections nl/schema#dateCreatedStart', // Earliest date of creation
CountryCreated = 'https://colonialcollections nl/schema#countryCreated',
IsPartOf = 'https://colonialcollections nl/schema#isPartOf',
}
Expand All @@ -61,6 +60,7 @@ const searchOptionsSchema = z.object({
types: z.array(z.string()).optional().default([]),
subjects: z.array(z.string()).optional().default([]),
locations: z.array(z.string()).optional().default([]),
materials: z.array(z.string()).optional().default([]),
})
.optional(),
});
Expand All @@ -82,7 +82,6 @@ const rawHeritageObjectSchema = z
.setKey(RawKeys.Image, z.array(z.string()).optional())
.setKey(RawKeys.Owner, z.array(z.string()).optional())
.setKey(RawKeys.Publisher, z.array(z.string()).optional())
.setKey(RawKeys.DateCreated, z.array(z.string()).optional())
.setKey(RawKeys.IsPartOf, z.array(z.string()).min(1));

type RawHeritageObject = z.infer<typeof rawHeritageObjectSchema>;
Expand Down Expand Up @@ -119,11 +118,13 @@ const rawSearchResponseWithAggregationsSchema = rawSearchResponseSchema.merge(
types: rawAggregationSchema,
subjects: rawAggregationSchema,
locations: rawAggregationSchema,
materials: rawAggregationSchema,
}),
owners: rawAggregationSchema,
types: rawAggregationSchema,
subjects: rawAggregationSchema,
locations: rawAggregationSchema,
materials: rawAggregationSchema,
}),
})
);
Expand Down Expand Up @@ -258,6 +259,7 @@ export class HeritageObjectSearcher {
types: buildAggregation(RawKeys.AdditionalType),
subjects: buildAggregation(RawKeys.About),
locations: buildAggregation(RawKeys.CountryCreated),
materials: buildAggregation(RawKeys.Material),
};

const sortByRawKey = sortByToRawKeys.get(options.sortBy!)!;
Expand Down Expand Up @@ -310,6 +312,7 @@ export class HeritageObjectSearcher {
[RawKeys.AdditionalType, options.filters?.types],
[RawKeys.About, options.filters?.subjects],
[RawKeys.CountryCreated, options.filters?.locations],
[RawKeys.Material, options.filters?.materials],
]);

for (const [rawHeritageObjectKey, filters] of queryFilters) {
Expand Down Expand Up @@ -356,6 +359,11 @@ export class HeritageObjectSearcher {
aggregations.locations.buckets
);

const materialFilters = buildFilters(
aggregations.all.materials.buckets,
aggregations.materials.buckets
);

const searchResult: SearchResult = {
totalCount: hits.total.value,
offset: options.offset!,
Expand All @@ -368,6 +376,7 @@ export class HeritageObjectSearcher {
types: typeFilters,
subjects: subjectFilters,
locations: locationFilters,
materials: materialFilters,
},
};

Expand Down

0 comments on commit 9b4caf3

Please sign in to comment.