java带有界面的lucene搜索器

太搓了,今天电脑中木马了,一直熬到晚上12点多才恢复正常,交一个程序。

package tianen;

import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
import org.apache.lucene.index.*;

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 lucenesearcher {
	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 LuceneSearcherTool();
							LuceneSearcherTool.search(phrase,indexPath);
						}
						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 lucenesearcher().creatAndShoeGUI();
			}
		}
		);
	}
	static class LuceneSearcherTool
	{
		public static void search(String phrase,String indexPath)throws IOException
		{
			IndexSearcher searcher = new IndexSearcher(indexPath);
			Term t= new Term("text",phrase);
			Query q=new TermQuery(t);
			Hits hs = searcher.search(q);
			int num = hs.length();
			jta.setText("检索的记录数量为:"+num+"\n");
			jta.setText(jta.getText()+"******************************************************"+"\n\n");
			for(int i=0;i<num;i++)
			{
				Document doc=hs.doc(i);
				if(doc==null)
				{
					continue;
				}
				Field field = doc.getField("filename");
				String filename=field.stringValue();
				field=doc.getField("uri");
				String uri =field.stringValue();
				field = doc.getField("cdate");
				String cdate=field.stringValue();
				field = doc.getField("digest");
				String digest=field.stringValue();
				StringBuffer sb=new StringBuffer();
				sb.append("URI:"+uri+"\n");
				sb.append("filename:"+filename+"\n");
				sb.append("cdate:"+cdate+"\n");
				sb.append("digest:"+digest+"\n");
				sb.append("------------------------------------------------"+"\n");
				jta.setText(jta.getText()+sb.toString());
			}
			searcher.close();
		}
	}

}


 

你可能感兴趣的:(java,exception,String,null,Lucene,import)