changeset 58:9cef3d27f2e8

Improved search Now collecting title, content and tags into an indexed (but not stored) 'search' field and searching that field by default. Now search query isn't doubled (before it searched title and content separately), but each card to document conversion might take double the memory because of title, content and tag concencation. Which shouldn't be a big deal, since cards usually don't contain a lot of text.
author Fox
date Fri, 23 Dec 2022 15:01:23 +0100
parents 99958b2e2a5a
children a2696310fa8c
files src/junotu/Card.java src/junotu/Database.java
diffstat 2 files changed, 19 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Card.java	Fri Dec 23 12:47:08 2022 +0100
+++ b/src/junotu/Card.java	Fri Dec 23 15:01:23 2022 +0100
@@ -14,6 +14,7 @@
     public static final String TAG_IDENTIFIER = TAG_CORE_PREFIX+"identifier";
     public static final String TAG_TITLE      = TAG_CORE_PREFIX+"title";
     public static final String TAG_CONTENT    = TAG_CORE_PREFIX+"content";
+    public static final String TAG_SEARCH     = TAG_CORE_PREFIX+"search";
     public static final String TAG_LAST_EDIT  = TAG_CORE_PREFIX+"timestamp_last_edit";
 
     public SortedMap< String, Set<Object> > tags = new TreeMap< String, Set<Object> >();
--- a/src/junotu/Database.java	Fri Dec 23 12:47:08 2022 +0100
+++ b/src/junotu/Database.java	Fri Dec 23 15:01:23 2022 +0100
@@ -98,11 +98,17 @@
     {
 	
 	Document document = new Document();
-	
+
+	String search = "";
 	for( String tag : card.tags.keySet() ) {
 	    Set<Object> values = card.tags.get( tag );
 	    for( Object value : values ) {
 		if( value == null ) {
+		    search += tag+" ";
+		} else {
+		    search += tag+" "+value.toString()+" ";
+		}
+		if( value == null ) {
 		    if( !tag.equals("") ) {
 			document.add( new Field( tag, "", Field.Store.YES, Field.Index.NOT_ANALYZED ) );
 		    }
@@ -119,6 +125,7 @@
 		}
 	    }
 	}
+	document.add( new Field( Card.TAG_SEARCH, search, Field.Store.NO, Field.Index.ANALYZED ) );
 
 	return document;
 	
@@ -234,40 +241,25 @@
     public Card[] searchSimple( String query ) throws Exception
     {
 
-	String[] split = query.split( " " );
-
-	Query queryTitle;
-	Query queryContent;
+	Query parsedQuery;
 	
 	try {
-	    /* TODO: FIXME: Don't use advanced QueryParser for simple search. */
-	    QueryParser queryTitleParser = new QueryParser(
-							   LUCENE_VERSION,
-							   Card.TAG_TITLE,
-							   new StandardAnalyzer(LUCENE_VERSION));
-	    QueryParser queryContentParser = new QueryParser(
-							     LUCENE_VERSION,
-							     Card.TAG_CONTENT,
-							     new StandardAnalyzer(LUCENE_VERSION));
+	    QueryParser queryParser = new QueryParser(
+		    LUCENE_VERSION,
+		    Card.TAG_SEARCH,
+		    new StandardAnalyzer(LUCENE_VERSION)
+	    );
 
-	    queryTitleParser.setAllowLeadingWildcard( true );
-	    queryContentParser.setAllowLeadingWildcard( true );
+	    queryParser.setAllowLeadingWildcard( true );
 	    
-	    queryTitle   = queryTitleParser.parse( query );
-	    queryContent = queryContentParser.parse( query );
-
+	    parsedQuery = queryParser.parse( query );
 	    
 	} catch( ParseException e ) {
-	    System.out.print( "Search query parsing exception, returning zero results.\n" );
+	    System.out.print( "Search query parsing exception, returning zero results: "+e.getMessage()+"\n" );
 	    return new Card[0];
 	}
-	
-	BooleanQuery queryFinal = new BooleanQuery();
 
-	queryFinal.add( queryTitle, BooleanClause.Occur.SHOULD );
-	queryFinal.add( queryContent, BooleanClause.Occur.SHOULD );
-
-	TopDocs hits = luceneSearcher.search( queryFinal, 32 );
+	TopDocs hits = luceneSearcher.search( parsedQuery, 32 );
 	Card[] cards = new Card[hits.scoreDocs.length];
 	
 	for( int i = 0; i < hits.scoreDocs.length; i++ ) {