Mercurial Hosting > luan
changeset 257:c5c60eca33dd
allow Lucene search for 0 rows
git-svn-id: https://luan-java.googlecode.com/svn/trunk@258 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 28 Oct 2014 03:05:37 +0000 |
parents | 15122d724ce4 |
children | 4dfa86dbca45 |
files | dist/jars/luan-core-trunk.jar dist/jars/luan-logging-trunk.jar dist/jars/luan-lucene-trunk.jar dist/jars/luan-mail-trunk.jar dist/jars/luan-web-trunk.jar lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneSearcher.java |
diffstat | 7 files changed, 30 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/Lucene.luan Tue Oct 28 01:59:35 2014 +0000 +++ b/lucene/src/luan/modules/lucene/Lucene.luan Tue Oct 28 03:05:37 2014 +0000 @@ -17,15 +17,25 @@ end ) end - function index.get_document(query) + function index.get_first(query, sort) return index.Searcher( function(searcher) - local results, _, total_hits = searcher.search(query,1) - if total_hits == 0 then - return nil - elseif total_hits > 1 then - error( "found " .. total_hits .. " documents" ) - end - return results() + local results, _, total_hits = searcher.search(query,1,sort) + return results(), total_hits + end ) + end + + function index.get_document(query) + local doc, total_hits = index.get_first(query); + if total_hits > 1 then + error( "found " .. total_hits .. " documents" ) + end + return doc + end + + function index.count(query) + return index.Searcher( function(searcher) + local _, _, total_hits = searcher.search(query,0) + return total_hits end ) end
--- a/lucene/src/luan/modules/lucene/LuceneSearcher.java Tue Oct 28 01:59:35 2014 +0000 +++ b/lucene/src/luan/modules/lucene/LuceneSearcher.java Tue Oct 28 03:05:37 2014 +0000 @@ -16,6 +16,7 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.TotalHitCountCollector; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -159,10 +160,21 @@ return new Sort(flds); } + private static final LuanFunction nothingFn = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + return LuanFunction.NOTHING; + } + }; + public Object[] search( LuanState luan, LuanTable queryTbl, int n, LuanTable sortTbl ) throws LuanException, IOException { Query query = query(queryTbl); if( query == null ) throw luan.exception("invalid query"); + if( n==0 ) { + TotalHitCountCollector thcc = new TotalHitCountCollector(); + searcher.search(query,thcc); + return new Object[]{ nothingFn, 0, thcc.getTotalHits() }; + } TopDocs td = sortTbl==null ? searcher.search(query,n) : searcher.search(query,n,sort(luan,sortTbl)); final ScoreDoc[] scoreDocs = td.scoreDocs; LuanFunction results = new LuanFunction() {