changeset 1342:60599adc27b8

add lucene search options
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Feb 2019 12:09:51 -0700
parents a015a0b5c388
children 7d9a1f8894b0
files conv.txt src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java website/src/manual.html
diffstat 4 files changed, 43 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Tue Feb 19 08:14:40 2019 -0700
+++ b/conv.txt	Wed Feb 20 12:09:51 2019 -0700
@@ -1,3 +1,4 @@
+lucene search sort
 call
 LuanState
 LuanLogger
--- a/src/luan/modules/lucene/Lucene.luan	Tue Feb 19 08:14:40 2019 -0700
+++ b/src/luan/modules/lucene/Lucene.luan	Wed Feb 20 12:09:51 2019 -0700
@@ -73,16 +73,30 @@
 		java_index.close()
 	end
 
-	function index.search(query, from, to, sort)
+	function index.search( query, from, to, options )
 		from or error "missing 'from' parameter"
 		to or error "missing 'to' parameter"
+		options = options or {}
+		local explain_fld = options.explain
+		local score_fld = options.score
 		local results = {}
-		local function fn(i,doc_fn)
+		local function fn(i,doc_fn,score)
 			if i >= from then
-				results[#results+1] = doc_fn()
+				local doc
+				if explain_fld == nil then
+					doc = doc_fn()
+				else
+					local explanation
+					doc, explanation = doc_fn("explain")
+					doc[explain_fld] = explanation.toString()
+				end
+				if score_fld ~= nil then
+					doc[score_fld] = score
+				end
+				results[#results+1] = doc
 			end
 		end
-		local total_hits = index.advanced_search(query,fn,to,sort)
+		local total_hits = index.advanced_search(query,fn,to,options.sort)
 		return results, total_hits
 	end
 
--- a/src/luan/modules/lucene/LuceneIndex.java	Tue Feb 19 08:14:40 2019 -0700
+++ b/src/luan/modules/lucene/LuceneIndex.java	Wed Feb 20 12:09:51 2019 -0700
@@ -53,7 +53,7 @@
 import org.apache.lucene.search.ScoreDoc;
 import org.apache.lucene.search.Collector;
 import org.apache.lucene.search.Scorer;
-import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.highlight.Formatter;
 import org.apache.lucene.search.highlight.Highlighter;
 import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
@@ -355,16 +355,24 @@
 
 	private static class DocFn extends LuanFunction {
 		final IndexSearcher searcher;
+		final Query query;
 		int docID;
 
-		DocFn(Luan luan,IndexSearcher searcher) {
+		DocFn(Luan luan,IndexSearcher searcher,Query query) {
 			super(luan);
 			this.searcher = searcher;
+			this.query = query;
 		}
 
 		@Override public Object call(Object[] args) throws LuanException {
 			try {
-				return toTable(luan(),searcher.doc(docID));
+				LuanTable doc = toTable(luan(),searcher.doc(docID));
+				if( args.length > 0 && "explain".equals(args[0]) ) {
+					Explanation explanation = searcher.explain(query,docID);
+					return new Object[]{doc,explanation};
+				} else {
+					return doc;
+				}
 			} catch(IOException e) {
 				throw new LuanException(e);
 			}
@@ -421,7 +429,7 @@
 			if( fn!=null && n==null ) {
 				if( sortStr != null )
 					throw new LuanException("sort must be nil when n is nil");
-				final DocFn docFn = new DocFn(luan,searcher);
+				final DocFn docFn = new DocFn(luan,searcher,query);
 				MyCollector col = new MyCollector() {
 					@Override public void collect(int doc) {
 						try {
@@ -447,10 +455,11 @@
 			Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr);
 			TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort);
 			final ScoreDoc[] scoreDocs = td.scoreDocs;
-			DocFn docFn = new DocFn(luan,searcher);
+			DocFn docFn = new DocFn(luan,searcher,query);
 			for( int i=0; i<scoreDocs.length; i++ ) {
-				docFn.docID = scoreDocs[i].doc;
-				fn.call(i+1,docFn);
+				ScoreDoc scoreDoc = scoreDocs[i];
+				docFn.docID = scoreDoc.doc;
+				fn.call(i+1,docFn,scoreDoc.score);
 			}
 			return td.totalHits;
 		} finally {
@@ -614,7 +623,9 @@
 		}
 	};
 
-	public LuanFunction highlighter(String queryStr,LuanFunction formatter,final Integer fragmentSize,String dotdotdot) throws ParseException {
+	public LuanFunction highlighter(String queryStr,final LuanFunction formatter,final Integer fragmentSize,String dotdotdot)
+		throws ParseException
+	{
 		Query query = SaneQueryParser.parseQuery(mfp,queryStr);
 		Formatter fmt = new Formatter() {
 			public String highlightTerm(String originalText,TokenGroup tokenGroup) {
--- a/website/src/manual.html	Tue Feb 19 08:14:40 2019 -0700
+++ b/website/src/manual.html	Wed Feb 20 12:09:51 2019 -0700
@@ -2765,6 +2765,11 @@
 Returns <code>x</code> as a double.
 
 
+<h4 heading><a name="Number.float" href="#Number.double"><code>Number.float (x)</code></a></h4>
+<p>
+Returns <code>x</code> as a float.
+
+
 <h4 heading><a name="Number.integer" href="#Number.integer"><code>Number.integer (x)</code></a></h4>
 <p>
 If the value <code>x</code> is convertible to an integer,