lucene创建索引

来源:互联网 发布:淘宝直通车类目受限 编辑:程序博客网 时间:2024/06/09 16:27

具体代码如下:


//生成页面生成索引

 public void generStaticPage(String id, HttpServletRequest request) throws IOException {
        Info info = infoDao.findOne(id);
        VelocityEngine engine = templateService.getInstance();
        templateService.generalPage(info);

//webapps下面新建indexpack存放生成的索引文件
        String indexpackUrl = InfoService.class.getResource("/").getPath()
                .replaceFirst("/", "").replaceAll("WEB-INF/classes/", "")
                + "static/indexpack";
        Document doc =null;
        try {
            doc=new Document();
            Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
            Field titleField = new Field("title", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
            Field contentField = new Field("content", "", Field.Store.YES, Field.Index.ANALYZED);
           
            idField.setValue(info.getId());
            doc.add(idField);
            titleField.setValue(info.getTitle());
            doc.add(titleField);
            contentField.setValue("content");
            doc.add(contentField);

          
            IndexWriter indexWriter = new IndexWriter(
                    new SimpleFSDirectory(new File(indexpackUrl)), new StandardAnalyzer(
                            Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);
          
            indexWriter.addDocument(doc);
            indexWriter.close();
       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }









//用到的service

package main.java.service;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Properties;


import main.java.beans.Info;


import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Service;


/**
 * TODO 此处描写类的信息
 * 
 * @time Dec 2, 2016 2:52:55 PM
 * @author cuixx
 * @since JDK1.7
 */
@Service
public class TemplateService{
   private static VelocityEngine engine;

//在classes下面新建一个
   private static String globalPath=TemplateService.class.getResource("/template").getPath();


   private void TemplateService() {engine=new VelocityEngine();}


   public VelocityEngine getInstance() {
       if(engine==null) {
        TemplateService();
       Properties properties=new Properties();
       System.out.println("@@@@@@@@@@path"+globalPath);
       properties.setProperty(engine.FILE_RESOURCE_LOADER_PATH, globalPath);
       engine.init(properties);
       }
       return engine;
   }
   
   public void generalPage(Info info) throws IOException {
       Template template=engine.getTemplate(info.getId()+".vm");
       VelocityContext vcontext=new VelocityContext();
       vcontext.put("title",info.getTitle());
       vcontext.put("content",info.getArticle().getInfo_content());
       StringWriter writer=new StringWriter();
       template.merge(vcontext, writer);

//在webapps下面新建static/htmlpage存生成的静态页
       File file=new File(TemplateService.class.getResource("/").getPath().replaceFirst("/", "").replaceAll("WEB-INF/classes/", "")+"static/htmlpage/"+info.getId()+".htm");
       if(file.exists()) {
           file.delete();
       }
       if(!file.exists()) {
           file.createNewFile();
       }
       OutputStreamWriter outputStream  = new OutputStreamWriter(new FileOutputStream(file, true));
       outputStream.write(writer.toString());
       outputStream.close();
   }
}

0 0
原创粉丝点击