当前位置:首页 > 搜索引擎
} } }
private void close(){ try { writer.close(); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String[] args){ CreateIndex ci = new CreateIndex(); try { ci.writeToIndex(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ ci.close(); } }
}
9.3 实现搜索代码:
package index;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
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.Term;
import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
33
public class Searcher { private Query query = null; private IndexSearcher searcher = null; public Searcher(){ try { searcher = new IndexSearcher(IndexReader.open(\ } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Searcher(String indexPath){ try { searcher = new IndexSearcher(IndexReader.open(indexPath)); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Hits searchFor(String key){ Hits hits = null; query = new TermQuery(new Term(\ try { hits = searcher.search(query); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return hits; } public void showHits(Hits hits) throws CorruptIndexException, IOException{ for(int i=0;i
34
List
for(int i=0;i return list; } } 9.4 GUI界面代码 package GUI; import index.Searcher; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SearchFrame extends JFrame{ String key; String path; JTextField tfTxt = new JTextField(); JButton search = new JButton(\ 搜索一下,你就知道\ JTextArea taContent = new JTextArea(); JPanel ptaContent = new JPanel(); List 35 this.setSize(500, 500); this.setTitle(\搜索引擎\ this.setLayout(null); this.setResizable(false); tfTxt.setBounds(60,20,200,30); this.add(tfTxt); search.setBounds(280,20,100,30); search.setSize(160,30); this.add(search); ptaContent.setBounds(50,80,350,350); this.add(ptaContent); search.addActionListener(new SearchListener()); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } class SearchListener implements ActionListener{ public void actionPerformed(ActionEvent ae) { ptaContent.removeAll(); ptaContent.repaint(); ptaContent.add(new JLabel(\与此相关的链接:\ try { urls = mySearch.getSearchResult(tfTxt.getText()); } catch (IOException e) { e.printStackTrace(); } if(urls.size()!=0){ for(Iterator 36
共分享92篇相关文档