我尝试使用 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
- 那么,如何建立正确的索引并提出正确的请求来搜索文件内容呢?
- 如何在pdf文档中搜索?