package jim.searcher;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.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;import org.apache.lucene.search.Sort; class jiemian {  
   JSearcher JS;
   JTextField jtfa;  
   JButton jba;  
   JTextField jtfb;  
   JButton jbb;  
   JButton jbc;  
   JTextArea jta;  
   JTextField jtfc;  
   JButton jbd;  
   JButton jbe;  
    public  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();  
                            JS.word  = jtfb.getText();  ///
                            JS.CreatIndex( indexPath,"testFiles","jim");///有错的就是这三句。
                            JS.ks();///
                        }  
                       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 jiemian().creatAndShoeGUI();
}
}
);
}
}

解决方案 »

  1.   

    package jim.searcher;/////////////这就是调用的JSearch类,不知道是不是这个类写错了
    import java.io.File;
    import java.io.IOException;import jim.Figurer.Figurer;import org.apache.lucene.analysis.Analyzer;
    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.IndexReader;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.queryParser.MultiFieldQueryParser;
    import org.apache.lucene.queryParser.ParseException;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.Sort;
    import org.apache.lucene.search.TopDocs;
    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;
    public class JSearcher {
    jiemian jm;
    String word;
    int total = 0;
    @SuppressWarnings("static-access")
    /* indexPath:存放索引的地址名.
     * textFile :要转换为txt格式的二进制文件的地址.
     * filePath :要搜索的文件的文件夹地址.(已转换为txt)注意:该文件夹需要自己创建.
     */
    public void CreatIndex(String indexPath,String textFile,String filePath){ //创建索引indexPath:存放索引的地址名
    Directory directory = null; //用来决定引索目录的储存方式
    IndexWriter writer = null; //引索器
    Document document = null; //引索文件
    Field field = null; //字段信息
    IndexWriterConfig iwc = null; //用来选择lucene的版本以及分词器的版本
    String title = "title"; //文件的标题
    String content = "content"; //文件的内容
    String [] files = null; //用来记录文件夹里所有文件的地址
    try {
    directory = FSDirectory.open(new File(indexPath)); //创建directory,其储存方式为在硬盘上储存 } catch (IOException e) {
    System.out.println("创建Directory时发生错误!"); 
    jm.jta.setText("创建Directory时发生错误!"); //**********************
    e.printStackTrace();
    }
    iwc = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer()); //选择lucene的版本以及分词器的版本
    try {
    writer = new IndexWriter(directory,iwc); //创建引索器
    } catch (CorruptIndexException e) {
    System.out.println("创建IndexWriter时发生错误!");
    jm.jta.setText("创建IndexWriter时发生错误!");
    e.printStackTrace();
    } catch (LockObtainFailedException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();

    try {
    files = FileList.getFiles(textFile); //记录文件夹里所有文件的地址
    for(int i = 0;i<files.length;i++){
    Figurer figurer = new Figurer(); //建立Figure用来分析不同类型的文件
    File file = new File(files[i]);
    try {
    figurer.toTextFile(files[i], filePath+"\\"+file.getName()+".txt"); //将不同类型的文件转换为txt格式
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } catch (IOException e) {
    System.out.println("无法打开存放要搜索的文件的文件夹");                               /////*********************
    jm.jta.setText("无法打开存放要搜索的文件的文件夹");
    e.printStackTrace();
    }

    try {
    files = FileList.getFiles(filePath); //存放要搜索的文件的文件夹
    } catch (IOException e1) {
    e1.printStackTrace();
    } //记录文件夹里所有文件的地址
    int num = files.length; //记录文件夹里所有文件的总数
    for( int i = 0; i < num ; i++ ){
    document = new Document(); //创建索引文件
    File file = new File(files[i]);
    title = file.getName(); //取得文件的名字
    field = new Field("title",title,Field.Store.YES,Index.NOT_ANALYZED); //创建索引字段
    document.add(field);
    content = FileText.getText(file);
    field = new Field("content",content,Field.Store.NO,Index.ANALYZED);
    document.add(field);
    String Path = file.getPath(); //获取文件的路径
    field = new Field("path",Path,Field.Store.YES,Index.NOT_ANALYZED);
    document.add(field);
    field = new Field("id",String.valueOf(i),Field.Store.YES,Index.NOT_ANALYZED);
    document.add(field);
    System.out.println("File: "+title+"  Indexed");
    try {
    writer.addDocument(document);
    } catch (CorruptIndexException e) {
    System.out.println("将Document写入IndexWriter时错误!");                         //*********************
    jm.jta.setText("将Document写入IndexWriter时错误!");
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    try {
    writer.close(); //关闭索引器
    } catch (CorruptIndexException e) {
    e.printStackTrace();
    } catch (IOException e) {
    System.out.println("IndexWriter关闭时错误!");                                //*******
    jm.jta.setText("将Document写入IndexWriter时错误!");
    e.printStackTrace();
    }
    System.out.println("Index is Created!");  
    }
    /* indexPath:索引存放的路径. 
     * field1:执行搜索的域.
     * field2:执行搜索的域.
     * keyWord:搜索的关键词
     * sort:默认填入Sort.INDEXORDER即可.
     * 以下是Sort的相关解释
     * Sort INDEXORDER:Represents sorting by index order. 
     * Sort RELEVANCE :Represents sorting by computed relevance. 
     * Using this sort criteria returns the same results as calling Searcher#search()without a sort criteria,
     *  only with slightly more overhead.  */
    public void Search(String indexPath,String field1,String field2,String keyWord,Sort sort,StringBuffer sb){

    Directory directory = null;
    IndexReader reader = null;
    TopDocs tds = null;
    Query query = null;
    Document doc = null;
    ScoreDoc[] sds1 = null;
    Analyzer analyzer=new IKAnalyzer();

        try {
    directory=FSDirectory.open(new File(indexPath));
        reader=IndexReader.open(directory);
        IndexSearcher searcher=new IndexSearcher(reader);
        String[] fields={field1,field2};
        MultiFieldQueryParser mp=new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);
        try {
    query=mp.parse(keyWord);
    } catch (ParseException e) {
    e.printStackTrace();
    }
        tds=searcher.search(query,50,sort);
        total=tds.totalHits;
        sds1=tds.scoreDocs; 
        for(ScoreDoc sd:sds1)
        {
           doc=searcher.doc(sd.doc);
           sb.append("Title: "+"["+doc.get("title")+"]"+" Path: "+"["+doc.get("path")+"]");
           sb.append('\n');
        }    
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    /* 得到搜索结果的条数 */
    public int getTotal(){
    return  total;
    }

    public void ks(){
    StringBuffer sb = new StringBuffer();
    JSearcher searcher = new JSearcher();
    PINYIN p = new PINYIN(); //将汉字转换成拼音
    //pinyinReader pyreader = new pinyinReader();
    //pyreader.ReadPY(); //对拼音词库建立索引
    //searcher.CreatIndex("index", "testFiles", "jim"); //建立索引
    pySearch py = new pySearch();
    boolean judge = false;
    for(int i = 0; i < word.length();i++)
    if((word.charAt(i)<'A'||word.charAt(i)>'z')&&word.charAt(i)!=' ')
    {
    // System.out.println("汉字");
    judge = true;
    break;
    }
    else
    {
    // System.out.println("字母");
    judge = false;
    }
    if(!judge)
    {
    py.Search(word,sb);
    searcher.Search("index", "title", "content", word, Sort.INDEXORDER,sb);
    System.out.print(sb);                                                                             //*****
    //jm.jta.setText(sb);
    }
    else
    {

    searcher.Search("index", "title", "content", word, Sort.INDEXORDER,sb);
    System.out.println("共搜到"+searcher.getTotal()+"条结果 : ");                                          //****
    jm.jta.setText("共搜到"+searcher.getTotal()+"条结果 : "); 
    System.out.print(sb);                                                                             //****
    //jm.jta.setText(sb);
    p.PinYin(word);
    p.print("mytest.txt");

    }


    }
    }
      

  2.   

    每人回答只有几个原因:
    1、你的代码太乱了,命名看不懂。
    2、不知道你的类里面写了什么,而且好像貌似java里面没看到过Jsearcher
    3、请你不要把自己的包名也给拷进来,还有把你自己导入的(import org.apache.lucene.search.Sort;
    )后面写个注释行吗,别人看不懂啊,只认得里面有个Searcher,大概是个搜索器。
    ----------严重建议买个字典什么的,至少把命名给弄规范了!让别人能看得懂!