我尝试使用Oak版本1.16.0在文件内容中实现全文搜索。
试图像Oak文档中所述创建索引来索引所有属性。
/oak:index/assetType
- jcr:primaryType = "oak:QueryIndexDefinition"
- type = "lucene"
- compatVersion = 2
- async = "async"
+ indexRules
- jcr:primaryType = "nt:unstructured"
+ nt:base
+ properties
- jcr:primaryType = "nt:unstructured"
+ allProps
- name = ".*"
- isRegexp = true
- nodeScopeIndex = true
public static void createIndex(Repository repository) {
Session session = null;
try {
session = repository.login();
Node root = session.getRootNode();
Node index = root.getNode("oak:index");
Node lucineIndex = index.addNode("assetType","oak:QueryIndexDefinition");
lucineIndex.setProperty("compatVersion", "2");
lucineIndex.setProperty("type", "lucene");
lucineIndex.setProperty("async", "async");
Node rules = lucineIndex.addNode("indexRules", "nt:unstructured");
Node base = rules.addNode("nt:base");
Node properties = base.addNode("properties", "nt:unstructured");
Node allProps = properties.addNode("allProps");
allProps.setProperty("jcr:content", ".*");
allProps.setProperty("isRegexp", true);
allProps.setProperty("nodeScopeIndex", true);
session.save();
} catch (LoginException e) {
e.printStackTrace();
} catch (RepositoryException e) {
e.printStackTrace();
} finally {
session.logout();
}
}
public static void saveFileIfNotExist(byte[] rawFile, String fileName, String folderName, String mimeType, Repository repository) {
Session session = null;
try {
session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
Node root = session.getRootNode();
Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(rawFile));
if(!root.hasNode(folderName)) {
System.out.println("NO FOLDER");
Node folder = root.addNode(folderName, "nt:folder");
Node file = folder.addNode(fileName, "nt:file");
Node content = file.addNode("jcr:content", "nt:resource");
content.setProperty("jcr:mimeType", mimeType);
content.setProperty("jcr:data", binary);
} else {
System.out.println("FOLDER EXIST");
}
session.save();
}
catch (RepositoryException e) {
e.printStackTrace();
} finally {
session.logout();
}
}
文件内容:
An implementation of the Value interface must override the inherited method
Object.equals(Object) so that, given Value instances V1 and V2,
V1.equals(V2) will return true if.
DocumentNodeStore rdb = new DocumentNodeStore(new RDBDocumentNodeStoreBuilder().setRDBConnection(dataSource));
Repository repo = new Jcr(new Oak(rdb)).with(new OpenSecurityProvider()).createRepository();
createIndex(repo);
byte[] rawFile = readBytes("D:\\file.txt");
saveFileIfNotExist(rawFile, "txt_folder", "text_file", "text/plain", repo);
Session session = null;
try {
session = repo.login();
Node root = session.getRootNode();
Node index = root.getNode("oak:index");
QueryManager queryManager = session.getWorkspace().getQueryManager();session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery("SELECT * FROM [nt:resource] AS s WHERE CONTAINS(s.*, '*so*') option(traversal warn)", Query.JCR_SQL2);
QueryResult result = query.execute();
RowIterator ri = result.getRows();
while (ri.hasNext()) {
Row row = ri.nextRow();
System.out.println("Row: " + row.toString());
}
} catch (RepositoryException e) {
e.printStackTrace();
}
finally {
session.logout();
((RepositoryImpl) repo).shutdown();
rdb.dispose();
}
但是什么也没有返回,并在日志中发出警告:
2019-10-02 18:27:35,821 [main] WARN QueryImpl - Traversal query (query without index): SELECT * FROM [nt:resource] AS s WHERE CONTAINS(s.*, '*so*') option(traversal warn); consider creating an index
我尚未仔细检查所有代码片段,但似乎缺少的一件事是设置了一个异步索引器(您的索引def具有async="async"
)。只是从我的头顶打字,但做类似的事情>
new Oak(rdb)).with(new OpenSecurityProvider().withAsyncIndexing("async", 5) // 5 is number seconds to define period at which async indexer would run
Btw,由于它是一个异步索引,因此您需要稍等片刻,结果才能显示在查询中。但是,即使未显示结果,查询仍应选择您的索引。