• Lucene.NET搜索多个索引文件


    有时对于一个Document来说,有一些Field会被频繁地操作,而另一些Field则不会。这时可以将频繁操作的Field和其他Field分开存放,而在搜索时同时检索这两部分Field而提取出一个完整的Document。 

        这要求两个索引包含的Document的数量必须相同, 在创建索引的时候,可以同时创建多个IndexWriter,将一个Document根据需要拆分成多个包含部分Field的Document,并将这些Document分别添加到不同的索引。

         而在搜索时,则必须借助ParallelReader类来整合。

    Directory dir1=FSDirectory.getDirectory(new File(INDEX_DIR1),false);
         Directory dir2
    =FSDirectory.getDirectory(new File(INDEX_DIR2),false);
        ParallelReader preader
    =new ParallelReader();
         preader.add(IndexReader.open(dir1));
         preader.add(IndexReader.open(dir2));
         IndexSearcher searcher
    =new IndexSearcher(preader);

    之后的操作和一般的搜索相同。

    Lucene.NET 同时搜索多个索引

      在创建索引的时候可以根据分类需要创建多个索引,而在搜索时可以同时搜索所有的索引, 这一功能通过MultiSearcher实现。

    Java模式的写法:
    IndexSearcher[] searchers
    =new IndexSearcher[]{new IndexSearcher(dir1),new IndexSearcher(dir2)};
           MultiSearcher msearcher
    =new MultiSearcher(searchers);

    .NET模式的写法:
    MultiReader reader 
    = new MultiReader(new IndexReader[] { 
            IndexReader.Open(System.IO.Path.Combine(Tpl.CommonSet.Get(
    "ThreadsIndexsData"),"main")),
            IndexReader.Open(System.IO.Path.Combine(Tpl.CommonSet.Get(
    "ThreadsIndexsData"),"1")) }
            );
        IndexSearcher searcher 
    = new IndexSearcher(reader);

  • 相关阅读:
    并发和多线程(二)--启动和中断线程(Interrupt)的正确姿势
    并发和多线程(一)--创建线程的方式
    MySQL系列(十二)--如何设计一个关系型数据库(基本思路)
    Elasticsearch系列(二)--query、filter、aggregations
    Spark-python入门
    python中的Turtle模块
    pytorch显示网络结构
    风格迁移(2)-Fast Style Transfer
    风格迁移(1)-格拉姆矩阵
    使用GAN生成图片
  • 原文地址:https://www.cnblogs.com/wudingfeng/p/Lucene.html
Copyright © 2020-2023  润新知