Skip to content

Commit

Permalink
Added support for custom query
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima committed Oct 27, 2021
1 parent c134418 commit dcca954
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/custom-query/package.json
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"
}
}
13 changes: 13 additions & 0 deletions examples/custom-query/src/index.html
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>
56 changes: 56 additions & 0 deletions examples/custom-query/src/index.ts
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);
}
});
67 changes: 67 additions & 0 deletions examples/custom-query/src/store.ts
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);
24 changes: 24 additions & 0 deletions examples/custom-query/tsconfig.json
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"
}
]
}
13 changes: 13 additions & 0 deletions library/resource/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {

//exists(entity: Identity) {}

query(sparqlConstructQuery: string) {
console.log(sparqlConstructQuery);
return quadsQuery(sparqlConstructQuery, this.context).pipe(
map((graph) => {
const iris = Object.keys(graph);
return iris.reduce((result, iri) => {
result.push(this.createProxy(graph, iri));
return result;
}, new Array<I>());
})
);
}

find() {
const q = this.queryBuilder.getIrisQuery();
console.log(q);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"build:example-todo-app": "npm -w @ldkit/example-todo-app run build",
"start:example-bulletin-boards": "npm -w @ldkit/example-bulletin-boards start",
"build:example-bulletin-boards": "npm -w @ldkit/example-bulletin-boards run build",
"start:example-custom-query": "npm -w @ldkit/example-custom-query start",
"build:example-custom-query": "npm -w @ldkit/example-custom-query run build",
"clean": "npm -w ./library run clean",
"prebuild": "npm run clean",
"build": "npm -w ./library run build",
Expand Down

0 comments on commit dcca954

Please sign in to comment.