Skip to content

Query Types

cardinal252 edited this page Sep 25, 2013 · 12 revisions

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.

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.

NumericRange

Fuzzy()

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

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