changeset 67:3f0b4e44c6ef

Hide cards with specific tags from search results For example board columns and board column cards.
author Fox
date Sat, 24 Dec 2022 01:27:22 +0100
parents df652edb3c0e
children c6b1b4def7c1
files src/junotu/Database.java
diffstat 1 files changed, 37 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r df652edb3c0e -r 3f0b4e44c6ef src/junotu/Database.java
--- a/src/junotu/Database.java	Fri Dec 23 22:41:51 2022 +0100
+++ b/src/junotu/Database.java	Sat Dec 24 01:27:22 2022 +0100
@@ -24,11 +24,15 @@
 import org.apache.lucene.search.SortField;
 
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.WildcardQuery;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.NumericRangeQuery;
 import org.apache.lucene.queryParser.QueryParser;
 
+import org.apache.lucene.index.Term;
+
 import org.apache.lucene.search.BooleanClause;
 
 import junotu.Card;
@@ -42,6 +46,8 @@
     private IndexSearcher luceneSearcher;
 
     private long highestIdentifier;
+
+    private Query[] hideQueries;
     
     public Database()
     {
@@ -72,6 +78,19 @@
 	} catch( IOException e ) { /* Also catches CorruptIndexException from Lucene */
 	    throw new RuntimeException(e);
 	}
+
+	hideQueries = new Query[Card.HIDE_TAGS.length+Card.HIDE_TAG_VALUES.length/2];
+	int pos = 0;
+	
+	for( int i = 0; i < Card.HIDE_TAGS.length; i++ ) {
+	    hideQueries[pos] = new WildcardQuery( new Term(Card.HIDE_TAGS[i], "*") );
+	    pos += 1;
+	}
+	
+	for( int i = 0; i < Card.HIDE_TAG_VALUES.length/2; i++ ) {
+	    hideQueries[pos] = new TermQuery( new Term(Card.HIDE_TAG_VALUES[i*2], Card.HIDE_TAG_VALUES[i*2+1]) );
+	    pos += 1;
+	}
 	
     }
 
@@ -316,9 +335,16 @@
     /** Return up to 'amount' of recently modified cards. */
     public Card[] searchTopRecent( int amount )
     {
+	BooleanQuery finalQuery = new BooleanQuery();
+
+	finalQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD );
+	for( int i = 0; i < hideQueries.length; i++ ) {
+	    finalQuery.add( hideQueries[i], BooleanClause.Occur.MUST_NOT );
+	}
+	
 	try {
 	    TopDocs topDocuments = luceneSearcher.search(
-						 new MatchAllDocsQuery(),
+						 finalQuery,
 						 null,
 						 amount,
 						 new Sort( new SortField( Card.TAG_LAST_EDIT, SortField.LONG, true ) )
@@ -342,6 +368,7 @@
     {
 
 	Query parsedQuery;
+	BooleanQuery finalQuery;
 	
 	try {
 	    QueryParser queryParser = new QueryParser(
@@ -358,10 +385,17 @@
 	    System.out.print( "Search query parsing exception, returning zero results: "+e.getMessage()+"\n" );
 	    return new Card[0];
 	}
+	
+	finalQuery = new BooleanQuery();
 
+	finalQuery.add( parsedQuery, BooleanClause.Occur.SHOULD );
+	for( int i = 0; i < hideQueries.length; i++ ) {
+	    finalQuery.add( hideQueries[i], BooleanClause.Occur.MUST_NOT );
+	}
+	
 	try {
 	    
-	    TopDocs hits = luceneSearcher.search( parsedQuery, 32 );
+	    TopDocs hits = luceneSearcher.search( finalQuery, 32 );
 	    Card[] cards = new Card[hits.scoreDocs.length];
 	    
 	    for( int i = 0; i < hits.scoreDocs.length; i++ ) {
@@ -376,5 +410,5 @@
 	}
 	
     }
-    
+
 }