简体   繁体   English

Lucene.net / C#/添加过滤器查询

[英]Lucene.net / c# / add filter query

I am in doubt of how to make a query in lucene.net using BooleanQuery and TermQuery etc. I want it to act in the same way as this sql statement: 我不确定如何使用BooleanQuery和TermQuery等在lucene.net中进行查询。我希望它以与此sql语句相同的方式起作用:

"... WHERE ((isprivate = false) OR (isprivate = true and userid = 1))"

Can anyone help me with this. 谁能帮我这个。

Untill now I only have 直到现在我只有

BooleanQuery booleanQuery = new BooleanQuery();
TermQuery isPrivateQuery = new TermQuery(new Term("isprivate", "true"));

Your query can be rewritten as: 您的查询可以重写为:

"... WHERE (isprivate = false) OR (userid = 1)"

So you can have the next code (considering that your values are stored as string): 因此,您可以使用下一个代码(考虑您的值存储为字符串):

BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.Add(new TermQuery(new Term("isprivate", "true")), BooleanClause.Occur.SHOULD);
booleanQuery.Add(new TermQuery(new Term("userid", "1")), BooleanClause.Occur.SHOULD);

If you want to make more complex queries, then you can add one BooleanQuery as a clause to another BooleanQuery: 如果要进行更复杂的查询,则可以将一个BooleanQuery作为子句添加到另一个BooleanQuery中:

// Creating "and" clause
BooleanQuery booleanQueryInner = new BooleanQuery();
booleanQueryInner.Add(new TermQuery(new Term("isprivate", "false")), BooleanClause.Occur.MUST);
booleanQueryInner.Add(new TermQuery(new Term("userid", "1")), BooleanClause.Occur.MUST);

// Creating "or" clause
BooleanQuery booleanQueryMain = new BooleanQuery();
booleanQueryMain.Add(new TermQuery(new Term("isprivate", "true")), BooleanClause.Occur.SHOULD);
booleanQueryMain.Add(booleanQueryInner, BooleanClause.Occur.SHOULD);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM