Created
March 5, 2015 18:14
-
-
Save chetanmeh/c1ccc4fa588ed1af467b to your computer and use it in GitHub Desktop.
Oak Lucene Index Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package org.apache.jackrabbit.oak.jcr; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
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.commons.JcrUtils; | |
import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorProvider; | |
import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider; | |
import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeStore; | |
import org.apache.jackrabbit.oak.plugins.segment.file.FileStore; | |
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 org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import static org.junit.Assert.fail; | |
public class LuceneJcr { | |
private final Logger log = LoggerFactory.getLogger(getClass()); | |
private NodeStore nodeStore; | |
private Repository repository; | |
public void initNodeStore() throws IOException { | |
FileStore fileStore = FileStore.newFileStore(new File("target/"+System.currentTimeMillis())).create(); | |
nodeStore = new SegmentNodeStore(fileStore); | |
} | |
public void initRepository() { | |
LuceneIndexProvider provider = new LuceneIndexProvider(); | |
Jcr jcr = new Jcr(nodeStore) | |
.withAsyncIndexing() | |
.with(new LuceneIndexEditorProvider()) | |
.with((QueryIndexProvider) provider) | |
.with((Observer) provider) | |
.withAsyncIndexing(); | |
repository = jcr.createRepository(); | |
log.info("Repository initialized"); | |
} | |
public void createLuceneIndex() throws RepositoryException { | |
Session session = createAdminSession(); | |
Node lucene = JcrUtils.getOrCreateByPath("/oak:index/lucene", "oak:Unstructured", | |
"oak:QueryIndexDefinition", session, false); | |
lucene.setProperty("compatVersion", 2); | |
lucene.setProperty("type", "lucene"); | |
lucene.setProperty("async", "async"); | |
Node rules = lucene.addNode("indexRules", "nt:unstructured"); | |
Node allProps = rules.addNode("nt:base") | |
.addNode("properties", "nt:unstructured") | |
.addNode("allProps", "oak:Unstructured"); | |
allProps.setProperty("name", ".*"); | |
allProps.setProperty("isRegexp", true); | |
allProps.setProperty("nodeScopeIndex", true); | |
session.save(); | |
session.logout(); | |
log.info("Lucene index created"); | |
} | |
private void createTestData() throws RepositoryException { | |
Session session = createAdminSession(); | |
Node test = session.getRootNode().addNode("test"); | |
test.setProperty("name", "torgeir"); | |
session.save(); | |
session.logout(); | |
log.info("Testdata created"); | |
} | |
private void performQuery() throws RepositoryException, InterruptedException { | |
final Session session = createAdminSession(); | |
TimeUnit.SECONDS.sleep(5); | |
log.info("Going to perform query"); | |
QueryManager qm =session.getWorkspace().getQueryManager(); | |
final Query q = qm.createQuery("select * from [nt:base] where " + | |
"contains(*,'torgeir')", Query.JCR_SQL2); | |
new RetryLoop(new RetryLoop.Condition() { | |
@Override | |
public String getDescription() { | |
return "Full text query"; | |
} | |
@Override | |
public boolean isTrue() throws Exception { | |
QueryResult r = q.execute(); | |
return r.getNodes().hasNext(); | |
} | |
}, 105, 200); | |
System.out.println(q.execute().getNodes().next()); | |
} | |
private Session createAdminSession() throws RepositoryException { | |
return repository.login(getAdminCredentials()); | |
} | |
private SimpleCredentials getAdminCredentials() { | |
return new SimpleCredentials("admin", "admin".toCharArray()); | |
} | |
public static void main(String[] args) throws Exception { | |
LuceneJcr test = new LuceneJcr(); | |
test.setUp(); | |
} | |
private void setUp() throws Exception { | |
initNodeStore(); | |
initRepository(); | |
createLuceneIndex(); | |
createTestData(); | |
performQuery(); | |
} | |
private static class RetryLoop { | |
private final long timeout; | |
static public interface Condition { | |
/** | |
* Used in failure messages to describe what was expected | |
*/ | |
String getDescription(); | |
/** | |
* If true we stop retrying. The RetryLoop retries on AssertionError, | |
* so if tests fail in this method they are not reported as | |
* failures but retried. | |
*/ | |
boolean isTrue() throws Exception; | |
} | |
public RetryLoop(Condition c, int timeoutSeconds, int intervalBetweenTriesMsec) { | |
timeout = System.currentTimeMillis() + timeoutSeconds * 1000L; | |
while (System.currentTimeMillis() < timeout) { | |
try { | |
if (c.isTrue()) { | |
return; | |
} | |
} catch (AssertionError ae) { | |
// Retry JUnit tests failing in the condition as well | |
reportException(ae); | |
} catch (Exception e) { | |
reportException(e); | |
} | |
try { | |
Thread.sleep(intervalBetweenTriesMsec); | |
} catch (InterruptedException ignore) { | |
} | |
} | |
onTimeout(); | |
fail("RetryLoop failed, condition is false after " + timeoutSeconds + " seconds: " | |
+ c.getDescription()); | |
} | |
/** | |
* Can be overridden to report Exceptions that happen in the retry loop | |
*/ | |
protected void reportException(Throwable t) { | |
} | |
/** | |
* Called if the loop times out without success, just before failing | |
*/ | |
protected void onTimeout() { | |
} | |
} | |
} |
@chetanmeh, Thanks for the code, tried running the example facing following issue, could you please help getting rid of warnings,
"Traversed 1000 nodes with filter Filter, consider creating an index or changing the query"
Thanks in advance..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anyone have working example of this?