changeset 599:50540f0813e2

support default search fields in lucene; add search to blog;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 16 Sep 2015 20:55:49 -0600
parents e930f92d0f61
children b926e53910dd
files blog/src/index.html.luan blog/src/lib/Db_mod.luan blog/src/lib/Post.luan blog/src/site.css lucene/ext/sane-lucene-queryparser.jar lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneIndex.java
diffstat 7 files changed, 73 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
diff -r e930f92d0f61 -r 50540f0813e2 blog/src/index.html.luan
--- a/blog/src/index.html.luan	Wed Sep 16 14:32:52 2015 -0600
+++ b/blog/src/index.html.luan	Wed Sep 16 20:55:49 2015 -0600
@@ -8,6 +8,8 @@
 
 
 return function()
+	local query = Http.request.parameter.query
+
 	Io.stdout = Http.response.text_writer()
 %>
 <html>
@@ -17,12 +19,18 @@
 		</style>
 	</head>
 	<body>
-		<h1>Demo Blog App</h1>
+		<h1><a href="/">Demo Blog App</a></h1>
+
+		<form>
+			<input name=query type=text value="<%= query or "" %>">
+			<input type=submit value=Search>
+		</form>
 
 		<div><a href="new">Make New Post</a></div>
 
 		<%
-		for _, post in ipairs(Post.get_all()) do
+		local posts = query and Post.search(query) or Post.get_all()
+		for _, post in ipairs(posts) do
 			%>
 			<a name="p<%= post.id %>">
 			<h2><%= post.subject %></h2>
diff -r e930f92d0f61 -r 50540f0813e2 blog/src/lib/Db_mod.luan
--- a/blog/src/lib/Db_mod.luan	Wed Sep 16 14:32:52 2015 -0600
+++ b/blog/src/lib/Db_mod.luan	Wed Sep 16 20:55:49 2015 -0600
@@ -8,7 +8,7 @@
 
 function M.new_db()
 	local dir = Io.uri(M.lucene_dir).to_string()
-	local db = Lucene.index(dir)
+	local db = Lucene.index( dir, Lucene.type.english, {"subject","content"} )
 	
 --	this is how you index a field
 --	db.indexed_fields.post_date = Lucene.type.long
diff -r e930f92d0f61 -r 50540f0813e2 blog/src/lib/Post.luan
--- a/blog/src/lib/Post.luan	Wed Sep 16 14:32:52 2015 -0600
+++ b/blog/src/lib/Post.luan	Wed Sep 16 20:55:49 2015 -0600
@@ -4,6 +4,8 @@
 local assert_string = Luan.assert_string or error()
 local Time = require "luan:Time"
 local now = Time.now or error()
+local String = require "luan:String"
+local trim = String.trim or error()
 local Db = require "site:/lib/Db"
 
 
@@ -52,4 +54,17 @@
 	return posts
 end
 
+function M.search(query)
+	query = trim(query)
+	if #query == 0 then
+		return M.get_all()
+	end
+	local docs = Db.search(query,1,1000)
+	local posts = {}
+	for _, doc in ipairs(docs) do
+		posts[#posts+1] = from_doc(doc)
+	end
+	return posts
+end
+
 return M
diff -r e930f92d0f61 -r 50540f0813e2 blog/src/site.css
--- a/blog/src/site.css	Wed Sep 16 14:32:52 2015 -0600
+++ b/blog/src/site.css	Wed Sep 16 20:55:49 2015 -0600
@@ -15,3 +15,25 @@
 a[href]:hover {
 	text-decoration: underline;
 }
+
+
+input[type="text"], input[type="email"] {
+	font: inherit;
+	padding: .5em .8em;
+	border-radius: 4px;
+	border-style: groove;
+}
+input[type="text"]:focus, input[type="email"]:focus {
+	border-color: #66afe9;
+	outline: none;
+}
+input[type="submit"] {
+	font: inherit;
+	padding: .5em;
+	border-radius: 4px;
+	background: #eee;
+	border-color: #eee;
+}
+input[type="submit"]:hover {
+	background: #ddd !important;
+}
diff -r e930f92d0f61 -r 50540f0813e2 lucene/ext/sane-lucene-queryparser.jar
Binary file lucene/ext/sane-lucene-queryparser.jar has changed
diff -r e930f92d0f61 -r 50540f0813e2 lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Wed Sep 16 14:32:52 2015 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Wed Sep 16 20:55:49 2015 -0600
@@ -3,7 +3,10 @@
 local error = Luan.error
 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex"
 local NumberFieldParser = require "java:sane.lucene.queryparser.NumberFieldParser"
+local StringFieldParser = require "java:sane.lucene.queryparser.StringFieldParser"
 local SaneQueryParser = require "java:sane.lucene.queryparser.SaneQueryParser"
+local Version = require "java:org.apache.lucene.util.Version"
+local EnglishAnalyzer = require "java:org.apache.lucene.analysis.en.EnglishAnalyzer"
 
 
 local M = {}
@@ -13,14 +16,16 @@
 	integer = NumberFieldParser.INT;
 	long = NumberFieldParser.LONG;
 	double = NumberFieldParser.DOUBLE;
+
+	english = StringFieldParser.new(EnglishAnalyzer.new(Version.LUCENE_CURRENT))
 }
 
 M.literal = SaneQueryParser.literal
 
-function M.index(index_dir)
+function M.index(index_dir,default_type,default_fields)
 	local index = {}
 	index.dir = index_dir
-	local java_index = LuceneIndex.new(index_dir)
+	local java_index = LuceneIndex.new(index_dir,default_type,default_fields)
 	index.indexed_fields = java_index.indexedFieldsMeta.newTable()
 	index.to_string = java_index.to_string
 	index.backup = java_index.backup
diff -r e930f92d0f61 -r 50540f0813e2 lucene/src/luan/modules/lucene/LuceneIndex.java
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 16 14:32:52 2015 -0600
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 16 20:55:49 2015 -0600
@@ -20,6 +20,7 @@
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.StoredField;
 import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.TextField;
 import org.apache.lucene.document.IntField;
 import org.apache.lucene.document.LongField;
 import org.apache.lucene.document.DoubleField;
@@ -69,8 +70,7 @@
 	private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class);
 
 	private static final String FLD_NEXT_ID = "nextId";
-	private static final Analyzer analyzer = new KeywordAnalyzer();
-	public static final FieldParser STRING_FIELD_PARSER = new StringFieldParser(analyzer);
+	public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer());
 
 	private final ReentrantLock writeLock = new ReentrantLock();
 	private final File indexDir;
@@ -80,15 +80,21 @@
 	private IndexSearcher searcher;
 	private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>();
 	private boolean isClosed = false;
-	private final MultiFieldParser mfp = new MultiFieldParser();
+	private final MultiFieldParser mfp;
 
-	public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException {
+	public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException {
+		mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields);
 		mfp.fields.put( "type", STRING_FIELD_PARSER );
 		mfp.fields.put( "id", NumberFieldParser.LONG );
 		File indexDir = new File(indexDirStr);
 		this.indexDir = indexDir;
 		Directory dir = FSDirectory.open(indexDir);
-		Version version = Version.LUCENE_4_9;
+		Version version = Version.LUCENE_4_9;		
+		Analyzer analyzer = STRING_FIELD_PARSER.analyzer;
+		if( defaultFieldParser instanceof StringFieldParser ) {
+			StringFieldParser sfp = (StringFieldParser)defaultFieldParser;
+			analyzer = sfp.analyzer;
+		}
 		IndexWriterConfig conf = new IndexWriterConfig(version,analyzer);
 		snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
 		conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
@@ -415,8 +421,13 @@
 			Object value = entry.getValue();
 			if( value instanceof String ) {
 				String s = (String)value;
-				if( indexed.contains(name) ) {
-					doc.add(new StringField(name, s, Field.Store.YES));
+				FieldParser fp = mfp.fields.get(name);
+				if( fp != null ) {
+					if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) {
+						doc.add(new TextField(name, s, Field.Store.YES));
+					} else {
+						doc.add(new StringField(name, s, Field.Store.YES));
+					}
 				} else {
 					doc.add(new StoredField(name, s));
 				}