У меня проблемы с тем, чтобы заставить полнотекстовый поиск работать с Apache Lucene. Я могу получить имена, когда введу всю этикетку, например "cat", но ввод "c" ничего не дает. Я использую RDF4J. Это SPARQL-запрос, который я использую:
SELECT DISTINCT ?e2 ?altLabel ?label ?description WHERE
{
{
?e2 search:matches ?match .
?match search:query ?string ;
search:property ?labelIri ;
search:snippet ?altLabel
}
?e2 ?labelIri ?label.
}
LuceneSailConnection затем преобразует его в:
Distinct
Projection
ProjectionElemList
ProjectionElem "e2"
ProjectionElem "label"
ProjectionElem "description"
Extension
ExtensionElem (description)
Var (name=description)
Join
Join
Join
StatementPattern
Var (name=e2)
Var (name=_const_232d65d1_uri, value=http://www.openrdf.org/contrib/lucenesail#matches, anonymous)
Var (name=match)
StatementPattern
Var (name=match)
Var (name=_const_802884e6_uri, value=http://www.openrdf.org/contrib/lucenesail#query, anonymous)
Var (name=string)
StatementPattern
Var (name=match)
Var (name=_const_f59a94f7_uri, value=http://www.openrdf.org/contrib/lucenesail#property, anonymous)
Var (name=labelIri)
StatementPattern
Var (name=e2)
Var (name=labelIri)
Var (name=label)
Это код, используемый для индексации концепций и их меток в базе знаний:
@Override
public void indexLocalKb(KnowledgeBase aKb) throws IOException
{
Analyzer analyzer = new StandardAnalyzer();
Directory directory = FSDirectory
.open(new File(luceneIndexDir, aKb.getRepositoryId()).toPath());
IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer));
try (RepositoryConnection conn = getConnection(aKb)) {
RepositoryResult<Statement> stmts = RdfUtils
.getStatementsSparql(conn, null, aKb.getLabelIri(), null,
Integer.MAX_VALUE, false, null);
while (stmts.hasNext()) {
Statement stmt = stmts.next();
String id = stmt.getSubject().stringValue();
String label = stmt.getObject().stringValue();
String predicate = stmt.getPredicate().stringValue();
indexEntity(id, label, predicate, indexWriter);
}
}
indexWriter.close();
}
private void indexEntity(String aId, String aLabel, String aPredictate,
IndexWriter aIndexWriter)
{
try {
String FIELD_ID = "id";
String FIELD_CONTENT = "label";
Document doc = new Document();
doc.add(new StringField(FIELD_ID, aId, Field.Store.YES));
doc.add(new StringField(FIELD_CONTENT, aLabel, Field.Store.YES));
aIndexWriter.addDocument(doc);
aIndexWriter.commit();
log.info("Entity indexed with id [{}] and label [{}], predicate [{}]",
aId, aLabel, aPredictate);
}
catch (IOException e) {
log.error("Could not index entity with id [{}] and label [{}]", aId, aLabel);
}
}
Я бы сказал, что если вы хотите искать вещи, которые начинаются с c
, то, согласно синтаксису запроса Lucene, запрос должен быть c*
. Ср. docs.rdf4j.org/programming Раздел 5.1.2. Полнотекстовый поиск
@rec это звучит как правильный ответ для меня - действительно хотите опубликовать его как ответ?
@rec Спасибо, это сработало.
Вы должны использовать синтаксис запросов Lucene. Ищите c*
вместо c
. См. http://www.lucenetutorial.com/lucene-query-syntax.html
Вы должны хотя бы упомянуть, какой API вы используете, учитывая, что Lucene соотв. Полнотекстовый поиск не является частью стандарта SPARQL. (Я бы предположил, что это Сезам или RDF4J)