Skip to content

Commit

Permalink
searchCollector (use in limited conf)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleujin committed Jul 31, 2015
1 parent 897e558 commit ab1f61b
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<target name="copy" depends="publish">
<echo>Copy to ICS6</echo>
<copy file="publish/isearcher_${version.number}.${build.number}.jar" todir="../../workspace/ICS6/ics/WEB-INF/lib/" />
<copy file="publish/isearcher_${version.number}.${build.number}.jar" todir="../ICS6_WORK/ics/WEB-INF/lib/" />
<echo>Copy to ICS6Extend</echo>
<copy file="publish/isearcher_${version.number}.${build.number}.jar" todir="../../workspace/ICS6Extend/lib/common/" />
<echo>Copy to AradonExtend</echo>
Expand Down
Binary file modified publish/isearcher_4.10.jar
Binary file not shown.
18 changes: 16 additions & 2 deletions src/net/ion/nsearcher/common/WriteDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ public class WriteDocument extends AbDocument {
private IndexSession isession;
private boolean newDoc = false;
private float boost = 1.0f ;
private Document doc;

public WriteDocument(IndexSession indexSession, String docId) {
this.isession = indexSession ;
this.docId = docId;
this.doc = new Document();
}

public WriteDocument(IndexSession indexSession, String docId, Document doc) {
this.isession = indexSession ;
this.docId = docId;
this.doc = (doc == null) ? new Document() : doc;
}

public WriteDocument(IndexSession indexSession) {
this.isession = indexSession ;
this.docId = new ObjectId().toString();
this.newDoc = true ;
this.doc = new Document();
}


public String idValue() {
return docId;
}
Expand All @@ -72,7 +80,6 @@ public WriteDocument boost(float boost){
public Document toLuceneDoc() {

FieldIndexingStrategy strategy = isession.fieldIndexingStrategy();
Document doc = new Document();
StringBuilder bodyBuilder = new StringBuilder(512);
bodyBuilder.append(docId + " ") ;

Expand Down Expand Up @@ -194,6 +201,12 @@ public WriteDocument text(String fieldName, String value) {
add(MyField.text(fieldName, value));
return this;
}

public WriteDocument stext(String fieldName, String value) {
add(MyField.manual(fieldName, value, Store.YES, true, MyFieldType.Text));
return this;
}


public WriteDocument number(String fieldName, long value) {
add(MyField.number(fieldName, value));
Expand Down Expand Up @@ -256,4 +269,5 @@ public Void insertVoid() throws IOException {
return null ;
}


}
10 changes: 10 additions & 0 deletions src/net/ion/nsearcher/index/IndexSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.ion.framework.util.MapUtil;
import net.ion.nsearcher.common.AbDocument.Action;
import net.ion.nsearcher.common.FieldIndexingStrategy;
import net.ion.nsearcher.common.MyField;
import net.ion.nsearcher.common.SearchConstant;
import net.ion.nsearcher.common.WriteDocument;
import net.ion.nsearcher.search.SingleSearcher;
Expand All @@ -16,6 +17,7 @@
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;

Expand Down Expand Up @@ -72,6 +74,14 @@ public WriteDocument newDocument(){
return new WriteDocument(this) ;
}


public WriteDocument loadDocument(String docId) throws IOException, ParseException {
Document findDoc = searcher.central().newSearcher().createRequestByKey(docId).findOne().toLuceneDoc() ;
WriteDocument result = new WriteDocument(this, docId, findDoc);
return result;
}


public FieldIndexingStrategy fieldIndexingStrategy() {
return fieldIndexingStrategy;
}
Expand Down
26 changes: 26 additions & 0 deletions src/net/ion/nsearcher/search/AbstractDocCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.ion.nsearcher.search;

import java.io.IOException;
import java.util.Set;

import org.apache.lucene.index.DirectoryReader;

import net.ion.nsearcher.common.ReadDocument;

public abstract class AbstractDocCollector implements DocCollector {
public abstract ColResult accept(ReadDocument doc) ;

public ColResult accept(DirectoryReader dreader, SearchRequest sreq, int docId) throws IOException{
return accept(toDoc(dreader, sreq, docId)) ;
}

private ReadDocument toDoc(DirectoryReader dreader, SearchRequest sreq, int docId) throws IOException {
Set<String> fields = sreq.selectorField();
if (fields == null || fields.size() == 0) {
return ReadDocument.loadDocument(dreader.document(docId));
}
return ReadDocument.loadDocument(dreader.document(docId, sreq.selectorField()));
}


}
20 changes: 20 additions & 0 deletions src/net/ion/nsearcher/search/DocCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.ion.nsearcher.search;

import java.io.IOException;

import org.apache.lucene.index.DirectoryReader;

public interface DocCollector {

public enum ColResult {
ACCEPT, REVOKE, BREAK
}

public final static DocCollector BLANK = new DocCollector() {
public ColResult accept(DirectoryReader dreader, SearchRequest sreq, int docId) {
return ColResult.ACCEPT;
}
};

public ColResult accept(DirectoryReader dreader, SearchRequest sreq, int docId) throws IOException ;
}
32 changes: 32 additions & 0 deletions src/net/ion/nsearcher/search/PageCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.ion.nsearcher.search;

import java.io.IOException;

import net.ion.framework.db.Page;

import org.apache.lucene.index.DirectoryReader;

public class PageCollector implements DocCollector{

private Page page ;
private DocCollector pre;
private int count = -1 ;

public PageCollector(Page page, DocCollector pre){
this.page = page ;
this.pre = pre ;
}

@Override
public ColResult accept(DirectoryReader dreader, SearchRequest sreq, int docId) throws IOException {
if (pre.accept(dreader, sreq, docId) == ColResult.ACCEPT){
count++ ;
if (count >= page.getStartLoc() && count < page.getEndLoc()){
return ColResult.ACCEPT ;
} else if (count == page.getEndLoc()){
return ColResult.BREAK ;
}
}
return ColResult.REVOKE ;
}
}
10 changes: 10 additions & 0 deletions src/net/ion/nsearcher/search/SearchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SearchRequest {
private Set<String> columns = SetUtil.newSet() ;
private Set<String> lazyColumns = SetUtil.newSet() ;
private String userDefine = "";
private DocCollector collector = DocCollector.BLANK;

SearchRequest(Searcher searcher, Query query){
this.searcher = searcher ;
Expand Down Expand Up @@ -229,5 +230,14 @@ public StoredFieldVisitor selector(){
return columns.size() == 0 ? null : new DocumentStoredFieldVisitor(columns) ;
}

public SearchRequest collect(DocCollector collector) {
this.collector = collector ;
return this;
}

public DocCollector collector(){
return collector ;
}


}
25 changes: 19 additions & 6 deletions src/net/ion/nsearcher/search/SearchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ public class SearchResponse {
private final long endTime;
private Future<Void> postFuture ;
private List<Integer> docs ;
private TopDocs tdocs;
private SearchResponse(ISearchable searcher, SearchRequest sreq, List<Integer> docs, TopDocs tdocs, long startTime) {
private int totalCount ;
private SearchResponse(ISearchable searcher, SearchRequest sreq, List<Integer> docs, int totalCount, long startTime) {
this.searcher = searcher ;
this.sreq = sreq ;
this.startTime = startTime;
this.endTime = System.currentTimeMillis();
this.docs = docs ;
this.tdocs = tdocs ;
this.totalCount = totalCount ;
}

public static SearchResponse create(ISearchable searcher, SearchRequest sreq, List<Integer> docs, int totalCount, long startTime) throws IOException {
return new SearchResponse(searcher, sreq, makeDocument(sreq, docs), totalCount, startTime) ;
}

public static SearchResponse create(ISearchable searcher, SearchRequest sreq, TopDocs docs, long startTime) throws IOException {
return new SearchResponse(searcher, sreq, makeDocument(searcher, sreq, docs), docs, startTime);
return new SearchResponse(searcher, sreq, makeDocument(sreq, docs), docs.totalHits, startTime);
}

public int totalCount() {
return tdocs.totalHits ;
return totalCount ;
// 전체 total은 searcherImpl이 구함.
// return searcher.totalCount(sreq, sreq.getFilter()) ;
}
Expand Down Expand Up @@ -87,7 +91,16 @@ public List<ReadDocument> getDocument() throws IOException{
return eachDoc(EachDocHandler.TOLIST) ;
}

private static List<Integer> makeDocument(ISearchable searcher, SearchRequest sreq, TopDocs docs) {
private static List<Integer> makeDocument(SearchRequest sreq, List<Integer> docs) {
List<Integer> result = ListUtil.newList() ;

for (int i = sreq.skip(); i < Math.min(sreq.limit(), docs.size()); i++) {
result.add(docs.get(i));
}
return result;
}

private static List<Integer> makeDocument(SearchRequest sreq, TopDocs docs) {
ScoreDoc[] sdocs = docs.scoreDocs;
List<Integer> result = ListUtil.newList() ;

Expand Down
39 changes: 38 additions & 1 deletion src/net/ion/nsearcher/search/SingleSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;

import net.ion.framework.util.Debug;
import net.ion.framework.util.ListUtil;
import net.ion.nsearcher.common.IKeywordField;
import net.ion.nsearcher.common.ReadDocument;
import net.ion.nsearcher.config.Central;
import net.ion.nsearcher.config.SearchConfig;
import net.ion.nsearcher.reader.InfoReader;
import net.ion.nsearcher.search.DocCollector.ColResult;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
Expand All @@ -50,7 +58,7 @@ public static SingleSearcher create(SearchConfig sconfig, Central central) throw
return new SingleSearcher(central, DirectoryReader.open(central.dir()));
}

public SearchResponse search(SearchRequest sreq, Filter filters) throws IOException {
public SearchResponse search(final SearchRequest sreq, Filter filters) throws IOException {
reloadReader();

Lock rlock = central.readLock() ;
Expand All @@ -59,6 +67,33 @@ public SearchResponse search(SearchRequest sreq, Filter filters) throws IOExcept
locked = rlock.tryLock();
long startTime = System.currentTimeMillis();

if (sreq.collector() != AbstractDocCollector.BLANK){
final AtomicInteger total = new AtomicInteger() ;
final List<Integer> docs = ListUtil.newList() ;
try {
isearcher.search(sreq.query(), filters, new Collector() {
public void setScorer(Scorer scorer) throws IOException {
}
public void setNextReader(AtomicReaderContext atomicreadercontext) throws IOException {
}
public void collect(int docId) throws IOException {
ColResult cresult = sreq.collector().accept(dreader, sreq, docId);
if (cresult == ColResult.ACCEPT) {
docs.add(docId) ;
total.incrementAndGet() ;
}
if (cresult == ColResult.BREAK) throw new IllegalStateException("break") ;
}
public boolean acceptsDocsOutOfOrder() {
return false;
}
});
} catch(IllegalStateException ignore){
}
return SearchResponse.create(this, sreq, docs, total.intValue(), startTime) ;
}


TopDocs docs = isearcher.search(sreq.query(), filters, sreq.limit(), sreq.sort());
return SearchResponse.create(this, sreq, docs, startTime);
} finally {
Expand All @@ -79,6 +114,8 @@ public Document findById(String id) throws IOException{
public int totalCount(SearchRequest sreq, Filter filters) {
try {
// reloadReader() ;
if (sreq.collector() == AbstractDocCollector.BLANK) throw new IllegalArgumentException("with collector condition, this method meanless") ;

TopDocs docs = isearcher.search(sreq.query(), filters, Integer.MAX_VALUE);
return docs.totalHits;
} catch (IOException e) {
Expand Down
8 changes: 0 additions & 8 deletions src/org/apache/lucene/analysis/ko/MyKoreanAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@
import java.io.Reader;
import java.io.StringReader;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import net.ion.framework.util.IOUtil;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.ko.HanjaMappingFilter;
import org.apache.lucene.analysis.ko.KoreanFilter;
import org.apache.lucene.analysis.ko.KoreanTokenizer;
import org.apache.lucene.analysis.ko.PunctuationDelimitFilter;
import org.apache.lucene.analysis.ko.WordSegmentFilter;
import org.apache.lucene.analysis.standard.ClassicFilter;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.StopwordAnalyzerBase;
Expand Down
25 changes: 25 additions & 0 deletions test/net/ion/nsearcher/index/TestDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,29 @@ public Document handle(IndexSession isession) throws Exception {
request.find().debugPrint();
}


public void testLoadDocument() throws Exception {
Document doc = indexer.index(new IndexJob<Document>() {
public Document handle(IndexSession isession) throws Exception {
final WriteDocument writeDoc = isession.newDocument("bleujin").keyword("@path", "_emp").keyword("@path", "/ion").stext("explain", "hello bleujin");
isession.insertDocument(writeDoc);
return writeDoc.toLuceneDoc();
}
});

// cen.newSearcher().createRequestByKey("bleujin").find().debugPrint();

indexer.index(new IndexJob<Void>() {
@Override
public Void handle(IndexSession isession) throws Exception {
isession.loadDocument("bleujin").number("age", 20).update() ;
return null;
}
}) ;

ReadDocument rdoc = cen.newSearcher().createRequest("").findOne() ;
assertEquals("hello bleujin", rdoc.asString("explain"));
assertEquals(true, cen.newSearcher().createRequestByTerm("explain", "bleujin").findOne() != null) ;
}

}
Loading

0 comments on commit ab1f61b

Please # to comment.