Skip to content

Commit

Permalink
Search improvements, hit based
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed May 26, 2024
1 parent 4212102 commit 1979b1c
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 60 deletions.
93 changes: 91 additions & 2 deletions __tests__/search1.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'vitest';
import wunderAutoComplete from '../fixtures/search1/autocomplete.json';
import { createSearch1AutocompleteStore } from '../src/search1';
import searchResponse from '../fixtures/search1/search.json';
import { createSearch1AutocompleteStore, createSearch1Store } from '../src/search1';
import invariant from 'tiny-invariant';

describe('Search 1 helper', () => {
Expand All @@ -20,7 +21,7 @@ describe('Search 1 helper', () => {
} as any,
{
fetcher: async (url: string, options: RequestInit) => {
return [wunderAutoComplete, null];
return [wunderAutoComplete as any, null];
},
}
);
Expand Down Expand Up @@ -80,4 +81,92 @@ describe('Search 1 helper', () => {
]
`);
});

test('Search 1 example', async () => {
const search = createSearch1Store(
{
'@context': 'http://iiif.io/api/search/1/context.json',
'@id': 'https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231',
profile: 'http://iiif.io/api/search/1/search',
},
{
fetcher: async (url: string, options: RequestInit) => {
return [searchResponse as any, null];
},
}
);

const $actions = search.getState();

expect($actions.hasSearch).toBe(true);

await $actions.search({ q: 'test' });

expect(search.getState().errorMessage).toBe('');
expect(search.getState().error).toBe(false);
expect(search.getState().loading).toBe(false);
expect(search.getState().lastQuery).toMatchInlineSnapshot(`
{
"q": "test",
}
`);

expect(search.getState().resources).not.toHaveLength(0);
expect(search.getState().hits).not.toHaveLength(0);

// Highlight.
$actions.highlightHit(0);
expect(search.getState().highlight).toMatchInlineSnapshot(`
{
"hit": {
"@type": "search:Hit",
"after": " in nachfolgender Weise zugetragen hat.",
"annotations": [
"https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/fd5845c8c1a96fd0",
],
"before": "Erstens beschreibt sie was sich im Jahre ",
"match": "1615",
},
"results": [
{
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/fd5845c8c1a96fd0",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"on": "https://api.digitale-sammlungen.de/iiif/presentation/v2/bsb10267231/canvas/10#xywh=921,516,82,44",
"resource": {
"@type": "cnt:ContentAsText",
"chars": "1615",
},
},
],
}
`);

$actions.nextHit();
expect(search.getState().highlight).toMatchInlineSnapshot(`
{
"hit": {
"@type": "search:Hit",
"after": " zur Nachtszeit sowohl von Schmerzen als Durst geplagt wurde, kamen meine heiligen Engel zu mir und sprachen: Liebe Schwester! komme hieher; der Herr ruft dich.",
"annotations": [
"https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/1bb78ba8497ff704",
],
"before": "Als ich im Juni ",
"match": "1615",
},
"results": [
{
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/1bb78ba8497ff704",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"on": "https://api.digitale-sammlungen.de/iiif/presentation/v2/bsb10267231/canvas/37#xywh=526,1509,83,43",
"resource": {
"@type": "cnt:ContentAsText",
"chars": "1615",
},
},
],
}
`);
});
});
51 changes: 51 additions & 0 deletions fixtures/search1/search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"@context": ["http://iiif.io/api/presentation/2/context.json", "http://iiif.io/api/search/1/context.json"],
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231?q=1615",
"@type": "sc:AnnotationList",
"resources": [
{
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/fd5845c8c1a96fd0",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": { "@type": "cnt:ContentAsText", "chars": "1615" },
"on": "https://api.digitale-sammlungen.de/iiif/presentation/v2/bsb10267231/canvas/10#xywh=921,516,82,44"
},
{
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/1bb78ba8497ff704",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": { "@type": "cnt:ContentAsText", "chars": "1615" },
"on": "https://api.digitale-sammlungen.de/iiif/presentation/v2/bsb10267231/canvas/37#xywh=526,1509,83,43"
},
{
"@id": "https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/24096cdccaf36187",
"@type": "oa:Annotation",
"motivation": "sc:painting",
"resource": { "@type": "cnt:ContentAsText", "chars": "1615" },
"on": "https://api.digitale-sammlungen.de/iiif/presentation/v2/bsb10267231/canvas/62#xywh=935,792,85,44"
}
],
"hits": [
{
"annotations": ["https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/fd5845c8c1a96fd0"],
"match": "1615",
"before": "Erstens beschreibt sie was sich im Jahre ",
"after": " in nachfolgender Weise zugetragen hat.",
"@type": "search:Hit"
},
{
"annotations": ["https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/1bb78ba8497ff704"],
"match": "1615",
"before": "Als ich im Juni ",
"after": " zur Nachtszeit sowohl von Schmerzen als Durst geplagt wurde, kamen meine heiligen Engel zu mir und sprachen: Liebe Schwester! komme hieher; der Herr ruft dich.",
"@type": "search:Hit"
},
{
"annotations": ["https://api.digitale-sammlungen.de/iiif/services/search/v1/bsb10267231/anno/24096cdccaf36187"],
"match": "1615",
"before": "Hier wollen wir anfügen was ihr der Herr aus gleichem Grunde als sie einst heftige Schmerzen erlitt durch ein Gleichniß erklärte. Dieß erzählt sie als im Jahre ",
"after": " vorgefallen, fol- gender Weise.",
"@type": "search:Hit"
}
]
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@
"lint": "publint"
},
"resolutions": {
"@iiif/presentation-3": "2.2.2",
"@iiif/presentation-3": "2.2.3",
"@iiif/presentation-3-normalized": "0.9.7"
},
"dependencies": {
"@types/geojson": "7946.0.13",
"@iiif/presentation-2": "1.0.4",
"@iiif/presentation-3": "2.2.2",
"@iiif/presentation-3": "2.2.3",
"@iiif/presentation-3-normalized": "0.9.7"
},
"optionalDependencies": {
Expand All @@ -237,6 +237,11 @@
"@iiif/parser": "^2.1.2",
"@atlas-viewer/iiif-image-api": "^2.2.0"
},
"overrides": {
"@iiif/parser": {
"@iiif/presentation-3": "2.2.3"
}
},
"devDependencies": {
"@types/jsdom": "^21.1.3",
"@types/svg-arc-to-cubic-bezier": "^3.2.0",
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1979b1c

Please sign in to comment.