Hi John,
if you don't mind I'll paste my code right here - if you find it
inconvinient, I'll send the sources to your email.
Btw, I enabled logging and found interesting detail - in case when I
invoke Thread.sleep(),
there is "proj.zoie.impl.indexing.AsyncDataConsumer: flushBuffer: post-
flush: currentVersion: 0" line before search,
which seems like guaranteing search results.
When there is no any delay, search finds nothing and this line goes
after search.
-----------------------Data.java--------------------------
package test.zoie.domain;
public class Data {
private long id;
private String content;
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
-------------------DataIndexable.java-------------------------
package test.zoie.domain;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import proj.zoie.api.indexing.ZoieIndexable;
public class DataIndexable implements ZoieIndexable {
private Data _data;
public DataIndexable(Data data) {
_data = data;
}
public long getUID() {
return _data.getId();
}
public IndexingReq[] buildIndexingReqs() {
Document doc = new Document();
doc.add(new
Field("content",_data.getContent(),Store.YES,Index.ANALYZED));
doc.add(new Field("id", String.valueOf(_data.getId()), Store.YES,
Index.ANALYZED_NO_NORMS));
return new IndexingReq[]{new IndexingReq(doc)};
}
public boolean isDeleted() {
return"_MARKED_FOR_DELETE".equals(_data.getContent());
}
public boolean isSkip(){
return "_MARKED_FOR_SKIP".equals(_data.getContent());
}
}
-----------DataIndexableInterpreter.java---------------------------
package test.zoie.domain;
import proj.zoie.api.indexing.ZoieIndexable;
import proj.zoie.api.indexing.ZoieIndexableInterpreter;
public class DataIndexableInterpreter implements
ZoieIndexableInterpreter<Data> {
public ZoieIndexable interpret(Data src){
return new DataIndexable(src);
}
@Override
public ZoieIndexable convertAndInterpret(Data src) {
return new DataIndexable(src);
}
}
---------------------Main.java------------------------------
package test.zoie.main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import proj.zoie.api.ZoieException;
import proj.zoie.api.ZoieIndexReader;
import proj.zoie.api.DataConsumer.DataEvent;
import proj.zoie.api.indexing.IndexReaderDecorator;
import proj.zoie.impl.indexing.DefaultIndexReaderDecorator;
import proj.zoie.impl.indexing.SimpleReaderCache;
import proj.zoie.impl.indexing.ZoieConfig;
import proj.zoie.impl.indexing.ZoieSystem;
import test.zoie.domain.Data;
import test.zoie.domain.DataIndexableInterpreter;
public class Main {
public static final String INDEX_DIR = "myIdxDir";
private static ZoieSystem indexingSystem;
public static void delete(File file){
if (!file.exists()){
return;
}
if (file.isDirectory()){
for (File child : file.listFiles()){
delete(child);
file.delete();
}
}
else {
file.delete();
}
}
public static void indexDoc(int id) throws ZoieException{
Data[] data = new Data[1];
data[0] = new Data();
data[0].setId(id);
data[0].setContent("tst");
ArrayList<DataEvent> eventList = new
ArrayList<DataEvent>(data.length);
for (Data datum : data) {
eventList.add(new DataEvent<Data>(datum, String.valueOf(0)));
}
indexingSystem.consume(eventList);
}
public static int findDoc(String id) throws IOException,
ParseException{
List<ZoieIndexReader> readerList = indexingSystem.getIndexReaders();
MultiReader reader = new MultiReader(readerList
.toArray(new IndexReader[readerList.size()]), false);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_20, "id",
indexingSystem.getAnalyzer());
TopDocs docs = searcher.search(parser.parse("id:"+id), 10);
int found = docs.totalHits;
searcher.close();
indexingSystem.returnIndexReaders(readerList);
return found;
}
public static void main(String[] args) throws ZoieException,
IOException,
ParseException, InterruptedException {
File idxDir = new File(INDEX_DIR);
delete(idxDir);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
Similarity similarity = new DefaultSimilarity();
IndexReaderDecorator decorator = new DefaultIndexReaderDecorator();
ZoieConfig config = new ZoieConfig();
config.setReadercachefactory(SimpleReaderCache.FACTORY);
indexingSystem = new ZoieSystem(idxDir,
new DataIndexableInterpreter(), decorator, config);
indexingSystem.start();
indexDoc(5);
// Thread.sleep(200);
System.out.println("Found: "+findDoc("5"));
indexingSystem.shutdown();
delete(idxDir);
}
}