Add text search where clause to SPARQL query

最后都变了- 提交于 2019-12-05 08:52:07
Rob Hall

Approximate Matching

String Matching

Joshua Taylor's comment points out an excellent and elegant solution to do exactly what you asked for:

filter contains( lcase(?label), "word").

You can also use regular expressions via the REGEX Filter Function. You would simply add an additional filter to your query such as:

FILTER regex(?label, "*word*", "i") .

This would allow you to retrieve all labels that contain word (case-insensitive).

Jena Text

The syntax text:query (rdfs:label 'word' 10) you mentioned is part of the jena-text project. Note that you must configure jena-text for it to work. The primary time that you want to use that is if you want to perform approximate text matching ie: if it's acceptable to search for word and get back things like words or wordpress etc.

Exact Matching

Another alternative is exact matching. You can do this by specifying an initial binding, or by modifying your query directly.

Query Modification

Modifying your query would produce one of several variations. Not all of these variations are considered equal (Plain Literals / Language Literals / Typed Literals), so you need to be careful when searching to know that your data will match.

 ?object  rdfs:label "word" .
 ?object  rdfs:label '''word''' .
 ?object  rdfs:label "word"@en .
 ?object  rdfs:label "word"^^xsd:string .

Binding Specification

Constructing an initial binding usually looks something like this (psuedocode):

final QuerySolutionMap initialBinding = new QuerySolutionMap(){{
     this.add("?label", model.createTypedLiteral(someString));
}};
final QueryExecution e = 
         QueryExecutionFactory.create(query,model,initialBinding);

Note that the second argument to add has the same choices as the query modification. You can create a language literal or a plain literal rather than a typed literal. Again, it needs to match your underlying data.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!