changeset 6:fbe307da953a

Started on database
author Fox
date Tue, 05 Apr 2022 02:20:25 +0200
parents 81608928a9db
children 4ee26d904fe3
files src/junotu/Database.java src/junotu/Main.java
diffstat 2 files changed, 98 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Database.java	Tue Apr 05 00:45:42 2022 +0200
+++ b/src/junotu/Database.java	Tue Apr 05 02:20:25 2022 +0200
@@ -0,0 +1,94 @@
+package junotu;
+
+import java.io.File;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.util.Version;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.ScoreDoc;
+
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.queryParser.QueryParser;
+
+import org.apache.lucene.search.BooleanClause;
+
+public class Database {
+
+    public class Card {
+	public String title;
+	public String content;
+    }
+    
+    public static final String DATABASE_DIRECTORY = "./Database";
+    public static final Version LUCENE_VERSION = Version.LUCENE_30;
+
+    public static final String TAG_TITLE = "_junotu_title";
+    public static final String TAG_CONTENT = "_junotu_content";
+    
+    private IndexWriter luceneWriter;
+    private IndexSearcher luceneSearcher;
+    
+    public Database() throws Exception
+    {
+	Directory indexDirectory = FSDirectory.open( new File( DATABASE_DIRECTORY ) );
+	luceneWriter = new IndexWriter(
+				       indexDirectory,
+				       new StandardAnalyzer(LUCENE_VERSION),
+				       true,
+				       IndexWriter.MaxFieldLength.UNLIMITED
+				       );
+	luceneSearcher = new IndexSearcher( indexDirectory );
+    }
+    
+    public void cardAdd( String title, String content ) throws Exception
+    {
+	Document document = new Document();
+
+	document.add( new Field( TAG_TITLE, title, Field.Store.YES, Field.Index.ANALYZED ) );
+	document.add( new Field( TAG_CONTENT, content, Field.Store.YES, Field.Index.ANALYZED ) );
+
+	luceneWriter.addDocument( document );
+	
+    }
+
+    public Card[] searchSimple( String query ) throws Exception
+    {
+
+	String[] split = query.split( " " );
+	
+        Query queryTitle = new QueryParser(
+					   LUCENE_VERSION,
+					   TAG_TITLE,
+					   new StandardAnalyzer(LUCENE_VERSION)).parse(query);
+	Query queryContent = new QueryParser(
+					     LUCENE_VERSION,
+					     TAG_CONTENT,
+					     new StandardAnalyzer(LUCENE_VERSION)).parse(query);
+
+	BooleanQuery queryFinal = new BooleanQuery();
+
+	queryFinal.add( queryTitle, BooleanClause.Occur.SHOULD );
+	queryFinal.add( queryContent, BooleanClause.Occur.SHOULD );
+
+	TopDocs hits = luceneSearcher.search( queryFinal, 32 );
+	Card[] cards = new Card[hits.scoreDocs.length];
+	
+	for( int i = 0; i < hits.scoreDocs.length; i++ ) {
+	    Document document = luceneSearcher.doc( hits.scoreDocs[i].doc );
+	    cards[i] = new Card();
+	    cards[i].title = document.get( TAG_TITLE );
+	    cards[i].content = document.get( TAG_CONTENT );
+	}
+
+	return cards;
+	
+    }
+    
+}
--- a/src/junotu/Main.java	Tue Apr 05 00:45:42 2022 +0200
+++ b/src/junotu/Main.java	Tue Apr 05 02:20:25 2022 +0200
@@ -1,5 +1,6 @@
 package junotu;
 
+import junotu.Database;
 import junotu.Window;
 import junotu.Window.Tab;
 
@@ -8,10 +9,12 @@
     public static final String PROGRAM_NAME = "Junotu";
     public static final int MAX_WINDOWS = 8;
 
+    public static Database database;
     public static Window[] windows = new Window[MAX_WINDOWS];
     
-    public static void main(String[] args)
+    public static void main(String[] args) throws Exception
     {
+	database = new Database();
         addWindow( Tab.SEARCH );
     }