lucene3.5程序

来源:互联网 发布:尔雅网络课程注册网址 编辑:程序博客网 时间:2024/06/05 14:40
用lucene3.5写了一个带界面的搜索器(索引另外建立),分析了一下这个程序的一些弊端,其一是不能自己建立索引,其二是将search写成内置类,
大大的限制了程序的可拓展性,因此下一步打算写一个拓展性强的程序。
lucene3.5写一个拖展性强的程序有利于程序的升级。
 
 
 
 
import org.apache.lucene.document.*;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.*;import org.apache.lucene.index.*;import java.io.File;import java.io.IOException;import jim.Figurer.Figurer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Index;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.LockObtainFailedException;import org.apache.lucene.util.Version;import org.wltea.analyzer.lucene.IKAnalyzer;import tool.FileList;import tool.FileText;import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;import javax.swing.*;public class Searcher {private JTextField jtfa;private JButton jba;private JTextField jtfb;private JButton jbb;private JButton jbc;private static JTextArea jta;private JTextField jtfc;private JButton jbd;private JButton jbe;private void creatAndShoeGUI(){JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame=new JFrame("搜索器");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JFileChooser fc = new JFileChooser();fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);Container con = frame.getContentPane();con.setLayout(new BorderLayout());JPanel jpup = new JPanel();jpup.setLayout(new GridLayout(2,2));jtfa=new JTextField(30);jba=new JButton("选择索引的存放路径");jba.addActionListener(new ActionListener()                      {                    public void actionPerformed(ActionEvent e)                    {                    int r=fc.showOpenDialog(null);                    if(r==JFileChooser.APPROVE_OPTION)                    {                    jtfa.setText(fc.getSelectedFile().getPath());                    }                    }                      });jtfb=new JTextField(30);jbb=new JButton("搜索");jbb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{String indexPath=jtfa.getText();String phrase = jtfb.getText();new Search();Search.search(indexPath,10);}catch(Exception ex){JOptionPane.showMessageDialog(null,"搜索失败!","提示",JOptionPane.ERROR_MESSAGE);}}});jpup.add(jtfa);jpup.add(jba);jpup.add(jtfb);jpup.add(jbb);jta=new JTextArea(10,30);JScrollPane jsp=new JScrollPane(jta);JPanel jpdown = new JPanel();jpdown.setLayout(new FlowLayout());jtfc=new JTextField(35);jbd=new JButton("设定导出路径");fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);jbd.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){int r = fc.showOpenDialog(null);if(r==JFileChooser.APPROVE_OPTION){jtfc.setText(fc.getSelectedFile().getPath());}}});jbe=new JButton("导出搜索结果");jbe.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{File f =new File(jtfc.getText());FileWriter fw = new FileWriter(f);PrintWriter pw = new PrintWriter(fw);pw.write(jta.getText());pw.flush();pw.close();JOptionPane.showMessageDialog(null, "写入文件成功","提示",JOptionPane.INFORMATION_MESSAGE);}catch(IOException ioe){JOptionPane.showMessageDialog(null, "写入文件成功","提示",JOptionPane.INFORMATION_MESSAGE);}}});            jpdown.add(jtfc);            jpdown.add(jbd);            jpdown.add(jbe);                        con.add(jpup,BorderLayout.NORTH);            con.add(jsp,BorderLayout.CENTER);            con.add(jpdown,BorderLayout.SOUTH);                        frame.setSize(200, 100);            frame.pack();            frame.setVisible(true);}public static void main(String[] args){SwingUtilities.invokeLater(new Runnable(){public void run(){new Searcher().creatAndShoeGUI();}});}static class  Search {static Directory directory = null;//存储方式static IndexReader reader = null;//读入引索static IndexSearcher searcher = null;//确定搜索对象static QueryParser parser = null;//用于确定搜索时的引索的版本以及分词器static Query query = null;//记录要搜索的词语static TopDocs tds = null;//记录搜索后返回的结果static Document document = null;//存放搜索结果以便于提取结果static ScoreDoc[] sds = null;//存放TopDocs传来的内容(搜索结果)public static void search(String indexPath,int num){//indexPath引索存放的目录,num为显示搜索结果的条数try {directory = FSDirectory.open(new File(indexPath));//创建directory,其储存方式为在硬盘上储存} catch (IOException e) {//System.out.println("创建Directory时发生错误!");e.printStackTrace();}try {reader = IndexReader.open(directory);//读入索引} catch (CorruptIndexException e) {//System.out.println("创建IndexReader时发生错误!");e.printStackTrace();} catch (IOException e) {//System.out.println("创建IndexReader时发生错误!");e.printStackTrace();}searcher = new IndexSearcher(reader);parser = new QueryParser(Version.LUCENE_35,"content",new IKAnalyzer());try {query = parser.parse("offraient");//设置搜索的关键词} catch (ParseException e) {//System.out.println("query = parser.parse(\"keyword\")时发生错误");e.printStackTrace();}try {tds = searcher.search(query,num);//设置显示的行数} catch (IOException e) {//System.out.println("std = searcher.search(query,5);时发生错误");e.printStackTrace();} sds = tds.scoreDocs;System.out.println("一共搜索到: "+sds.length+" 条");if(sds.length != 0){for( ScoreDoc sd:sds){try {document = searcher.doc(sd.doc);} catch (CorruptIndexException e) {System.out.println("document = searcher.doc(sd.doc);时发生错误");e.printStackTrace();} catch (IOException e) {System.out.println("document = searcher.doc(sd.doc);时发生错误");e.printStackTrace();}//System.out.println(document.get("title")+"["+document.get("path")+"]");}}else//System.out.println("The word you enter can't be found!");try {reader.close();} catch (IOException e) {//System.out.println("关闭reader时发生错误!");e.printStackTrace();}//System.out.println("Finished");}public void check() throws IOException{//检查索引是否被正确建立(打印索引)directory = FSDirectory.open(new File("index"));IndexReader reader = IndexReader.open(directory);for(int i = 0;i<reader.numDocs();i++){//System.out.println(reader.document(i));}}}}

原创粉丝点击