Query Types

Below is a quick overview of the query types available for use within lucinq. Pretty much all of them can be used in the Setup() method or the constructor taking the Action parameters, such as.

queryBuilder.Setup
    (
        x => x.WildCard(BBCFields.Description, "a*"),
        x => x.Sort(BBCFields.Sortable)
    );

// or in the constructor
IQueryBuilder queryBuilder = new QueryBuilder(
        x => x.WildCard(BBCFields.Description, "a*"),
        x => x.Sort(BBCFields.Sortable));

Term()

Term queries are one of the simplest query types. It is a direct match.

queryBuilder.Term(BBCFields.Title, "africa");
// or not
queryBuilder.Term(BBCFields.Title, "africa", Matches.Never);

Terms()

Terms is an easy syntax for an Or() on a single field with multiple terms.

queryBuilder.Terms(BBCFields.Title, new[] {"europe", "africa"}, Matches.Sometimes);

WildCard()

Wildcard queries in lucene are just as you would expect, they look for the given string within the field specified. The wildcard value should contain the ‘*’ character wherever you need it to be.

queryBuilder.WildCard(BBCFields.Description, "a*");

Phrase()

Phrase queries look for terms that are no more than. The ‘slop’ value (entered as the ‘2’ in the method call) gives the distance apart that the terms can occur.

queryBuilder.Phrase(2).AddTerm(BBCFields.Title, "wildlife").AddTerm(BBCFields.Title, "africa");

// or more verbose
PhraseQuery phrase = queryBuilder.Phrase(2);
phrase.AddTerm(BBCFields.Title, "wildlife");
phrase.AddTerm(BBCFields.Title, "africa");

Group()

Group generates a BooleanQuery object to allow the grouping of multiple queries, the defaultChildOccur parameter allows you to set what the default occurance is for the child queries.

And()

And() generates a BooleanQuery object to allow the grouping of multiple queries with a default setup meaning that all child queries must be present. And queries return the parent query builder.

Or()

Or() generates a BooleanQuery object to allow the grouping of multiple queries with a default setup meaning that all child queries should be present. Or returns the parent query builder.

NumericRange

Fuzzy()

Fuzzy queries attempt to look for terms that are a close approximation of what is entered.

queryBuilder.Fuzzy(BBCFields.Title, "afric");

What Next?

Now you have querying nailed, you might want to consider Sorting your data.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s