日期:2014-05-19  浏览次数:20720 次

重赏:关于lucene 搜索的问题 满意就结贴
这是创建索引:
Java code

public void creatIndex()throws Exception {
        String dataPath = CmsContext.projectPath+PropertiesUtil.getProperty("lucene_source_dir");
        String indexPath = CmsContext.projectPath +PropertiesUtil.getProperty("lucene_index_dir");
        
        int i = 0 ;
        File indexDir = new File(indexPath);//索引存储目录
        File dir = new File(dataPath);// 待索引的数据文件目录

        Analyzer analyzer = null;// 采用的分词器
        FSDirectory directory = null;
        IndexWriter iwriter = null;
        FileInputStream fis = null;
        BufferedReader reader = null;
        try{
            if(!indexDir.exists())//如果不存在索引目录,则建立此目录
                indexDir.mkdirs();
            analyzer = new IKAnalyzer();
            directory = FSDirectory.open(indexDir);//打开目录
            iwriter = new IndexWriter(directory,
                    analyzer, true, IndexWriter.MaxFieldLength.LIMITED);//索引生成器
            List<File> files = new ArrayList<File>();
            FileUtil.getAllFiles(files, dir.getAbsolutePath());//获得目录下所有的文件,不包括文件夹
            for (File file:files) {
                if(file.isFile()){
                    Document doc = new Document();                
                    fis = new FileInputStream(file);
                    String content = "";
                    reader = new BufferedReader(new InputStreamReader(fis,PropertiesUtil.getProperty("static_charset")));//需要加上编码方式,否则中文都为乱码
        
                    StringBuffer buffer = new StringBuffer("");
                    content = reader.readLine();
                    while (content != null) {
                        buffer.append(content);
                        content = reader.readLine();
                    }
                    String html =  buffer.toString();
                    String title = Util.fetchContentByTag(html, "title");
                    String filePath = file.getPath().replace(CmsContext.projectPath,CmsContext.webCtx).replace("\\", "/");
                    doc.add(new Field("title", (title==null?file.getName():title), Field.Store.YES, Field.Index.ANALYZED));
                    doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.ANALYZED));
                    doc.add(new Field("content", html2Text(html), Field.Store.YES, Field.Index.ANALYZED));
                    doc.add(new Field("time", Util.date2str(new Date(file.lastModified()), "yyyy-MM-dd"), Field.Store.YES, Field.Index.ANALYZED));
                    
                    iwriter.addDocument(doc);
                    i++;
                }    
            }
        }catch(Exception e){throw e;}
        finally
        {
            if(reader!=null)reader.close();
            if(fis!=null)fis.close();
            if(iwriter!=null){
                iwriter.optimize();
                iwriter.close();
                }
            if(directory!=null)directory.close();
            if(analyzer!=null)analyzer.close();
        }

    }


这是 搜索:
Java code

public JSONObject searchIndex(String key, String luceneIndexDir)
            throws Exception {
        JSONObject obj = new JSONObject();
        JSONArray aryJson = new JSONArray();
        Directory dir = null;
        IndexSearcher search = null;
        Analyzer analyzer = null;
        
        try{
            File indexDir = new File(luceneIndexDir);
            dir = FSDirectory.open(indexDir);
            search = new IndexSearcher(dir,true);//read-only            

            
            Term term = new Term("content", key);        
            Quer