Lucene创建索引_unable to create index directory: /lucene/indexes-程序员宅基地

package com.onlydoit.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;


import com.youedata.bean.FileBean;


/**Lucene索引工具类
 * @author youedata
 *
 */
public class LuceneIndexUtils {

/**创建或追加内容索引
* @param txtPath txt文件
* @param ipath 生成索引位置
* @param startRecPos 开始位置
* @param indexedRecNum 索引记录数
* @throws Exception
*/
public static void createLuIndexByTxt(String txtPath, String ipath, int startRecPos, int indexedRecNum) throws Exception {
FileBean bean = readSingleFile(txtPath, "utf-8");
Directory directory = null;


IndexWriter iw = null;
File indexFile = new File(ipath);
directory = FSDirectory.open(indexFile);
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46, analyzer);
iwc.setMaxBufferedDocs(indexedRecNum);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
iw = new IndexWriter(directory, iwc);


String name = bean.getFileName();
name = name.replace(".txt", "");
String id = name;
System.out.println(id);
Document doc = new Document();


String content = bean.getFileContent();
content = content.replaceAll("《", "");
content = content.replaceAll("》", "");
content = content.replaceAll("\\(", "");
content = content.replaceAll("\\)", "");
content = content.replaceAll("\\)", "");
content = content.replaceAll("\\(", "");
content = content.replaceAll("\\[", "");
content = content.replaceAll("\\]", "");
content = content.replaceAll("【", "");
content = content.replaceAll("】", "");
content = content.replaceAll("〔", "");
content = content.replaceAll("〕", "");
content = content.replaceAll("”", "");
content = content.replaceAll("“", "");
content = content.replaceAll("\"", "");
doc.add(new StringField("id", id, Field.Store.YES));
doc.add(new StringField("fileContent", content, Field.Store.YES));
iw.addDocument(doc);
System.out.println("成功");
iw.close();
}


/**获取txt文件信息到实体类
* @param path
* @param encoding
* @return
* @throws Exception
*/
public static FileBean readSingleFile(String path, String encoding) throws Exception {
File file = new File(path);
FileBean bean = new FileBean();
if (file.isFile()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader br = new BufferedReader(read);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
bean.setFileContent(sb.toString());
bean.setFileName(file.getName());
bean.setFilePath(file.getPath());
read.close();
}
return bean;
}


    /**
      * 删除政策索引
      * 
      * @param str 删除的关键字
      * @param INDEX_DIR 索引路径
      * @throws Exception
      */
     public static void deletePolicyIndex(String policyId,String INDEX_DIR) throws Exception {
         Date date1 = new Date();
         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
         Directory directory = FSDirectory.open(new File(INDEX_DIR));
         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
         IndexWriter indexWriter = new IndexWriter(directory, config);
         indexWriter.deleteDocuments(new Term("id",policyId));  
         indexWriter.close();
         Date date2 = new Date();
         System.out.println("删除索引耗时:" + (date2.getTime() - date1.getTime()) + "ms\n");
     }
     
     /**更新政策索引
     * @param temp
     * @param ipath
     * @param startRecPos
     * @param indexedRecNum
     * @throws IOException
     */
    public static void updatePolicyIndex(FileBean temp,String ipath, int startRecPos,int indexedRecNum) throws IOException{
    Directory directory = null;
  IndexWriter iw = null;
  File indexFile = new File(ipath);
  directory = FSDirectory.open(indexFile);
  Analyzer analyzer = new IKAnalyzer();
  IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,analyzer);
  iwc.setMaxBufferedDocs(indexedRecNum);
  iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
  iw = new IndexWriter(directory, iwc);
  Document doc = new Document();
  int a = getWeight(temp.getPolicyPic(), temp.getPolicyRead(),Integer.parseInt(temp.getPubDateFors()));
  doc.add(new StringField("id",temp.getId(),Field.Store.YES));
  doc.add(new StringField("unitId", temp.getUnitId(), Field.Store.YES));
  doc.add(new StringField("unitName", temp.getUnitName(),Field.Store.YES));
  doc.add(new StringField("unitAllName", temp.getUnitAllName(),Field.Store.YES));
  doc.add(new StringField("unitType", temp.getUnitType(),Field.Store.YES));
  doc.add(new StringField("unitTypeName", temp.getUnitTypeName(),Field.Store.YES));
  doc.add(new StringField("themeId",temp.getThemeId(), Field.Store.YES));
  doc.add(new StringField("themeName", temp.getThemeName(),Field.Store.YES));
  doc.add(new IntField("pubDateFors", Integer.parseInt(temp.getPubDateFors()), Field.Store.YES));
  doc.add(new StringField("pubDateFord", temp.getPubDateFord(),Field.Store.YES));
  doc.add(new StringField("fileName", temp.getFileName(),Field.Store.YES));
  doc.add(new StringField("fileNo", temp.getFileNo(), Field.Store.YES));
  doc.add(new StringField("policyPic", temp.getPolicyPic(),Field.Store.YES));
  doc.add(new StringField("policyRead", temp.getPolicyRead(),Field.Store.YES));
  doc.add(new StringField("belongUnit", temp.getBelongUnit(),Field.Store.YES));
  doc.add(new IntField("weight", a, Field.Store.YES));
  // -------------------新增字段的域、值-----------------------------
  doc.add(new StringField("interviewOnline", temp.getInterviewOnline(), Field.Store.YES));
  doc.add(new StringField("ecologicalKeyWordsId", temp.getEcologicalKeyWordsId(), Field.Store.YES));
  doc.add(new StringField("ecologicalKeyWords", temp.getEcologicalKeyWords(), Field.Store.YES));
  doc.add(new StringField("timeStageId", temp.getTimeStageId(),Field.Store.YES));
  doc.add(new StringField("timeStage", temp.getTimeStage(),Field.Store.YES));
  // -------------------新增字段的域、值-----------------------------
  iw.updateDocument(new Term("id", temp.getId()), doc);
  System.out.println("更新索引成功");
  iw.close();
     }
     /**创建或追加标题索引
     * @param temp
     * @param ipath
     * @param startRecPos
     * @param indexedRecNum
     * @throws Exception
     */
    public static void createTitleIndexByBean(FileBean temp,String ipath, int startRecPos,int indexedRecNum) throws Exception {
 
  Directory directory = null;
  IndexWriter iw = null;
  File indexFile = new File(ipath);
  directory = FSDirectory.open(indexFile);
  Analyzer analyzer = new IKAnalyzer();
  IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,analyzer);
  iwc.setMaxBufferedDocs(indexedRecNum);
  iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
  iw = new IndexWriter(directory, iwc);
Document doc = new Document();
int a = getWeight(temp.getPolicyPic(), temp.getPolicyRead(),Integer.parseInt(temp.getPubDateFors()));
  doc.add(new StringField("id",temp.getId(),Field.Store.YES));
  doc.add(new StringField("unitId", temp.getUnitId(), Field.Store.YES));
  doc.add(new StringField("unitName", temp.getUnitName(),Field.Store.YES));
  doc.add(new StringField("unitAllName", temp.getUnitAllName(),Field.Store.YES));
  doc.add(new StringField("unitType", temp.getUnitType(),Field.Store.YES));
  doc.add(new StringField("unitTypeName", temp.getUnitTypeName(),Field.Store.YES));
  doc.add(new StringField("themeId",temp.getThemeId(), Field.Store.YES));
  doc.add(new StringField("themeName", temp.getThemeName(),Field.Store.YES));
  doc.add(new IntField("pubDateFors", Integer.parseInt(temp.getPubDateFors()), Field.Store.YES));
  doc.add(new StringField("pubDateFord", temp.getPubDateFord(),Field.Store.YES));
  doc.add(new StringField("fileName", temp.getFileName(),Field.Store.YES));
  doc.add(new StringField("fileNo", temp.getFileNo(), Field.Store.YES));
  doc.add(new StringField("policyPic", temp.getPolicyPic(),Field.Store.YES));
  doc.add(new StringField("policyRead", temp.getPolicyRead(),Field.Store.YES));
  doc.add(new StringField("belongUnit", temp.getBelongUnit(),Field.Store.YES));
  doc.add(new IntField("weight", a, Field.Store.YES));
  // -------------------新增字段的域、值-----------------------------
  doc.add(new StringField("interviewOnline", temp.getInterviewOnline(), Field.Store.YES));
  doc.add(new StringField("ecologicalKeyWordsId", temp.getEcologicalKeyWordsId(), Field.Store.YES));
  doc.add(new StringField("ecologicalKeyWords", temp.getEcologicalKeyWords(), Field.Store.YES));
  doc.add(new StringField("timeStageId", temp.getTimeStageId(),Field.Store.YES));
  doc.add(new StringField("timeStage", temp.getTimeStage(),Field.Store.YES));
// -------------------新增字段的域、值-----------------------------
iw.addDocument(doc);
System.out.println("创建标题索引成功");
  iw.close();
  }


  public static int getWeight(String policyPic, String policyRead,int pubDateFors) {
  int a = 0;
  if (policyPic.equals("否") && policyRead.equals("否")) {
  a = pubDateFors;
  } else if (!policyPic.equals("否") && !policyRead.equals("否")) {
  a = 4 * pubDateFors;
  } else {
  a = 2 * pubDateFors;
  }
  return a;
  }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hpuly/article/details/61918025

智能推荐

Android 用户界面---拖放(Drag and Drop)(三)_dragview 没收到action-drop-程序员宅基地

文章浏览阅读398次。设计拖放操作本节主要内容如下:1. 如何开始拖拽;2. 在拖拽期间如何响应事件;3. 如何响应落下事件;4. 如何结束拖放操作。开始拖拽用户使用一个拖拽手势开始拖拽,通常是在View对象上长按。在响应中,应该做下列事情:1. 必要时,给要移动的数据创建一个ClipData和ClipData.Item对象,作为ClipData_dragview 没收到action-drop

巨变前夜:智能汽车产业迎来“软件定义汽车”时代-程序员宅基地

文章浏览阅读381次。在电动化、智能化、网联化、共享化等“新四化”产业变革趋势下,汽车正在由过去单纯的机械代步工具向新一代移动智能终端转变。而针对智能汽车这一新兴产业,我国也在近年来密集推出了多项行动计划或发展..._汽车属性由机械产品向软硬件结合甚至是软件为主导的产品转变

HGDB修改数据库用户访问权限举例_hgdb 用户被锁定-程序员宅基地

文章浏览阅读242次。highgo=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+----------+-------------+----------_hgdb 用户被锁定

word2vec原理总结_word2vec总结-程序员宅基地

文章浏览阅读412次。CBOW与Skip-Gram模型基础:https://www.cnblogs.com/pinard/p/7160330.htmlHierarchical Softmax的模型:https://www.cnblogs.com/pinard/p/7243513.htmlNegative Sampling的模型:https://www.cnblogs.com/pinard/p/7249903.h..._word2vec总结

ORACLE SQL 优化的若干方法详细_oracle有缓存无法优化sql-程序员宅基地

文章浏览阅读1.4k次。ORACLE SQL 优化的若干方法详细一、 先介绍一下oracle的SGA:数据库的系统全局区,SGA主要由三部分构成:共享池、数据缓冲区、日志缓冲区。1、 共享池又由两部分构成:共享SQL区和数据字典缓冲区。共享SQL区专门存放用户SQL命令,oracle使用最近最少使用等优先级算法来更新覆盖;数据字典缓冲区(library cache)存放数据库运行的动态信息。数据库运行一段_oracle有缓存无法优化sql

猫儿PDF-Word格式转换经验谈_pdf转换成分word之后叠加怎么办?-程序员宅基地

文章浏览阅读1.2k次。猫儿我最近工作遇到了万恶的格式转换问题PDF-Word。众位看官抢着说“这不是很简单嘛?”“你就用XX软件就可以了嘛!”事实果真如此么?今天准备了10款软件和1份PDF文档进行测评。 1.ABBYY FineReader11一个知名的俄罗斯ORC软件,拥有高超的识别技术。OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数_pdf转换成分word之后叠加怎么办?

随便推点

android 触摸监听重写_Android 触摸事件监听(Activity层,ViewGroup层,View层)详细介绍...-程序员宅基地

文章浏览阅读1k次。Android不同层次的触摸事件监听APP开发中,经常会遇到有关手势处理的操作,比如向右滑动返回上一个页面。关于触摸事件的处理,我们可以大概处理在不同的层次上。Activity层:可以看做触摸事件获取的最顶层ViewGroup层:ViewGroup层可以自主控制是否让子View获取触摸事件View层:可以决定自己是否真正的消费触摸事件,如果不消费抛给上层ViewGroupActivity级别的手势..._android 屏幕触摸底层view监听不到

Omnipeek(wildpackets)抓包:过滤器设置和数据包分析-程序员宅基地

文章浏览阅读4.8k次。在C/S或B/S架构的系统级测试中,有时需要验证客户端是否发送了某种特定的数据包。可以通过Omnipeek抓包工具分析验证,过滤出符合要求的数据包。关键是设置过滤条件,使Omnepeek能够保存符合条件的数据包。设置过滤条件的过程是这样的:1、简单过滤器点击菜单栏view\filter打开过滤器视图,双击需要编辑的过滤打开Edit Filter对话框,如下:图1

MAC OS X安装配置Android Studio开发环境_os x 安装android studio-程序员宅基地

文章浏览阅读2.7w次,点赞2次,收藏3次。大三的最后阶段要实习一个月,实习的内容可选度不高,没自己喜欢的项目。折衷了一下,选择了安卓。安卓,没有使用过任何的安卓产品,所以还是想通过一个月的时间能对安卓有个细致的了解。 首先,要搭建开发环境。这很好,谷歌也刚刚发布了一款Android Studio安卓IDE,看教程貌似很强大。 通过http://www.android.com/下载安装文件后,双击将软件拖入到软件目录_os x 安装android studio

3unit5&6&7-程序员宅基地

文章浏览阅读117次。Red Hat System Administration III#############单元 五iSCSI远程块存储一、iSCSI概念iSCSI(Internet SCSI)支持从客户端(发起端)通过IP向远程服务器上的SCSI存储设备(目标)发送SCSI命令。iSCSI限定名称用于确定发起端和目标,并采用iqn.yyyy-mm.{reverse doma..._the device failed the self-test 201388

ios底部安全距离-程序员宅基地

文章浏览阅读6.9k次,点赞4次,收藏14次。背景: 目前公司开发商城小程序,对于iOS要设置底部安全距离,否则底部会被黑条遮挡技术方案:1、苹果官方推荐:使用env(),constant()来适配,env()和constant(),是IOS11新增特性,Webkit的css函数,用于设定安全区域与边界的距离,有4个预定义变量: safe-area-inset-left:安全区域距离左边边界的距离 safe-area-inset-right:安全区域距离右边边界的距离 safe-area...

第十五课.马尔科夫链蒙特卡洛方法_马尔可夫链是哪门课-程序员宅基地

文章浏览阅读438次。目录M-H采样Metropolis-Hastings采样原理M-H采样步骤Gibbs方法Gibbs核心流程Gibbs采样的合理性证明Gibbs采样实验在第十四课中讲述了马尔科夫链与其稳态的性质,本篇讨论基于马尔科夫链蒙特卡洛(MCMC)方法的采样。M-H采样Metropolis-Hastings采样原理我们的目标分布是p(z)p(z)p(z),同时我们手里有一个便于随时间进行遍历的马尔科夫链,其状态转移矩阵为QQQ。为了便于在马尔科夫链上随时间进行状态转移,这里的矩阵QQQ设计为:Qij=P(x_马尔可夫链是哪门课

推荐文章

热门文章

相关标签