如何使用推理通过 marklogic 搜索文档中的相似列?
How to use inferencing for searching on similar columns in documents via marklogic?
假设我有两个 xml 文档:
文档 1:
<item>
<item_id> 001 </item_id>
<color>blue</color>
</item>
文档 2:
<item>
<item_ref_id>abc</item_ref_id>
<color>blue</color>
</item>
现在为了推理,我将定义一个三元组:
<item_ref_id> <http://www.w3.org/2002/07/owl#sameAs> <item_id>
如果我使用 <item_id>
= abc 编写 SPARQL 查询来获取 document2,它应该可以工作。
这是否可能通过推理实现,我们如何通过 MarkLogic 完成此类工作。
实现这一目标需要所有三元组?
更新我用作的方法:
import module namespace sem = "http://marklogic.com/semantics" at
"/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $doc in sem:query-results-serialize(sem:sparql( "SELECT ?s WHERE
{?s <http://www.w3.org/2002/07/owl#sameAs>
<productId>}"),"xml")//s:uri/text()
return cts:element-value-query(xs:QName($doc), '001')
我得到的结果是:
cts:element-值查询(fn:QName("","id"), "001", ("lang=en"), 1)
cts:element-值查询(fn:QName("","productId"), "001", ("lang=en"), 1)
对此我有几个问题:
1.我的方法是否适合解决我上面提到的这种情况?
2. 我无法使用 sparql 查询的结果和扩展查询来搜索文档,你能更新一下我在这方面做错了什么吗?
您只能对 RDF 数据进行推理,因此您必须将 XML 结构转换为三元组。然后您可以定义这样的规则:
rule "item_ref_id" construct {
?s <item_id> ?o
} {
?s <item_ref_id> ?o
}
然后你只需要 select 规则 运行 SPARQL 就可以使用它。
HTH!
但是,您也可以在 MarkLogic 文档搜索中利用 sameAs 三元组。在处理搜索调用时,您可以识别基于 item_id
的搜索。然后,您可以使用从 SPARQL 调用返回的值来扩展 item_id
:
select * { ?s <http://www.w3.org/2002/07/owl#sameAs> <item_id> }
然后 运行 扩展搜索查询。
--加法--
您在更新后的问题中分享的代码几乎已经存在,您已成功从 productId 外推到 id。您只需要将元素查询包装成一个and-query,然后将其传递给cts:search。类似于:
import module namespace sem = "http://marklogic.com/semantics" at
"/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
let $qnames :=
for $id in sem:query-results-serialize(sem:sparql( "SELECT ?s WHERE
{?s <http://www.w3.org/2002/07/owl#sameAs
<item_id>}"),"xml")//s:uri/text()
return xs:QName($id)
return cts:search(collection(), cts:element-value-query($qnames, '001'))
HTH!
我已经通过这种方式为上述场景添加了关系:
(item1 uri)---> 具有唯一 --> id ---> 与 <--- productid <---- 具有唯一 <--- (item2 uri)
item_id ---> (hasValue-def) ---> 001
item_ref_id---> (hasValue-def) ----> abc
添加以下三元组后,我可以使用 item_id 搜索项目,使用推理作为:
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $doc in sem:query-results-serialize(
sem:sparql("SELECT * WHERE {?s <has-unique-key as#>/<https://www.w3.org/TR/2002/WD-owl-ref-20021112/#hasValue-def>/<http://www.w3.org/2002/07/owl#sameAs>* <001>}"), "xml")//s:uri/text()
return fn:doc($doc)
@grtjn
我已经找到了该修复的解决方案,它解决了搜索两个 ID 的查询,请检查:
Document1:
<item>
<item_id> 001 </item_id>
<color>blue</color>
</item>
Document2:
<item>
<item_ref_id>abc</item_ref_id>
<color>blue</color>
</item>
Triple:
<sem:triple>
<sem:subject>item_ref_id</sem:subject>
<sem:predicate>http://www.w3.org/2002/07/owl#sameAs</sem:predicate>
<sem:object>item_id</sem:object>
</sem:triple>
使用上面的结构,我 运行 下面的查询(从解决方案修改而来),它用 item_id:
解析了两个 id 的文档
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $id in sem:query-results-serialize(
sem:sparql( "SELECT ?s WHERE {?s <http://www.w3.org/2002/07/owl#sameAs> <item_id>}"),"xml")//s:uri/text()
return cts:search(collection(),cts:and-query((
cts:element-value-query(xs:QName($id), '001'))
))
如果我也通过 'abc' 进行搜索,它会起作用。
感谢您提供如何使用它的想法,它帮助我解决了这个问题。
假设我有两个 xml 文档: 文档 1:
<item>
<item_id> 001 </item_id>
<color>blue</color>
</item>
文档 2:
<item>
<item_ref_id>abc</item_ref_id>
<color>blue</color>
</item>
现在为了推理,我将定义一个三元组:
<item_ref_id> <http://www.w3.org/2002/07/owl#sameAs> <item_id>
如果我使用 <item_id>
= abc 编写 SPARQL 查询来获取 document2,它应该可以工作。
这是否可能通过推理实现,我们如何通过 MarkLogic 完成此类工作。
实现这一目标需要所有三元组?
更新我用作的方法:
import module namespace sem = "http://marklogic.com/semantics" at
"/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $doc in sem:query-results-serialize(sem:sparql( "SELECT ?s WHERE
{?s <http://www.w3.org/2002/07/owl#sameAs>
<productId>}"),"xml")//s:uri/text()
return cts:element-value-query(xs:QName($doc), '001')
我得到的结果是:
cts:element-值查询(fn:QName("","id"), "001", ("lang=en"), 1)
cts:element-值查询(fn:QName("","productId"), "001", ("lang=en"), 1)
对此我有几个问题: 1.我的方法是否适合解决我上面提到的这种情况? 2. 我无法使用 sparql 查询的结果和扩展查询来搜索文档,你能更新一下我在这方面做错了什么吗?
您只能对 RDF 数据进行推理,因此您必须将 XML 结构转换为三元组。然后您可以定义这样的规则:
rule "item_ref_id" construct {
?s <item_id> ?o
} {
?s <item_ref_id> ?o
}
然后你只需要 select 规则 运行 SPARQL 就可以使用它。
HTH!
但是,您也可以在 MarkLogic 文档搜索中利用 sameAs 三元组。在处理搜索调用时,您可以识别基于 item_id
的搜索。然后,您可以使用从 SPARQL 调用返回的值来扩展 item_id
:
select * { ?s <http://www.w3.org/2002/07/owl#sameAs> <item_id> }
然后 运行 扩展搜索查询。
--加法--
您在更新后的问题中分享的代码几乎已经存在,您已成功从 productId 外推到 id。您只需要将元素查询包装成一个and-query,然后将其传递给cts:search。类似于:
import module namespace sem = "http://marklogic.com/semantics" at
"/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
let $qnames :=
for $id in sem:query-results-serialize(sem:sparql( "SELECT ?s WHERE
{?s <http://www.w3.org/2002/07/owl#sameAs
<item_id>}"),"xml")//s:uri/text()
return xs:QName($id)
return cts:search(collection(), cts:element-value-query($qnames, '001'))
HTH!
我已经通过这种方式为上述场景添加了关系:
(item1 uri)---> 具有唯一 --> id ---> 与 <--- productid <---- 具有唯一 <--- (item2 uri)
item_id ---> (hasValue-def) ---> 001
item_ref_id---> (hasValue-def) ----> abc
添加以下三元组后,我可以使用 item_id 搜索项目,使用推理作为:
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $doc in sem:query-results-serialize(
sem:sparql("SELECT * WHERE {?s <has-unique-key as#>/<https://www.w3.org/TR/2002/WD-owl-ref-20021112/#hasValue-def>/<http://www.w3.org/2002/07/owl#sameAs>* <001>}"), "xml")//s:uri/text()
return fn:doc($doc)
@grtjn
我已经找到了该修复的解决方案,它解决了搜索两个 ID 的查询,请检查:
Document1:
<item>
<item_id> 001 </item_id>
<color>blue</color>
</item>
Document2:
<item>
<item_ref_id>abc</item_ref_id>
<color>blue</color>
</item>
Triple:
<sem:triple>
<sem:subject>item_ref_id</sem:subject>
<sem:predicate>http://www.w3.org/2002/07/owl#sameAs</sem:predicate>
<sem:object>item_id</sem:object>
</sem:triple>
使用上面的结构,我 运行 下面的查询(从解决方案修改而来),它用 item_id:
解析了两个 id 的文档import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
declare namespace s = "http://www.w3.org/2005/sparql-results#";
for $id in sem:query-results-serialize(
sem:sparql( "SELECT ?s WHERE {?s <http://www.w3.org/2002/07/owl#sameAs> <item_id>}"),"xml")//s:uri/text()
return cts:search(collection(),cts:and-query((
cts:element-value-query(xs:QName($id), '001'))
))
如果我也通过 'abc' 进行搜索,它会起作用。
感谢您提供如何使用它的想法,它帮助我解决了这个问题。