zoukankan      html  css  js  c++  java
  • 艾伟_转载:Lucene.net多字段多索引目录搜索 狼人:

    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);

    这个搜索是对搜索后的结果进行合并,剔除重复的信息。

  • 相关阅读:
    AMap公交线路查询
    AMap行政区查询服务
    使用JavaScript的Join方法
    获取layer.open弹出层的返回值
    MVC项目中WebViewPage的实战应用
    ASP.NET管道
    Android接收系统广播
    python字符串转换成数字
    mysql缓存
    两个矩阵对应位置的数据相加,并返回一个矩阵
  • 原文地址:https://www.cnblogs.com/waw/p/2157033.html
Copyright © 2011-2022 走看看