From 3b21bd113e3461522ed97f42f7c7f09f242c0d7e Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 11 Jan 2021 22:31:33 +0100 Subject: [PATCH] Add GUI interface for searcher --- src/main/java/org/RI/P2/Searcher.java | 135 ++++++++++++++++++++-- src/main/java/org/RI/P2/SearcherForm.form | 107 +++++++++++++++++ 2 files changed, 231 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/RI/P2/SearcherForm.form diff --git a/src/main/java/org/RI/P2/Searcher.java b/src/main/java/org/RI/P2/Searcher.java index 2c70b43..ca5844d 100644 --- a/src/main/java/org/RI/P2/Searcher.java +++ b/src/main/java/org/RI/P2/Searcher.java @@ -4,48 +4,127 @@ import java.io.IOException; import java.nio.file.Paths; import java.text.ParseException; -import org.apache.lucene.analysis.core.WhitespaceAnalyzer; +import javax.swing.table.DefaultTableModel; + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.queryparser.classic.QueryParser; -public class Searcher { - - IndexReader index; +public class Searcher extends javax.swing.JFrame { + IndexSearcher searcher; String dataPath; String indexPath; - Searcher(String dataPath, String indexPath) { + Searcher(String dataPath, String indexPath) throws IOException { this.dataPath = dataPath; this.indexPath = indexPath; + searcher = createIndexSearcher(); + initComponents(); + } + + private void initComponents() { + jTextField1 = new javax.swing.JTextField(); + jButton1 = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jTable1 = new javax.swing.JTable(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jTextField1.setToolTipText(""); + + jButton1.setText("Buscar"); + jButton1.setToolTipText(""); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + try { + jButton1ActionPerformed(evt); + } catch (IOException exp) { + System.err.println(exp); + } catch (org.apache.lucene.queryparser.classic.ParseException exp) { + System.err.println(exp); + } + } + }); + + jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { + + }, new String[] { "Titulo", "Autores" })); + jScrollPane1.setViewportView(jTable1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup().addContainerGap(19, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addGroup(layout.createSequentialGroup().addComponent(jTextField1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jButton1)) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, + javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap())); + layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup().addGap(15, 15, 15) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, + javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); + + pack(); + } + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) + throws IOException, org.apache.lucene.queryparser.classic.ParseException { + searchFiles(jTextField1.getText()); } IndexSearcher createIndexSearcher() throws IOException { Directory indexDirectory = FSDirectory.open(Paths.get(indexPath)); IndexReader indexReader = DirectoryReader.open(indexDirectory); - IndexSearcher searcher = new IndexSearcher(indexReader); + searcher = new IndexSearcher(indexReader); return searcher; } - TopDocs searchFiles(String queryString, int resultNumber) + TopDocs queryIndex(String queryString, int resultNumber) throws IOException, org.apache.lucene.queryparser.classic.ParseException { - IndexSearcher searcher = createIndexSearcher(); - Query query = new QueryParser("abstract", new WhitespaceAnalyzer()).parse(queryString); + Query query = new QueryParser("abstract", new StandardAnalyzer()).parse(queryString); TopDocs topDocs = searcher.search(query, resultNumber); return topDocs; } + void showResults(TopDocs docs) throws IOException { + System.out.println(docs.totalHits); + DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); + for (ScoreDoc scoreDoc : docs.scoreDocs) { + Document doc = searcher.doc(scoreDoc.doc); + model.addRow(new Object[] { doc.get("title"), doc.get("authors") }); + } + } + + void searchFiles(String query) throws IOException, org.apache.lucene.queryparser.classic.ParseException { + int resultNumber = 20; + TopDocs results = queryIndex(query, resultNumber); + showResults(results); + } + private static void usage() { System.out.println("Usage: Searcher "); System.exit(1); } - public static void main(String[] args) throws IOException, ParseException { + public static void main(String[] args) + throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException { if (args.length != 1) { usage(); } @@ -53,6 +132,40 @@ public class Searcher { String indexDirectory = ".index"; Indexer indexer = new Indexer(dataDirectory, indexDirectory); indexer.populateIndex(); - Searcher searcher = new Searcher(dataDirectory, indexDirectory); + + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(Searcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(Searcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(Searcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(Searcher.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + try { + new Searcher(args[0], indexDirectory).setVisible(true); + } catch (IOException exp) { + System.err.println(exp); + } + } + }); } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton jButton1; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTable jTable1; + private javax.swing.JTextField jTextField1; + // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/org/RI/P2/SearcherForm.form b/src/main/java/org/RI/P2/SearcherForm.form new file mode 100644 index 0000000..69df184 --- /dev/null +++ b/src/main/java/org/RI/P2/SearcherForm.form @@ -0,0 +1,107 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + </Component> + </SubComponents> + </Container> + </SubComponents> +</Form>