2012-05-17 53 views
0

我想創建一個查詢Lucene的搜索像下面如何建立Lucene的查詢x和(Y或Z)

詞: 「searched_word」 AND(類別:0或類別:1)

什麼手段searched_word必須在字字段和類別字段爲0或1

字字段是正常字段類型 類別字段是NumericField型

BooleanQuery mainQuery = new BooleanQuery(); 

    Query query1 = new TermQuery(new Term("category", NumericUtils.IntToPrefixCoded(0))); 
    Query query2 = new TermQuery(new Term("category", NumericUtils.IntToPrefixCoded(1)); 

    Query queryTerm= new TermQuery(new Term("word", searchedWord)); 


    mainQuery.Add(queryTerm, BooleanClause.Occur.MUST); 

    BooleanQuery innerOrQuery = new BooleanQuery(); 

    //How do i add them as OR and as AND to mainQuery 
    innerOrQuery.Add(querySingleDirection, BooleanClause.Occur.MUST); 
    innerOrQuery.Add(queryBothDirection, BooleanClause.Occur.MUST); 
    mainQuery.Add(innerOrQuery, BooleanClause.Occur.MUST); 
    TopDocs topDocs = _indexSearcher.Search(mainQuery, Settings.Current.MaximumTopScoreCount); 

回答

0
Query queryTerm = new TermQuery(new Term("word", searchedWord)); 
Query query1 = new TermQuery(new Term("category", NumericUtils.IntToPrefixCoded(0))); 
Query query2 = new TermQuery(new Term("category", NumericUtils.IntToPrefixCoded(1)); 

BooleanQuery innerOrQuery = new BooleanQuery(); 
innerOrQuery.Add(query1, BooleanClause.Occur.SHOULD); 
innerOrQuery.Add(query2, BooleanClause.Occur.SHOULD); 

BooleanQuery mainQuery = new BooleanQuery(); 
mainQuery.Add(queryTerm, BooleanClause.Occur.MUST); 
mainQuery.Add(innerOrQuery, BooleanClause.Occur.MUST); 

TopDocs topDocs = _indexSearcher.Search(mainQuery, Settings.Current.MaximumTopScoreCount); 
+0

謝謝,我不知道我怎麼沒想到這個:) – Yucel

+0

嗨。你可以請看看這篇文章http://stackoverflow.com/questions/16906689/filter-not-working-with-text-values-lucene-3-0-3。瘋了。 – Huzaifa

相關問題