在使用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);
}
}
答案 0 :(得分:2)
您必须使用Lucene查询语法。搜索public class DontDestroyOnLoad : MonoBehaviour
{
[SerializeField] string _id;
public static DontDestroyOnLoad Get(string id)
{
var instances = FindObjectsOfType<DontDestroyOnLoad>();
return instances.FirstOrDefault(i => i._id == id);
}
void Awake()
{
if (string.IsNullOrEmpty(_id))
{
_id = Guid.NewGuid().ToString();
}
var instance = Get(_id);
if (instance != null && instance != this)
{
Destroy(instance.gameObject);
}
DontDestroyOnLoad(gameObject) ;
}
}
代替搜索c*
。参见http://www.lucenetutorial.com/lucene-query-syntax.html