匹配至少有一个共同字的字符串

我正在进行查询以获取具有特定标题的文档的URI。 我的查询是:

PREFIX rdf:  PREFIX dc:  SELECT ?document WHERE { ?document dc:title ?title. FILTER (?title = "…" ). } 

其中"…"实际上是this.getTitle()的值,因为查询字符串是由以下生成的:

 String queryString = "PREFIX rdf:  " + "PREFIX dc:  SELECT ?document WHERE { " + "?document dc:title ?title." + "FILTER (?title = \"" + this.getTitle() + "\" ). }"; 

通过上面的查询,我只得到标题与this.getTitle()完全相同的文档。 想象一下this.getTitle由超过1个单词组成。 即使只有一个单词形成this.getTitle出现在文档标题上(例如),我也想获取文档。 我怎么能这样做?

假设你有一些像(在Turtle中)的数据:

 @prefix :  . @prefix dc:  . :a dc:title "Great Gatsby" . :b dc:title "Boring Gatsby" . :c dc:title "Great Expectations" . :d dc:title "The Great Muppet Caper" . 

然后你可以使用如下查询:

 prefix :  prefix dc:  select ?x ?title where { # this is just in place of this.getTitle(). It provides a value for # ?TITLE that is "Gatsby Strikes Again". values ?TITLE { "Gatsby Strikes Again" } # Select a thing and its title. ?x dc:title ?title . # Then filter based on whether the ?title matches the result # of replacing the strings in ?TITLE with "|", and matching # case insensitively. filter( regex( ?title, replace( ?TITLE, " ", "|" ), "i" )) } 

得到像这样的结果

 ------------------------ | x | title | ======================== | :b | "Boring Gatsby" | | :a | "Great Gatsby" | ------------------------ 

这个问题特别简洁,因为你正在动态生成模式,你甚至可以根据图形模式中的另一个值来制作它。 例如,如果你想要所有标题匹配至少一个单词的东西,你可以这样做:

 prefix :  prefix dc:  select ?x ?xtitle ?y ?ytitle where { ?x dc:title ?xtitle . ?y dc:title ?ytitle . filter( regex( ?xtitle, replace( ?ytitle, " ", "|" ), "i" ) && ?x != ?y ) } order by ?x ?y 

要得到:

 ----------------------------------------------------------------- | x | xtitle | y | ytitle | ================================================================= | :a | "Great Gatsby" | :b | "Boring Gatsby" | | :a | "Great Gatsby" | :c | "Great Expectations" | | :a | "Great Gatsby" | :d | "The Great Muppet Caper" | | :b | "Boring Gatsby" | :a | "Great Gatsby" | | :c | "Great Expectations" | :a | "Great Gatsby" | | :c | "Great Expectations" | :d | "The Great Muppet Caper" | | :d | "The Great Muppet Caper" | :a | "Great Gatsby" | | :d | "The Great Muppet Caper" | :c | "Great Expectations" | ----------------------------------------------------------------- 

当然, 非常重要的是要注意你现在正在根据你的数据生成模式,这意味着可以将数据放入你的系统的人可能会投入非常昂贵的模式来阻止查询并导致拒绝-服务。 在一个更平凡的笔记中,如果你的任何标题中包含会干扰正则表达式的字符,你可能会遇到麻烦。 一个有趣的问题是,如果某个东西有多个空格的标题,那么该模式就会变成The|Words|With||Two|Spaces ,因为那里的空模式可能会使所有东西都匹配。 这是一个有趣的方法,但它有很多警告。

通常,您可以执行此操作,如此处所示,或者通过在代码中生成正则表达式(您可以在其中处理转义等),或者您可以使用支持某些基于文本的扩展的SPARQL引擎(例如, jena) -text ,将Apache Lucene或Apache Solr添加到Apache Jena)。