Use filters to narrow down search results, or use them for database-like queries.
Use the `Filter
` method to filter _string fields_ in several ways. Below are use cases and examples illustrating the methods.
## Existence
To search for documents where a string field has a value, use the Exists method. The following search finds blog posts that have a title. The following code is similar to the LINQ query `Where(x => x.Title != null)
`.
Like all filters, it can be negated using an exclamation point (!). For example, to find all blog posts that lack a title, use the following code.
## Exact match
For exact matching, use the `Match
` method. The following search matches blog posts titled _Find_ but not those titled _find,_ _fInd_, _Find rocks!_ or _Hello Find_. The LINQ equivalent is **Where(x => x.Title == Find)**.
## Case-insensitive match
For exact matching without regard to case, use the `MatchCaseInsensitive
` method. The following search matches blog posts titled _Find_, _find_ and _fInd_ but not those titled _Find rocks!_ or _Hello Find_. The following search is similar to the LINQ query `Where(x => x.Title.ToLower() == Find.ToLower())
`.
## Match by beginning of a string (startsWith)
The `Prefix
` method lets you match by the beginning of a string. The following search matches blog posts titled _Find_ and _Find rocks!_ but not _find_, _Finding_ or _Hello Find_.
Use the `PrefixCaseInsensitive
` method to match by the beginning of a string in a case-insensitive way. The following search matches blog posts titled _Find_, _Find rocks!_ and _Find_ but not _Finding_ or _Hello Find_.
## Match by beginning of any word (wildcard, autocomplete)
In some situations, such as when building autocomplete or search-as-you-type functionality, you want to filter on whether any word in a string starts with a certain string. Achieve this with the `AnyWordBeginsWith
` method.
The above query matches blog posts titled _Banana_, _banana_,_Banana split_ or _Yellow bananas_. It does not match blog posts titled _Abandon,_ because no word in that title starts with _ban_ (although it contains those characters).
Note
While the `
Match
` and `Prefix
` methods are case-sensitive and have case-insensitive counterparts, the `AnyWordBeginsWith
` method is not case-sensitive and has no case-sensitive counterpart.
## Match by range
The `InRange
` method lets you filter on string fields that match a specific range. For instance, the following search matches blog posts titled _A_, _Aa_, and _B_ but not those titled _Bb_ or _C_.
## Match by a set of values
The In method filters on string fields that match an explicit list of values. For instance, the following search matches blog entries titled _A_, _B_ and _C_.
## Filter on string collections
You also can use the `Exists
`, `Match
`, `In
`, and `MatchCaseInsensitive
` methods for properties that implement `IEnumerable<string>
`. The following search uses _Exists_ and matches blog posts that have at least one tag.
The following search matches any blog post that has the tag **Find**. It does not limit the results to only blog posts tagged with Find.