0

我正在尝试为 Lucene 创建一个查询以进行如下搜索

word:"searched_word" AND (category:0 OR category:1)

什么意思 searched_word 必须在 word 字段中,并且 category 字段是 0 或 1。

word字段是普通字段类型类别字段是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);
4

1 回答 1

1
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);
于 2012-05-17T17:27:56.053 回答