云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 搜索引擎

搜索引擎

  • 62 次阅读
  • 3 次下载
  • 2026/4/29 23:52:28

} } }

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 getSearchResult(String key) throws CorruptIndexException, IOException{

34

List list = new ArrayList(); Hits hits = searchFor(key);

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 urls; Searcher mySearch = new Searcher(); public SearchFrame(){

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 it = urls.iterator();it.hasNext();){ String url = (String)it.next(); LinkLabel lb = new LinkLabel(url,url); ptaContent.add(lb); } } ptaContent.validate(); } } public static void main(String[] args){ JFrame myFrame = new SearchFrame(); } }

36

搜索更多关于: 搜索引擎 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

} } } 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 (IOEx

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com