-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c134418
commit dcca954
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@ldkit/example-custom-query", | ||
"version": "0.0.3", | ||
"author": "Karel Klima", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "parcel src/index.html", | ||
"watch": "parcel watch src/index.ts", | ||
"nodemon": "nodemon dist/index.js", | ||
"build": "parcel build src/index.html --no-scope-hoist" | ||
}, | ||
"dependencies": { | ||
"@ldkit/core": "*", | ||
"@ldkit/namespaces": "*", | ||
"@ldkit/rdf": "*", | ||
"@ldkit/sparql": "*", | ||
"rxjs": "^7.3.0" | ||
}, | ||
"devDependencies": { | ||
"@parcel/packager-ts": "^2.0.0", | ||
"@parcel/transformer-typescript-types": "^2.0.0", | ||
"parcel": "^2.0.0", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.4.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>LDKit Custom Query</title> | ||
</head> | ||
<body> | ||
Open the developer console | ||
<div id="app"></div> | ||
<script type="module" src="index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { lucene, lucene_instance, SearchResource } from "./store"; | ||
import { $ } from "@ldkit/sparql"; | ||
import { namedNode as n } from "@ldkit/rdf"; | ||
import { skos } from "@ldkit/namespaces"; | ||
|
||
console.log("CUSTOM SEARCH QUERY USING LUCENE GRAPHDB CONNECTOR"); | ||
|
||
const searchString = "ní*"; | ||
const splitExactMatch = "<em>ní</em>"; | ||
|
||
const s = (str: string) => `"${str}"`; | ||
|
||
const query = $` | ||
CONSTRUCT { | ||
?entity a ${n(skos.Concept)} ; | ||
${n(skos.prefLabel)} ?label ; | ||
${n(lucene.snippetText)} ?snippetText ; | ||
${n(lucene.snippetField)} ?snippetField ; | ||
${n(lucene.score)} ?score . | ||
} WHERE { | ||
SELECT DISTINCT ?entity ?label ?snippetField ?snippetText ?score { | ||
{ ?search a ${n(lucene_instance.label_index)} } | ||
UNION | ||
{ ?search a ${n(lucene_instance.defcom_index)} } | ||
?search ${n(lucene.query)} ${s(searchString)} ; | ||
${n(lucene.snippetSize)} 2000 ; | ||
${n(lucene.entities)} ?entity . | ||
GRAPH ?g { | ||
?entity a ${n(skos.Concept)} ; | ||
${n(skos.prefLabel)} ?label . | ||
} | ||
?entity ${n(lucene.score)} ?initScore ; | ||
${n(lucene.snippets)} _:s . | ||
_:s ${n(lucene.snippetText)} ?snippetText ; | ||
${n(lucene.snippetField)} ?snippetField . | ||
FILTER (lang(?label) = "cs") | ||
BIND(IF(lcase(str(?snippetText)) = lcase(str(${s( | ||
splitExactMatch | ||
)})), ?initScore * 2, IF(CONTAINS(lcase(str(?snippetText)), ${s( | ||
splitExactMatch | ||
)}), IF(?snippetField = "label", ?initScore * 1.5, ?initScore), ?initScore)) as ?exactMatchScore) | ||
BIND(IF(?snippetField = "label", ?exactMatchScore * 2, IF(?snippetField = "definition", ?exactMatchScore * 1.2, ?exactMatchScore)) as ?score) | ||
} | ||
ORDER BY desc(?score) | ||
LIMIT 100 | ||
} | ||
`.toString(); | ||
|
||
console.log(query); | ||
|
||
const results = SearchResource.query(query); | ||
results.subscribe((res) => { | ||
for (const r of res) { | ||
console.log(r.label, r.score, r.snippetField, r.snippetText); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
createResource, | ||
createNamespace, | ||
createDefaultContext, | ||
} from "@ldkit/core"; | ||
import type { SchemaInterface } from "@ldkit/core"; | ||
import { xsd, skos } from "@ldkit/namespaces"; | ||
|
||
export const lucene = createNamespace({ | ||
iri: "http://www.ontotext.com/connectors/lucene#", | ||
prefix: "lucene:", | ||
terms: [ | ||
"query", | ||
"entities", | ||
"snippets", | ||
"snippetText", | ||
"snippetField", | ||
"snippetSize", | ||
"score", | ||
], | ||
} as const); | ||
|
||
export const lucene_instance = createNamespace({ | ||
iri: "http://www.ontotext.com/connectors/lucene/instance#", | ||
prefix: "lucene_instance:", | ||
terms: ["label_index", "defcom_index"], | ||
} as const); | ||
|
||
const SearchSchema = { | ||
"@type": skos.Concept, | ||
label: skos.prefLabel, | ||
snippetField: lucene.snippetField, | ||
snippetText: lucene.snippetText, | ||
score: { | ||
"@id": lucene.score, | ||
"@type": xsd.double, | ||
}, | ||
} as const; | ||
|
||
const customFetch = (resource: RequestInfo, init?: RequestInit) => { | ||
console.log("CUSTOM FETCH FETCHING"); | ||
const headers = init?.headers as Headers; | ||
const ct = (init?.headers as Headers).get("Content-type"); | ||
console.log(ct); | ||
if (ct === "application/x-www-form-urlencoded") { | ||
headers.set( | ||
"Content-type", | ||
"application/x-www-form-urlencoded; charset=UTF-8" | ||
); | ||
} | ||
return fetch(resource, init); | ||
}; | ||
|
||
createDefaultContext({ | ||
sources: [ | ||
{ | ||
type: "sparql", | ||
value: | ||
"https://xn--slovnk-7va.gov.cz/prohlizime/sluzby/db-server/repositories/termit", | ||
}, | ||
], | ||
fetch: customFetch, | ||
}); | ||
|
||
export type SearchInterface = SchemaInterface<typeof SearchSchema>; | ||
|
||
export const SearchResource = createResource(SearchSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"noEmit": true | ||
}, | ||
"include": ["src"], | ||
"references": [ | ||
{ | ||
"path": "../../library/context" | ||
}, | ||
{ | ||
"path": "../../library/core" | ||
}, | ||
{ | ||
"path": "../../library/namespaces" | ||
}, | ||
{ | ||
"path": "../../library/rdf" | ||
}, | ||
{ | ||
"path": "../../library/sparql" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters