Lucene进行中文分词索引构建时出现的问题,谁能帮我解答!

package IndexService;

/**
 * 
 * @method Lucene索引操作对象工具类
 * @author Mr yi
 * @time 2019年5月23日
 */
public class LuceneUtils {

    private static Directory directory  = null;

//    private static IndexWriterConfig indexWriterConfig = null; 

    private static Analyzer analyzer = null;
    
    private static Version matchVersion = null;
    
    //static 代码块随着类的加载,只加载一次。作用是初始化类。
    static{
        try {
            //在 6.6 以上版本中 version 不再是必要的,并且,存在无参构造方法,可以直接使用默认的 StandardAnalyzer 分词器。
            matchVersion = Version.LUCENE_8_5_0;
            //索引存放的位置,设置在当前目录中(项目根路径下)
//            final String INDEXURL = "E://users//Administrator//eclipse-workspace//searchEngine//indexDir";
//            directory = FSDirectory.open(new File(INDEXURL).toPath());/* Paths.get(INDEXURL)*/
            //analyzer = new StandardAnalyzer(); // 标准分词器,适用于英文[支持中文采用的方法为单字切分。他会将词汇单元转换成小写形式,并去除停用词和标点符号]
            //analyzer = new SmartChineseAnalyzer();//中文分词
            //analyzer = new ComplexAnalyzer();//中文分词
            //analyzer = new IKAnalyzer();//中文分词
             analyzer = new IKAnalyzer();//中文分词
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Directory getDirectory() {
        return directory;
    }

    /**
     * 
     * @method 返回用于操作索引的对象
     * @author Mr yi
     * @time 2019年5月23日
     * @return
     * @throws Exception
     */
    public static IndexWriter getIndexWriter() throws Exception{
        //创建索引写入配置
//        indexWriterConfig = new IndexWriterConfig(analyzer);
        //创建索引写入对象
//        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        IndexWriter indexWriter = new IndexWriter("E://users//Administrator//eclipse-workspace//searchEngine//indexDir",analyzer, true);
        return indexWriter;
    }

    /**
     * 
     * @method 返回用于读取索引的对象
     * @author Mr yi
     * @time 2019年5月23日
     * @return
     * @throws Exception
     */
    public static IndexSearcher getIndexSearcher() throws Exception{
        IndexReader indexReader  = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        return indexSearcher;
    }

    /**
     * 
     * @method 返回当前版本
     * @author Mr yi
     * @time 2019年5月23日
     * @return
     */
    public static Version getMatchVersion() {
        return matchVersion;
    }

    /**
     * 
     * @method 返回当前使用的分词器
     * @author Mr yi
     * @time 2019年5月23日
     * @return
     */
    public static Analyzer getAnalyzer() {
        return analyzer;
    }
    
    
    /**
     * 
     * @method 将Article数据转换为Document
     * @author Mr yi
     * @time 2019年5月23日
     * @param article 对象
     * @return
     * @throws IOException 
     */
    public static  Document articleToDocument(News news) throws IOException{
         
        if(news==null)
        return null;
        
        Document document = new Document();
        StringField identifier = new StringField("newsId",news.getId(),Store.YES);
        StringField newsurl = new StringField("newsUrl",news.getUrl(),Store.YES);
        TextField newstitle = new TextField("newsTitle",news.getTitle(),Store.YES);
        TextField newsdate = new TextField("newsDate",news.getDate(),Store.YES);
        TextField newsbody = new TextField("newsBody",news.getBody(),Store.YES);
        long mills = System.currentTimeMillis();
        StringField indextime = new StringField("indexTime",mills+"",Store.YES);
        StringField newsdate2 = new StringField("newDate2",news.getDate().substring(0, 4)+news.getDate().substring(5, 7)+news.getDate().substring(8, 10),Store.YES);
        
        document.add(identifier);  
        document.add(newsurl);  
        document.add(newstitle); 
        document.add(newsdate);  
        document.add(newsbody);
        document.add(indextime);  
        document.add(newsdate2);  
        return document;
    }

  /**
     * 
     * @method 添加索引
     * @author Mr yi
     * @time 2019年5月24日
     * @param document
     * @throws Exception
     */
   public static void addIndex(String path) throws Exception{
       //获取indexWrite对象
       IndexWriter indexWriter =  LuceneUtils.getIndexWriter();
       
       File folder = new File(path);
       if (folder.isDirectory()) {
        File[] list = folder.listFiles();
        for (File f : list) {
            File file = new File("E://users//Administrator//eclipse-workspace//searchEngine//dataDir//"+f.getName());
            Newsdao nd=new Newsdao();
         ArrayList<String> content = nd.readFileContent(file);
         String id = nd.readFileId(file);
         News news = new News();
         news.setId(id);
//         System.out.println(news.getId());
         news.setUrl(content.get(0).replace("url:", ""));
//         System.out.println(news.getUrl());
         news.setTitle(content.get(1).replace("title:", ""));
//         System.out.println(news.getTitle());
         news.setDate(content.get(2).replace("time:", ""));
//         System.out.println(news.getDate());
         news.setBody(content.get(3));
//         System.out.println(news.getBody());
       
    Document document = LuceneUtils.articleToDocument(news);
    
    try {
           //将document写入磁盘中
          indexWriter.addDocument(document);
      }finally {//定要注意关闭indexWrite. 包括异常下,用finally关闭.否则会导致下一次写索引失败.,修改程序后,直接删除write.lock文件后就可以
          indexWriter.close();
      }
    }}
       
      
       
   }
   
   /**
    * 添加索引
    * @param article
    * @throws Exception 
    */
   public static void main(String[] args) throws Exception{
//       IndexWriter indexWriter =  LuceneUtils.getIndexWriter();
//       News news  = new News(); //这里需要给news 对象赋值 
       String path = "E://users//Administrator//eclipse-workspace//searchEngine//dataDir";
       LuceneUtils.addIndex(path);
    
   }

 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lucene是一个功能强大、高性能的开源搜索引擎库,它提供了索引和分词机制来提升搜索的效率和准确性。 索引lucene中重要的概念之一。在搜索引擎中,文档被索引后才能被搜索到。lucene使用倒排索引的方式进行索引构建,也就是将文档中的词语作为索引的基本单位,而不是文档本身。lucene将每个词语与它所在的文档进行映射,这样在搜索就能快速找到包含指定词语的文档。通过索引lucene能够高效地处理大量的文档,并且支持各种搜索方式,如全文搜索、精确搜索、模糊搜索等。 分词是将文本切割成一个个有意义的词语的过程。在搜索引擎中,将文档进行分词后可以提高搜索的效果,使得搜索更加准确。lucene提供了多种分词器,可以根据不同的语言和需求选择适合的分词器。分词器将文本中的词语提取出来,并进行词根化、去掉停用词等预处理操作,使得搜索能够匹配到更多的相关文档。例如,在对一篇英文文档进行索引前,lucene的分词器会将该文档中的单词逐个提取出来,并将单词转化为词干形式,如将“running”转化为“run”,以提高搜索的准确性和覆盖度。 总之,lucene索引和分词机制是搜索引擎实现高效、准确搜索的重要组成部分。通过索引lucene可以快速定位到包含指定词语的文档;通过分词,lucene可以对文本进行预处理,使得搜索能够匹配更多相关文档。这些机制的结合让lucene具备了强大的搜索能力,广泛应用于各类搜索引擎和文本检索系统中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值