The analyzer is being applied through the query parser, but not in the term query (term queries don't use analyzers). I don't know what the chinese analyzer does, but in the case of many analyzers if applied also at indexing this may result in terms being indexed which are not the same as the original word, and therefore TermQuery will not necessarily retrieve a result.
For example in English if you use snowball stemming in an analyzer, then the word "government" will be indexed as "govern". A term query on "government" will then fail. Frequently therefore to search successfully you must use QueryParser with the same Analyzer. I see this problem a lot with first-time users. Index a single document containing only a few words, then use Luke http://www.getopt.org/luke/ to see how the terms have actually been indexed by your index code. In my own usage of Lucene I almost always use a MultiFieldAnalyzer to do the indexing so that I can make sure some fields (usually keyword fields where the exact word is important) are indexed without a "clever" analyzer, and other fields (usually text fields) are indexed with a "clever" analyzer. I'm not sure how/whether you can pass a single term through an analyzer and then turn it into a term query somehow. Yours, Moray ------------------------------------- Moray McConnachie Director of IT +44 1865 261 600 Oxford Analytica http://www.oxan.com -----Original Message----- From: Li Bing [mailto:[email protected]] Sent: 25 August 2009 02:56 To: Granroth, Neal V. Cc: [email protected] Subject: Re: Query vs. BooleanQuery Dear Neal, I appreciate so much for your help! Even though I updated the code as follows, there are still no results for the term-based search. ...... IndexSearcher searcher = new IndexSearcher(fsDirectory); Term term = new Term(searchField, searchKeyword); TermQuery termQuery = new TermQuery(term); Hits results = searcher.Search(termQuery); ...... However, the Query search got results. The searchKeyword is just an ordinary word, no any special characters, since I just want to learn how to use Term to use. Any particular requirements to generate the index? I can figure out the problems. Thanks so much! LB On Tue, Aug 25, 2009 at 2:37 AM, Granroth, Neal V.<[email protected]> wrote: > > 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 >>> >>> >>> >> >> >
