Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

c# - How to query for terms IN a collection using Lucene.Net, similar to SQL's IN operator?

We are trying to search whether documents have a particular field value in a collection of possible values,

field:[value1, value2, value3, ..., valueN]

which would return the element if it matches any of the input values, similar to SQL's IN() operator.

This would be similar to a range query, but the elements do not necessarily describe a range.

An example using Lucene.Net API would be,

var query = new QueryParser(version, "FieldName", analyzer).In("value1", "value2", "value3");

Is this possible in Lucene.Net?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

field:value1 field:value2 .... should do the trick. By default all terms are ORed.

Programmatically, you can try,

public static Query In(string fieldName, IEnumerable<string> values)
{
    var query = new BooleanQuery();
    foreach (var val in values)
    {
        query.Add(new TermQuery(new Lucene.Net.Index.Term(fieldName, val)), BooleanClause.Occur.SHOULD);
    }
    return query;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...