Lucene.Net:如何将MultiFiledQueryParser转换为BooleanQuery?

时间:2011-01-04 04:37:43

标签: lucene.net booleanquery

这是我当前执行MultiField Query

的代码
       ''# Variables used by Lucene
        Dim reader As IndexReader = IndexReader.Open(luceneDirectory, True)
        Dim searcher As IndexSearcher = New IndexSearcher(reader)
        Dim parser As QueryParser = New MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29,
                                                              New String() {"title",
                                                                            "description",
                                                                            "region",
                                                                            "pricelow",
                                                                            "pricehigh",
                                                                            "url",
                                                                            "user",
                                                                            "location"},
                                                                        New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29))
        Dim query As Query = parser.Parse(search.ToLower)


        ''# We want to ensure that only relevant dates are being returned.
        Dim nowValue = DateTools.DateToString(DateTime.Now, DateTools.Resolution.DAY)
        Dim dateFilter = If(searchPast, Nothing, FieldCacheRangeFilter.NewStringRange("filterdate",
                                                                                  lowerVal:=nowValue,
                                                                                  includeLower:=True,
                                                                                  upperVal:=Nothing,
                                                                                  includeUpper:=False))


''#============================================================================
''# Here's where I also need to add a regionFilter where I have something like

''# Dim regionFilter = New FieldCacheTermsFilter("region", New String() {HttpContext.Current.Request.Url.Subdomain})

''# This is because we must always ONLY ever return search results that are from
''# HttpContext.Current.Request.Url.Subdomain (where Subdomain is an extension 
''# method that returns the name of the region... much like Kijiji.ca)
''#============================================================================

        Dim topDocs As TopDocs = searcher.Search(query, dateFilter, 1000)

我只是不确定如何去做这件事。如何更改它以便我可以执行布尔查询并包含第二个过滤器?

注意:我仍然需要搜索

等字段
  

用户:用户名
  位置:德国
  等

之前我尝试过严格的布尔查询,但我无法使用上述搜索字词。不知道我哪里出错了。

1 个答案:

答案 0 :(得分:0)

好像答案看起来像这样

    ''# Variables used by Lucene
    Dim reader As IndexReader = IndexReader.Open(luceneDirectory, True)
    Dim searcher As IndexSearcher = New IndexSearcher(reader)
    Dim parser As QueryParser = New MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29,
                                                          New String() {"title",
                                                                        "description",
                                                                        "region",
                                                                        "pricelow",
                                                                        "pricehigh",
                                                                        "url",
                                                                        "user",
                                                                        "location"},
                                                                    New StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29))
    Dim query As Query = parser.Parse(search.ToLower)

    Dim nowValue = DateTools.DateToString(DateTime.Now, DateTools.Resolution.DAY)
    Dim dateFilter = If(searchPast, Nothing, FieldCacheRangeFilter.NewStringRange("filterdate",
                                                                                  lowerVal:=nowValue,
                                                                                  includeLower:=True,
                                                                                  upperVal:=Nothing,
                                                                                  includeUpper:=False))


        ''# Here's where the extra filtering magic happens
        ''# First we add the original query to the boolean query
        ''# Then we add another "MUST" query that says that region has to = Subdomain
        Dim bquery = New BooleanQuery
        bquery.Add(query, BooleanClause.Occur.MUST)
        bquery.Add(New TermQuery(New Term("region", HttpContext.Current.Request.Url.Subdomain)), BooleanClause.Occur.MUST)

        Dim topDocs As TopDocs = searcher.Search(bquery, dateFilter, 1000)