Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search index and topic improvements #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/enrich/typesense-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export const enrichTypesense: Enrichment<{}, { record: SingleRecord; foundTopics
extraTopics[`topic_${k}`] = v;
}

let plaintext = '';
const keywordsFile = join(api.files, 'keywords.txt');
if (existsSync(keywordsFile)) {
plaintext = await readFile(keywordsFile, 'utf-8');
}
// let plaintext = '';
// const keywordsFile = join(api.files, 'keywords.txt');
// if (existsSync(keywordsFile)) {
// plaintext = await readFile(keywordsFile, 'utf-8');
// }
Comment on lines -62 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can look at this again in the future, it was a way to have scripts that extract plaintext (into the keywords.txt) for indexing - but it made the output 2-3x larger!

const collections = meta.partOfCollections || [];

return {
Expand All @@ -79,7 +79,7 @@ export const enrichTypesense: Enrichment<{}, { record: SingleRecord; foundTopics
url: meta.url,
totalItems: meta.totalItems,
collections: collections.map((c: any) => c.slug),
plaintext,
plaintext: api.resource.metadata.map((m: any) => getValue(m.value)).join(' '),
...extraTopics,
},
foundTopics: Object.keys(extraTopics),
Expand Down
49 changes: 34 additions & 15 deletions src/extract/extract-topics.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Extraction } from "../util/extract.ts";
import { getValue } from "@iiif/helpers/i18n";
import { getSingleLabel } from "../util/get-single-label.ts";
import { Extraction } from '../util/extract.ts';
import { getValue } from '@iiif/helpers/i18n';
import { getSingleLabel } from '../util/get-single-label.ts';

interface ExtractTopicsConfig {
language?: string;
translate?: boolean;
upperCase?: boolean;
dateRange?: string[];
commaSeparated?: string[];
topicTypes: Record<string, string | string[]>; // e.g. { author: ['Author', 'Written by'] } and it will look in the metadata for these.
}

export const extractTopics: Extraction<ExtractTopicsConfig> = {
id: "extract-topics",
name: "Extract Topics",
types: ["Manifest"],
id: 'extract-topics',
name: 'Extract Topics',
types: ['Manifest'],

invalidate: async () => {
return true;
Expand All @@ -22,17 +24,17 @@ export const extractTopics: Extraction<ExtractTopicsConfig> = {
const {
commaSeparated = [],
translate = true,
upperCase = true,
dateRange = [],
topicTypes = {},
language = "en",
language = 'en',
} = config;

const topicsToParse = Object.keys(topicTypes);
const latestResource = resource.vault?.get(api.resource);
const metadata = latestResource?.metadata || [];
const metadataLabels: string[] = await Promise.all(
metadata.map((item: any) =>
getSingleLabel(item.label, { language, translate }),
),
metadata.map((item: any) => getSingleLabel(item.label, { language, translate }))
);

const indices: Record<string, string[]> = {};
Expand All @@ -44,18 +46,35 @@ export const extractTopics: Extraction<ExtractTopicsConfig> = {
if (index === -1) {
continue;
}

const arrayRange = ([start, stop]: number[]) =>
Array.from({ length: stop - start + 1 }, (value, index) => (start + index).toString());

const makeUpperCase = (string: string) =>
upperCase ? string.charAt(0).toUpperCase() + string.slice(1) : string;

const values = metadata[index].value;
const first = Object.keys(values)[0];
const value = values[first] as string[];
if (value) {
if (commaSeparated.includes(topic)) {
indices[topic] = indices[topic] || [];
if (dateRange.includes(topic)) {
value.forEach((v) => {
indices[topic] = indices[topic] || [];
indices[topic].push(...v.split(",").map((t) => t.trim()));
if (v.includes('-')) {
const range = arrayRange(v.split('-').map((d) => +d));
indices[topic].push(...range);
} else {
indices[topic].push(v);
}
});
} else if (commaSeparated.includes(topic)) {
value.forEach((v) => {
indices[topic].push(...v.split(',').map((t) => makeUpperCase(t.trim())));
});
} else {
indices[topic] = indices[topic] || [];
indices[topic].push(...value);
value.forEach((v) => {
indices[topic].push(makeUpperCase(v.trim()));
});
}
}
}
Expand Down