zoukankan      html  css  js  c++  java
  • Lucene.net多字段(Fields)、多索引目录(IndexSearcher)搜索

    Lucene.net是目前在.net环境中被普遍使用的全文索引的开源项目,这次在项目的开发中也使用它进行全文索引。
    在开发过程中碰到一些小问题就是对多字段和多索引目录进行搜索。
    1、多字段搜索就是同时要一个以上的字段中的内容进行比较搜索,类似概念在SQL中就是select * from Table where a like '%query%' or b like '%query%'。

    Lucene.net中的单个字段查询大家都比较熟悉,这里对字段content进行搜索
    Query query = QueryParser.Parse(querystr,"content",new ChineseAnalyzer());
    Hits hits = searcher.Search(query);

    对多个字段查询用到一个MultiFieldQueryParser对象,该对象继承自Query,我们要对字段title,content进行搜索。
    string[] fields = {"content","title"};
    Query multiquery = MultiFieldQueryParser.Parse(querystr,fields,new ChineseAnalyzer());
    Hits hits = searcher.Search(multiquery);

    2、多索引目录就是要在多个索引目录的中进行比较搜索,类似概念在SQL中就是select * from TableA union select * from TableB。
    IndexSearcher[] searchers = new IndexSearcher[2];
    searchers[0] = new IndexSearcher(IndexPath0);
    searchers[1] = new IndexSearcher(IndexPath1);

    MultiSearcher multisearcher = new MultiSearcher(searchers);
    TopDocs multitopdocs = multisearcher.Search(query, null, 1000);
    这个搜索的结果可能有相同的信息,比如你有一条相同的信息在多个目录中索引,搜索的结果就会出现多次相同的信息。

    还有一种搜索方式是用到ParallelMultiSearcher这个对象,它是从MulitSearcher继承而来。
    ParallelMultiSearcher parallelmultisearcher = new ParallelMultiSearcher(searchers);
    TopDocs paralleltopdocs = parallelmultisearcher.Search(query, null, 1000);
    这个搜索是对搜索后的结果进行合并,剔除重复的信息。
  • 相关阅读:
    POJ 2411 状态压缩递,覆盖方案数
    POJ 2774 最长公共子串
    POJ 1743 不可重叠的最长重复子串
    POJ 3294 出现在至少K个字符串中的子串
    POJ 3261 出现至少K次的可重叠最长子串
    POJ 1741/1987 树的点分治
    HDU1556 Color the ball
    解决linux系统时间不对的问题
    CentOS 6.9使用Setup配置网络(解决dhcp模式插入网线不自动获取IP的问题)
    Linux网络配置(setup)
  • 原文地址:https://www.cnblogs.com/0000/p/1531696.html
Copyright © 2011-2022 走看看