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

#4513 - Support full text search in knowledge bases running on Blazegraph #4514

Merged
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
7 changes: 7 additions & 0 deletions inception/inception-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions inception/inception-kb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@
<artifactId>spring-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class IriConstants
public static final String PREFIX_LUCENE_SEARCH = "http://www.openrdf.org/contrib/lucenesail#";
public static final String PREFIX_MWAPI = "https://www.mediawiki.org/ontology#API/";
public static final String PREFIX_STARDOG = "tag:stardog:api:search:";
public static final String PREFIX_BLAZEGRAPH = "http://www.bigdata.com/rdf/search#";

public static final String UKP_WIKIDATA_SPARQL_ENDPOINT = "http://knowledgebase.ukp.informatik.tu-darmstadt.de:8890/sparql";
public static final Set<String> IMPLICIT_NAMESPACES = Set.of(RDF.NAMESPACE, RDFS.NAMESPACE,
Expand Down Expand Up @@ -87,6 +88,7 @@ public class IriConstants
public static final IRI FTS_VIRTUOSO;
public static final IRI FTS_WIKIDATA;
public static final IRI FTS_STARDOG;
public static final IRI FTS_BLAZEGRAPH;
public static final IRI FTS_NONE;

public static final List<IRI> CLASS_IRIS;
Expand Down Expand Up @@ -116,6 +118,7 @@ public class IriConstants
FTS_LUCENE = vf.createIRI(PREFIX_LUCENE_SEARCH, "matches");
FTS_WIKIDATA = vf.createIRI(PREFIX_MWAPI, "search");
FTS_STARDOG = vf.createIRI(PREFIX_STARDOG, "textMatch");
FTS_BLAZEGRAPH = vf.createIRI(PREFIX_BLAZEGRAPH, "search");
FTS_NONE = vf.createIRI("FTS:NONE");

CLASS_IRIS = asList(RDFS.CLASS, OWL.CLASS, WIKIDATA_CLASS, SKOS.CONCEPT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.tudarmstadt.ukp.inception.kb.querybuilder;

import static org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.prefix;
import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.iri;
import static org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf.literalOf;

import org.eclipse.rdf4j.sparqlbuilder.core.Prefix;
import org.eclipse.rdf4j.sparqlbuilder.core.Variable;
import org.eclipse.rdf4j.sparqlbuilder.graphpattern.GraphPattern;
import org.eclipse.rdf4j.sparqlbuilder.rdf.Iri;

import de.tudarmstadt.ukp.inception.kb.IriConstants;

public class BlazegraphFtsQuery
implements GraphPattern
{
public static final Prefix PREFIX_BLAZEGRAPH_SEARCH = prefix("bds",
iri(IriConstants.FTS_BLAZEGRAPH));
public static final Iri BLAZEGRAPH_SEARCH = PREFIX_BLAZEGRAPH_SEARCH.iri("search");
public static final Iri BLAZEGRAPH_RELEVANCE = PREFIX_BLAZEGRAPH_SEARCH.iri("relevance");
public static final Iri BLAZEGRAPH_MAX_RANK = PREFIX_BLAZEGRAPH_SEARCH.iri("maxRank");

private final Variable subject;
private final Variable score;
private final Variable matchTerm;
private final Variable matchTermProperty;
private final String query;
private int limit = 0;

public BlazegraphFtsQuery(Variable aSubject, Variable aScore, Variable aMatchTerm,
Variable aMatchTermProperty, String aQuery)
{
subject = aSubject;
score = aScore;
matchTerm = aMatchTerm;
matchTermProperty = aMatchTermProperty;
query = aQuery;
}

public BlazegraphFtsQuery withLimit(int aLimit)
{
limit = aLimit;
return this;
}

@Override
public String getQueryString()
{
var sb = new StringBuilder();
sb.append("SERVICE ");
sb.append(BLAZEGRAPH_SEARCH.getQueryString());
sb.append(" { \n");

var pattern = matchTerm //
.has(BLAZEGRAPH_SEARCH, literalOf(query)) //
.andHas(BLAZEGRAPH_RELEVANCE, score);

if (limit > 0) {
pattern = pattern.andHas(BLAZEGRAPH_MAX_RANK, literalOf(2 * limit));
}

sb.append(pattern.getQueryString());
sb.append(" } ");
sb.append(subject.has(matchTermProperty, matchTerm).getQueryString());
return sb.toString();
}

@Override
public boolean isEmpty()
{
return false;
}
}
Loading
Loading