Hi,
here is the code:
package org.apache.lucene; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Collector; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.util.Date; public class SearchEngine extends JFrame { String filePath = "C:/Users/yOuRs/Desktop/files"; String indexPath = "C:/Users/yOuRs/Desktop/index"; static JLabel jlblNoResult = new JLabel(""); static JLabel[][] jlblResult = new JLabel[20][2]; JButton btnsrc; JButton btnidx; JTextField qry; JLabel label; JLabel result; JRadioButton poor; JRadioButton average; JRadioButton good; static final File INDEX_DIR = new File("index"); public static void main (String []args) { SearchEngine frame = new SearchEngine(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("SEARCH ENGINE"); frame.setSize(500, 400); frame. setVisible(true); } public SearchEngine () { Container pane = getContentPane(); pane.setLayout(new FlowLayout()); JPanel panel1 = new JPanel(); panel1.setLayout(new FlowLayout()); btnsrc = new JButton("Search"); label = new JLabel("Query:"); qry = new JTextField(20); btnidx = new JButton("Index"); panel1.add(label); panel1.add(qry); panel1.add(btnsrc); panel1.add(btnidx); JPanel panel2 = new JPanel(); result = new JLabel("Search Result:"); panel2.add(result); ButtonGroup grp = new ButtonGroup(); poor = new JRadioButton("Poor"); average = new JRadioButton("Average"); good = new JRadioButton("Good"); panel2.add(poor); panel2.add(average); panel2.add(good); grp.add(poor); grp.add(average); grp.add(good); JPanel panel3 = new JPanel(new GridLayout(41,0)); panel3.add(jlblNoResult); for (int i=0;i<20;i++){ for(int k=0;k<2;k++){ jlblResult[i][k] = new JLabel(""); panel3.add(jlblResult[i][k]); } } pane.add(panel1); pane.add(panel2); pane.add(panel3); btnidx.addActionListener(new ActionListener() { private boolean deleteDir1; public void actionPerformed(ActionEvent e) { File INDEX_DIR = new File(indexPath); if (INDEX_DIR.exists()) { try { deleteDir1 = deleteDir1(INDEX_DIR); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } final File docDir = new File(filePath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path"); System.exit(1); } Date start = new Date(); try { IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); System.out.println("Indexing to directory '" +INDEX_DIR+ "'..."); indexDocs(writer, docDir); System.out.println("Optimizing..."); writer.optimize(); writer.close(); Date end = new Date(); System.out.println(end.getTime() - start.getTime() + " total milliseconds"); } catch (IOException event) { System.out.println(" caught a " + event.getClass() + "\n with message: " + event.getMessage()); } } private void indexDocs(IndexWriter writer, File docDir) { // TODO Auto-generated method stub } }); btnsrc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String normsField = null; boolean paging = true; int hitsPerPage = 10; int control = 0; jlblNoResult.setText(""); for (int i=0;i<20;i++){ for(int k=0;k<2;k++) jlblResult[i][k].setText(""); } IndexReader reader = null; try { reader = IndexReader.open(FSDirectory.open(new File(indexPath)), true); } catch (CorruptIndexException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } if (normsField != null) reader = new OneNormsReader(reader, normsField); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); BufferedReader in = null; if (queries != null) { try { in = new BufferedReader(new FileReader(queries)); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { try { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, analyzer); while (control ==0) { JLabel jtfInput = null; String line = jtfInput.getText(); if (line == null || line.length() == -1) break; line = line.trim(); if (line.length() == 0) break; Query query = null; try { query = parser.parse(line); } catch (org.apache.lucene.queryParser.ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { try { searcher.search(query, null, 100); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } Date end = new Date(); System.out.println("Time: "+(end.getTime()-start.getTime())+"ms"); } if (paging) { try { doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { try { doStreamingSearch(searcher, query); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } control = 1; } try { reader.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } public static void indexDocs(IndexWriter writer, File file) throws IOException { // do not try to index files that cannot be read if (file.canRead()) { if (file.isDirectory()) { String[] files = file.list(); // an IO error could occur if (files != null) { for (int i = 0; i < files.length; i++) { indexDocs(writer, new File(file, files[i])); } } } else { System.out.println("adding " + file); try { writer.addDocument(FileDocument.Document(file)); } // at least on windows, some temporary files raise this exception with an "access denied" message // checking if the file can be read doesn't help catch (FileNotFoundException fnfe) { ; } } } } public static boolean deleteDir1(File path) throws IOException { if( path.exists() ) { File[] files = path.listFiles(); for(int i=0; i<files.length; i++) { if(files[i].isDirectory()) { deleteDir1(files[i]); } else { files[i].delete(); } } } return( path.delete() ); } public static void doStreamingSearch(final Searcher searcher, Query query) throws IOException { Collector streamingHitCollector = new Collector() { private Scorer scorer; private int docBase; // simply print docId and score of every matching document @Override public void collect(int doc) throws IOException { System.out.println("doc=" + doc + docBase + " score=" + scorer.score()); } @Override public boolean acceptsDocsOutOfOrder() { return true; } @Override public void setNextReader(IndexReader reader, int docBase) throws IOException { this.docBase = docBase; } @Override public void setScorer(Scorer scorer) throws IOException { this.scorer = scorer; } }; searcher.search(query, streamingHitCollector); } public static void doPagingSearch(BufferedReader in, Searcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { // Collect enough docs to show 5 pages TopScoreDocCollector collector = TopScoreDocCollector.create( 5 * hitsPerPage, false); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; int numTotalHits = collector.getTotalHits(); jlblNoResult.setText(numTotalHits + " total matching documents"); int manipulation = 0; int start = 0; //int end = Math.min(numTotalHits, hitsPerPage); int end = numTotalHits; FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; while (manipulation == 0) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length +" of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'n') { break; } collector = TopScoreDocCollector.create(numTotalHits, false); searcher.search(query, collector); hits = collector.topDocs().scoreDocs; } //end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { if (raw) { // output raw format System.out.println("doc="+hits[i].doc+" score="+hits[i].score); continue; } Highlighter highlighter = new Highlighter(new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(400)); Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { jlblResult[i][0].setText((i+1) + ". " + path); jlblResult[i][1].setText(" Score = " + hits[i].score); String title = doc.get("title"); if (title != null) { jlblResult[i][0].setText(" Title: " + doc.get("title")); jlblResult[i][1].setText(" Score = " + hits[i].score); } } else { jlblResult[i][0].setText((i+1) + ". " + "No path for this document"); //JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green."); } File file = new File(path); try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. System.out.println(dis.readLine()); } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } manipulation = 1; } } protected void deleteDir(File iNDEX_DIR2) { // TODO Auto-generated method stub } }
this is my coding to build a search engine by using eclipse n lucene.. however there is error and when i click the search button it has no output..i want to display the document that matching with my query that i have inserted. can you help me to fix these errors ?? i just want to index and search text files in my local data..how can i make these search engine function like google, but not for web..just for text files in my laptop..help me pleaseee...
Hi Friend,
There is one error which occurs at the following line:
reader = new OneNormsReader(reader, normsField);
Actually we haven't get the OneNormsReader class. So please explain the above line and provide this class.
Thanks
Ads