首页 > 解决方案 > 在 GraphDB 的 Lucene 连接器中使用通配符

问题描述

我正在使用 GraphDB 的Lucene 连接器。我在我的三重存储中为实体代码构建索引my_index,我想使用这样的索引进行子字符串匹配。

例子。

实体代码:

FooBar
FooBaz
BazFoo

Lucene 连接器:

PREFIX :<http://www.ontotext.com/connectors/lucene#>
PREFIX inst:<http://www.ontotext.com/connectors/lucene/instance#>
INSERT DATA {
    inst:my_index :createConnector '''
{
  "fields": [
    {
      "fieldName": "code",
      "propertyChain": [
        "http://foo#identifier"
      ],
      "indexed": true,
      "stored": true,
      "analyzed": true,
      "multivalued": true,
      "facet": true
    }
  ],
  "types": [
    http://foo#MyType"
  ],
  "stripMarkup": false
}
''' .
}

SPARQL查询利用 Lucene 连接器:

PREFIX : <http://www.ontotext.com/connectors/lucene#>
PREFIX inst: <http://www.ontotext.com/connectors/lucene/instance#>

SELECT ?entity {
  ?search a inst:my_index ;
      :query "code:Foo*" ;
      :entities ?entity .
}

我想获取代码以Foo(ie, FooBar, FooBaz) 开头的所有实体,但我得到的是一个空的结果集。

我怎样才能得到它们?

编辑:

在尝试了 Vassil答案中的示例后,我发现问题可能与区分大小写有关。

行为:

标签: lucenewildcardgraphdb

解决方案


默认情况下,前缀搜索应该可以开箱即用。我怀疑您的查询还有另一个问题。如果你用 搜索所有可能的值会发生什么:query "*:*"

这是用于检查和重复数据集的测试用例。

生成样本虚拟数据

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
INSERT DATA
{
    <urn:1> a <urn:type>;
        rdfs:label "FooBar".
    <urn:2> a <urn:type>;
        rdfs:label "FooBaz".
    <urn:3> a <urn:type>;
        rdfs:label "BazFoo".
}

您还需要rdf:type为每个 RDF 资源定义语句。

创建 Lucene 连接器

PREFIX :<http://www.ontotext.com/connectors/lucene#>
PREFIX inst:<http://www.ontotext.com/connectors/lucene/instance#>
INSERT DATA {
    inst:my_index :createConnector '''
{
  "fields": [
    {
      "fieldName": "label",
      "propertyChain": [
        "http://www.w3.org/2000/01/rdf-schema#label"
      ],
      "indexed": true,
      "stored": true,
      "analyzed": true,
      "multivalued": true,
      "facet": true
    }
  ],
  "types": [
    "urn:type"
  ],
  "stripMarkup": false
}
''' .
}

连接器将索引所有类的所有rdfs:labelurn:type

测试前缀搜索

PREFIX : <http://www.ontotext.com/connectors/lucene#>
PREFIX inst: <http://www.ontotext.com/connectors/lucene/instance#>

SELECT ?entity {
  ?search a inst:my_index ;
      :query "label:Foo*" ;
      :entities ?entity .
}

数据库返回urn:1urn:2


推荐阅读