Mercurial Hosting > junotu
changeset 117:652027516bd6
Database, TabSimpleSearch: Now storing and displaying total results found
author | Fox |
---|---|
date | Sun, 15 Oct 2023 09:26:24 +0200 |
parents | 85d38a6a2349 |
children | 290101988622 |
files | src/junotu/Database.java src/junotu/TabCalendarBoard.java src/junotu/TabSimpleSearch.java |
diffstat | 3 files changed, 51 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/Database.java Tue Aug 29 21:53:05 2023 +0200 +++ b/src/junotu/Database.java Sun Oct 15 09:26:24 2023 +0200 @@ -39,6 +39,11 @@ public class Database { + public class SearchResult { + public Card cards[]; + public int total = 0; + } + public static final String DATABASE_DIRECTORY = "./database"; public static final Version LUCENE_VERSION = Version.LUCENE_30; @@ -412,7 +417,7 @@ } /** Return up to 'amount' of recently modified cards. */ - public Card[] searchTopRecent( int amount ) + public SearchResult searchTopRecent( int amount ) { BooleanQuery finalQuery = new BooleanQuery(); @@ -422,6 +427,7 @@ } try { + SearchResult result = new SearchResult(); TopDocs topDocuments = luceneSearcher.search( finalQuery, null, @@ -432,22 +438,24 @@ ) ); - Card[] cards = new Card[topDocuments.scoreDocs.length]; + result.cards = new Card[topDocuments.scoreDocs.length]; + result.total = topDocuments.totalHits; for( int i = 0; i < topDocuments.scoreDocs.length; i++ ) { Document document = luceneSearcher.doc( topDocuments.scoreDocs[i].doc ); - cards[i] = cardFromDocument( document ); + result.cards[i] = cardFromDocument( document ); } - return cards; + return result; } catch( IOException e ) { throw new RuntimeException(e); } } - public Card[] searchSimple( String query ) + public SearchResult searchSimple( String query ) { + SearchResult result = new SearchResult(); Query parsedQuery; BooleanQuery finalQuery; @@ -465,7 +473,8 @@ } catch( ParseException e ) { System.out.print( "Search query parsing exception, returning zero results: "+e.getMessage()+"\n" ); - return new Card[0]; + result.cards = new Card[0]; + return result; } finalQuery = new BooleanQuery(); @@ -476,7 +485,6 @@ } try { - TopDocs hits = luceneSearcher.search( finalQuery, null, @@ -487,14 +495,16 @@ new SortField( Card.TAG_SAVED, SortField.LONG, true ) ) ); - Card[] cards = new Card[hits.scoreDocs.length]; + + result.cards = new Card[hits.scoreDocs.length]; + result.total = hits.totalHits; for( int i = 0; i < hits.scoreDocs.length; i++ ) { Document document = luceneSearcher.doc( hits.scoreDocs[i].doc ); - cards[i] = cardFromDocument( document ); + result.cards[i] = cardFromDocument( document ); } - return cards; + return result; } catch( IOException e ) { throw new RuntimeException(e); @@ -502,7 +512,7 @@ } - public Card[] searchCustom( Query query, int limit, boolean useIgnores ) + public SearchResult searchCustom( Query query, int limit, boolean useIgnores ) { if( useIgnores ) { @@ -518,16 +528,18 @@ } try { - + SearchResult result = new SearchResult(); TopDocs hits = luceneSearcher.search( query, limit ); - Card[] cards = new Card[hits.scoreDocs.length]; + + result.cards = new Card[hits.scoreDocs.length]; + result.total = hits.totalHits; for( int i = 0; i < hits.scoreDocs.length; i++ ) { Document document = luceneSearcher.doc( hits.scoreDocs[i].doc ); - cards[i] = cardFromDocument( document ); + result.cards[i] = cardFromDocument( document ); } - return cards; + return result; } catch( IOException e ) { throw new RuntimeException(e);
--- a/src/junotu/TabCalendarBoard.java Tue Aug 29 21:53:05 2023 +0200 +++ b/src/junotu/TabCalendarBoard.java Sun Oct 15 09:26:24 2023 +0200 @@ -516,7 +516,7 @@ Card card; Query query = new WildcardQuery( new Term(Card.TAG_CALENDAR_BOARD, "*") ); - Card cards[] = Main.database.searchCustom( query, 1, false ); + Card cards[] = Main.database.searchCustom( query, 1, false ).cards; if( cards.length == 0 ) { card = new Card(); @@ -625,7 +625,7 @@ Card card = null; term = term.createTerm( DATE_FORMAT.format(curTime) ); - Card cards[] = Main.database.searchCustom( new TermQuery(term), 1, false ); + Card cards[] = Main.database.searchCustom( new TermQuery(term), 1, false ).cards; if( cards.length != 0 ) { card = cards[0];
--- a/src/junotu/TabSimpleSearch.java Tue Aug 29 21:53:05 2023 +0200 +++ b/src/junotu/TabSimpleSearch.java Sun Oct 15 09:26:24 2023 +0200 @@ -28,6 +28,7 @@ import javax.swing.JMenuItem; import junotu.Main; +import junotu.Database.SearchResult; import junotu.Window.Tab; import junotu.Window.TabInterface; import junotu.Card; @@ -39,6 +40,8 @@ private final String KEY_ACTION_COMMIT = "commit"; + private int lastTotalFound = -1; + private JTextField field; private JButton create; private JButton context; @@ -99,20 +102,23 @@ @Override public void changedUpdate( DocumentEvent e ) { - updateTitle(); + updateTitle(); /* 'Searching' title. */ search(); + updateTitle(); /* 'Found' title. */ } @Override public void removeUpdate( DocumentEvent e ) { updateTitle(); search(); + updateTitle(); } @Override public void insertUpdate( DocumentEvent e ) { updateTitle(); search(); + updateTitle(); } } ); @@ -138,26 +144,30 @@ public void run() { search(); + throw new RuntimeException(); } } ); return; } - Card[] cards; + SearchResult result; String text = field.getText(); if( text.length() > 0 ) { - cards = Main.database.searchSimple( field.getText() ); + result = Main.database.searchSimple( field.getText() ); + } else { - cards = Main.database.searchTopRecent( 32 ); + result = Main.database.searchTopRecent( 32 ); } - System.out.print("Search: Found "+cards.length+" matches.\n"); + System.out.print("Search: Displaying "+result.cards.length+"/"+result.total+" matches.\n"); + + lastTotalFound = result.total; /* TODO: Reuse widgets. */ results.removeAll(); - for( Card card : cards ) { + for( Card card : result.cards ) { CardWidget cardWidget = new CardWidget( card ); results.add( cardWidget ); } @@ -195,7 +205,12 @@ String text = field.getText(); if( text.length() > 0 ) { - window.setTitle( window.preferredTitle( "Search: "+text ) ); + if( lastTotalFound != -1 ) { + window.setTitle( window.preferredTitle( "Found "+lastTotalFound+": "+text ) ); + } else { + /* In the middle of search. */ + window.setTitle( window.preferredTitle( "Searching: "+text ) ); + } } else { window.setTitle( window.preferredTitle( "Search" ) ); }