我正在文件节点存储中尝试使用带有lucene的长颈鹿橡树。索引定义记录已成功创建,但似乎未创建索引记录。我正在研究的完整项目是here
我在代码中所做的是初始化存储库,创建lucene索引定义,添加具有单个属性“ name”和值“ foo”的测试数据。睡眠5秒钟以完成异步索引编制,然后在重试循环中执行以下查询从[nt:base]中选择*,其中contains(。,'foo')。
没有结果返回。我已经尝试过Oak-run控制台来检索oak:index / lucene目录上的lc信息,它也不会显示任何结果。
这是代码的主要部分
public static void main( String[] args ) throws Exception
{
init();
createLuceneIndex();
createTestData();
performQuery();
}
private static void init() throws InvalidFileStoreVersionException, IOException {
LuceneIndexProvider provider = new LuceneIndexProvider();
FileStore fs = FileStoreBuilder.fileStoreBuilder(new File("repository")).build();
nodestore = SegmentNodeStoreBuilders.builder(fs).build();
repository = new Jcr(new Oak(nodestore))
.withAsyncIndexing("async", 3)
.with(new LuceneIndexEditorProvider())
.with((QueryIndexProvider) provider)
.with((Observer)provider)
.withAsyncIndexing()
.createRepository();
}
private static void createLuceneIndex() throws RepositoryException {
Session session = createAdminSession();
Node indexesNode = session.getRootNode().getNode("oak:index");
IndexDefinitionBuilder idxBuilder = new IndexDefinitionBuilder();
IndexRule indexRules = idxBuilder.indexRule("nt:unstructured");
indexRules.sync();
indexRules.property("name").analyzed().nodeScopeIndex();
idxBuilder.async("async");
idxBuilder.includedPaths("/");
Node documentIndex = indexesNode.addNode("lucene", "oak:QueryIndexDefinition");
idxBuilder.build(documentIndex);
session.save();
session.logout();
}
private static void createTestData() throws LoginException, RepositoryException {
Session session = createAdminSession();
Node test = session.getRootNode().addNode("test");
test.setProperty("name", "foo");
session.save();
session.logout();
}
private static void performQuery() throws Exception {
final Session session = createAdminSession();
TimeUnit.MICROSECONDS.sleep(5);
QueryManager qm = session.getWorkspace().getQueryManager();
final Query q = qm.createQuery("select * from [nt:base] where contains(., 'foo')", Query.JCR_SQL2);
new RetryLoop(new RetryLoop.Condition() {
public String getDescription() {
return "Full text query";
}
public boolean isTrue() throws Exception {
QueryResult r = q.execute();
return r.getNodes().hasNext();
}
}, 20, 500);
}
pom文件依赖项
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
<version>1.21-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-core</artifactId>
<version>1.21-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
<version>1.21-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-segment-tar</artifactId>
<version>1.21-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-lucene</artifactId>
<version>1.21-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
我对POM和代码进行了一些更改,并且运行良好。请看下面:
package org.codeaffectionado.training;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.jackrabbit.oak.Oak;
import org.apache.jackrabbit.oak.jcr.Jcr;
import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorProvider;
import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider;
import org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder;
import org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder.IndexRule;
import org.apache.jackrabbit.oak.segment.SegmentNodeStoreBuilders;
import org.apache.jackrabbit.oak.segment.file.FileStore;
import org.apache.jackrabbit.oak.segment.file.FileStoreBuilder;
import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
import org.apache.jackrabbit.oak.spi.commit.Observer;
import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import static org.codeaffectionado.training.App.RetryLoop.createAdminSession;
public class App
{
private static NodeStore nodestore;
private static Repository repository;
public static void main( String[] args ) throws Exception
{
init();
createLuceneIndex();
createTestData();
RetryLoop.performQuery();
}
private static void init() throws InvalidFileStoreVersionException, IOException {
LuceneIndexProvider provider = new LuceneIndexProvider();
FileStore fs = FileStoreBuilder.fileStoreBuilder(new File("repository")).build();
nodestore = SegmentNodeStoreBuilders.builder(fs).build();
repository = new Jcr(new Oak(nodestore))
.withAsyncIndexing("async", 3)
.with(new LuceneIndexEditorProvider())
.with((QueryIndexProvider) provider)
.with((Observer)provider)
.withAsyncIndexing()
.createRepository();
}
private static void createLuceneIndex() throws RepositoryException {
Session session = createAdminSession();
Node indexesNode = session.getRootNode().getNode("oak:index");
IndexDefinitionBuilder idxBuilder = new IndexDefinitionBuilder();
IndexRule indexRules = idxBuilder.indexRule("nt:unstructured");
indexRules.sync();
indexRules.property("name").analyzed().nodeScopeIndex();
idxBuilder.async("async");
idxBuilder.includedPaths("/");
Node documentIndex = indexesNode.addNode("lucene", "oak:QueryIndexDefinition");
idxBuilder.build(documentIndex);
session.save();
session.logout();
}
private static void createTestData() throws LoginException, RepositoryException {
Session session = createAdminSession();
Node test = session.getRootNode().addNode("test");
test.setProperty("name", "foo");
session.save();
session.logout();
}
public static class RetryLoop {
private final long timeout;
static public interface Condition {
String getDescription();
boolean isTrue() throws Exception;
}
public RetryLoop(Condition c, int timeoutSeconds, int intervalBetweenTriesMsec) throws Exception {
timeout = System.currentTimeMillis() + timeoutSeconds * 1000L;
while(System.currentTimeMillis() < timeout) {
try {
if(c.isTrue()) {
return ;
}
} catch(AssertionError ae) {
} catch(Exception e) {
}
try {
Thread.sleep(intervalBetweenTriesMsec);
} catch(InterruptedException ignore) {
}
}
throw new Exception("timeout occur");
}
static Session createAdminSession() throws LoginException, RepositoryException {
return repository.login(getCred());
}
private static SimpleCredentials getCred() {
return new SimpleCredentials("admin", "admin".toCharArray());
}
private static void performQuery() throws Exception {
final Session session = createAdminSession();
TimeUnit.MICROSECONDS.sleep(5);
QueryManager qm = session.getWorkspace().getQueryManager();
final Query q = qm.createQuery("select * from [nt:base] where contains(., 'foo')", Query.JCR_SQL2);
new RetryLoop(new RetryLoop.Condition() {
public String getDescription() {
return "Full text query";
}
public boolean isTrue() throws Exception {
QueryResult r = q.execute();
return r.getNodes().hasNext();
}
}, 20, 500);
}
}}
POM更改:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codeaffectionado.training</groupId>
<artifactId>jackrabbit-tutorial</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jackrabbit-tutorial</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
<version>1.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-core</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-segment-tar</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-lucene</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.codeaffectionado.training.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>