Search code examples
lucene.netbooleanquery

Lucene.Net: How can I convert MultiFiledQueryParser to BooleanQuery?


This is my current code that does a 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)

I'm just not sure how to go about doing this. How can I change it up so that I can do a boolean query and include a second filter?

Note: I still need to be able to search on fields like

user:username
location:germany
etc

I tried a strictly boolean query before and I could not use the above search terms. Not sure where I went wrong.


Solution

  • It appears as though the answer looks like this

        ''# 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)