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();
}
}
- lucene学习----创建索引
- lucene创建索引
- Java_ABC_3.Lucene创建索引
- Lucene 索引创建
- LUCENE创建索引【鸡蛋】
- Lucene 索引创建
- lucene--创建索引,搜索
- lucene创建索引
- Lucene 4.4 创建索引
- Lucene.Net创建索引
- lucene 索引创建查询
- Lucene 索引创建 .
- lucene创建索引
- lucene 创建索引
- Lucene之创建索引
- Lucene创建空索引
- Lucene-索引创建&搜索
- Lucene索引创建过程
- 【Java】interrupt、interrupted和isInterrupted的区别
- Docker系列(七)Shipyard安装及介绍
- KNN算法R实现
- 如何修改tomcat项目的图标
- Docker系列(八)Kubernetes介绍
- lucene创建索引
- 关于自动拆箱的危害--慎重自动拆箱
- MFC的来龙去脉-----消息循环,找处理场所
- Docker系列(九)Kubernetes安装
- MFC的来龙去脉-----热身开胃汤
- 系统学习机器学习之神经网络(四) --SOM
- MFC的来龙去脉-----消息处理,找处理函数
- 结合CSerialPort类,实现完整的串口收发功能
- Spark系列(三)SparkContext分析