The difference in operation would be due to differences in the search requested by the two sections of code. What does searchKeyword contain? QueryParser may be splitting the string into multiple terms. The code shown for BooleanQuery will only work when searchKeyword is a single term. That is, it will search for the text specified as a single term.
By the way, as the code shown for BooleanQuery only uses a single term, the clause Occur.MUST or Occur.Should is unimportant. For a single term there is really no need for the BooleanQuery object at all, you could instead use the TermQuery object directly in the call to Search() -- Neal -----Original Message----- From: Li Bing [mailto:[email protected]] Sent: Monday, August 24, 2009 1:12 PM To: [email protected] Cc: [email protected] Subject: Re: Query vs. BooleanQuery I tried. But it still doesn't work. Thanks so much! LB On Tue, Aug 25, 2009 at 1:20 AM, [email protected]<[email protected]> wrote: > It is due to : > > booleanQuery.Add(termQuery, BooleanClause.Occur.MUST); > > Change it to > > booleanQuery.Add(termQuery, BooleanClause.Occur.SHOULD); > > and you'll see same results. > > > Li Bing: >> >> Dear all, >> >> (The codes in the previous email have something specific in my >> applications. I made changes in this email) >> >> I tried to search using two approaches, Query and BooleanQuery. To my >> surprise, the two behaves different on the same index? Or I made >> something wrong? >> >> On the same index, Query can get results whereas BooleanQuery get NOTHING. >> Why? >> >> The codes are shown as follows. >> >> Using Query, >> >> ...... >> IndexSearcher searcher = new IndexSearcher(fsDirectory); >> Analyzer chineseAnalyzer = new ChineseAnalyzer(); >> QueryParser queryParser = new QueryParser(searchField, >> chineseAnalyzer); >> Query query = queryParser.Parse(searchKeyword); >> Hits results = searcher.Search(query); >> ...... >> >> Using BooleanQuery, >> >> ...... >> IndexSearcher searcher = new IndexSearcher(fsDirectory); >> Term term = new Term(searchField, searchKeyword); >> TermQuery termQuery = new TermQuery(term); >> BooleanQuery booleanQuery = new BooleanQuery(); >> booleanQuery.Add(termQuery, BooleanClause.Occur.MUST); >> Hits results = searcher.Search(booleanQuery); >> ...... >> >> I think the codes should be correct. But why do they get different >> results on the same index? >> >> Thanks so much for your help! >> LB >> >> >> > >
