changeset 312:d34be4588556

add lucene query parsing git-svn-id: https://luan-java.googlecode.com/svn/trunk@313 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 24 Dec 2014 11:15:27 +0000
parents 22ca4f3a65c3
children 1f6d828986b9
files core/src/luan/modules/PackageLuan.java lucene/ext/lucene-queryparser-4.9.0.jar lucene/src/luan/modules/lucene/LuanQueryNodeProcessor.java lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneIndex.java lucene/src/luan/modules/lucene/LuceneSearcher.java
diffstat 6 files changed, 77 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
diff -r 22ca4f3a65c3 -r d34be4588556 core/src/luan/modules/PackageLuan.java
--- a/core/src/luan/modules/PackageLuan.java	Tue Dec 23 07:39:30 2014 +0000
+++ b/core/src/luan/modules/PackageLuan.java	Wed Dec 24 11:15:27 2014 +0000
@@ -46,6 +46,8 @@
 		if( mod == null ) {
 			if( modName.startsWith("java:") ) {
 				mod = JavaLuan.load(luan,modName.substring(5));
+				if( mod == null )
+					return null;
 			} else {
 				String src = read(luan,modName+".luan");
 				if( src == null )
diff -r 22ca4f3a65c3 -r d34be4588556 lucene/ext/lucene-queryparser-4.9.0.jar
Binary file lucene/ext/lucene-queryparser-4.9.0.jar has changed
diff -r 22ca4f3a65c3 -r d34be4588556 lucene/src/luan/modules/lucene/LuanQueryNodeProcessor.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lucene/src/luan/modules/lucene/LuanQueryNodeProcessor.java	Wed Dec 24 11:15:27 2014 +0000
@@ -0,0 +1,52 @@
+package luan.modules.lucene;
+
+import java.util.List;
+import org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessor;
+import org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler;
+import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode;
+import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode;
+import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
+
+
+public class LuanQueryNodeProcessor implements QueryNodeProcessor {
+	private final LuceneIndex index;
+	private final QueryNodeProcessor qnp;
+
+	public LuanQueryNodeProcessor(LuceneIndex index,QueryNodeProcessor qnp) {
+		this.index = index;
+		this.qnp = qnp;
+	}
+
+	public QueryConfigHandler getQueryConfigHandler() {
+		return qnp.getQueryConfigHandler();
+	}
+
+	public void setQueryConfigHandler(QueryConfigHandler queryConfigHandler) {
+		qnp.setQueryConfigHandler(queryConfigHandler);
+	}
+
+	public QueryNode process(QueryNode queryTree) throws QueryNodeException {
+		fix(queryTree);
+//		System.out.println(queryTree);
+		return qnp.process(queryTree);
+	}
+
+	private void fix(QueryNode queryTree) {
+		if( queryTree instanceof FieldQueryNode ) {
+			FieldQueryNode fqn = (FieldQueryNode)queryTree;
+			CharSequence fldSeq = fqn.getField();
+			if( fldSeq == null )
+				throw new RuntimeException("missing field for value: "+fqn.getText());
+			String fld = fldSeq.toString();
+			fld = index.map_field_name(fld);
+//			System.out.println("field = "+fld);
+			fqn.setField(fld);
+		}
+		List<QueryNode> list = queryTree.getChildren();
+		if( list != null ) {
+			for( QueryNode qn : list ) {
+				fix(qn);
+			}
+		}
+	}
+}
diff -r 22ca4f3a65c3 -r d34be4588556 lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Tue Dec 23 07:39:30 2014 +0000
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Wed Dec 24 11:15:27 2014 +0000
@@ -61,6 +61,8 @@
 	local Query = {}
 	index.Query = Query
 
+	Query.parse = java_index.parse
+
 	Query.all_docs = MatchAllDocsQuery.new()
 	
 	function Query.term(t)
diff -r 22ca4f3a65c3 -r d34be4588556 lucene/src/luan/modules/lucene/LuceneIndex.java
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Tue Dec 23 07:39:30 2014 +0000
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Dec 24 11:15:27 2014 +0000
@@ -19,8 +19,11 @@
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.util.Version;
+import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;
+import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
 import luan.modules.Utils;
 import luan.Luan;
 import luan.LuanState;
@@ -188,4 +191,11 @@
 		reader.close();
 	}
 
+
+	public Query parse(String s) throws QueryNodeException {
+		StandardQueryParser qp = new StandardQueryParser();
+		qp.setQueryNodeProcessor(new LuanQueryNodeProcessor(this,qp.getQueryNodeProcessor()));
+		return qp.parse(s,null);
+	}
+
 }
diff -r 22ca4f3a65c3 -r d34be4588556 lucene/src/luan/modules/lucene/LuceneSearcher.java
--- a/lucene/src/luan/modules/lucene/LuceneSearcher.java	Tue Dec 23 07:39:30 2014 +0000
+++ b/lucene/src/luan/modules/lucene/LuceneSearcher.java	Wed Dec 24 11:15:27 2014 +0000
@@ -21,6 +21,7 @@
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -77,7 +78,15 @@
 		}
 	}
 
-	@LuanMethod public Object[] search( final LuanState luan, Query query, Object nObj, Sort sort ) throws LuanException, IOException {
+	@LuanMethod public Object[] search( final LuanState luan, Object queryObj, Object nObj, Sort sort ) throws LuanException, IOException, QueryNodeException {
+		Query query;
+		if( queryObj instanceof Query ) {
+			query = (Query)queryObj;
+		} else if( queryObj instanceof String ) {
+			String s = (String)queryObj;
+			query = index.parse(s);
+		} else
+			throw luan.exception("bad argument #1 (string or Query expected, got "+Luan.type(queryObj)+")");
 		if( nObj instanceof LuanFunction ) {
 			final LuanFunction fn = (LuanFunction)nObj;
 			Collector col = new MyCollector() {
@@ -134,7 +143,7 @@
 	LuanTable table() {
 		LuanTable tbl = Luan.newTable();
 		try {
-			add( tbl, "search", LuanState.class, Query.class, Object.class, Sort.class );
+			add( tbl, "search", LuanState.class, Object.class, Object.class, Sort.class );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}