You typically use the `For
` method to search for phrases or keywords in indexed documents. For instance, to search for blog posts that contain _car_, use the code in the following examples.
Typically, a search query is fetched from user input.
## Search fields
By default, a search query using the `For
` method is performed against a field named _\_all_. The field is generated when an object is indexed, and it is made up of all of the document's fields combined.
Search queries should specify fields using the `InField
`, `AndInField
` and `InFields
` methods. These methods expect a lambda expression, which retrieves the field name. For example, this code searches the **Title** field of blog posts.
To also search the **Content **field, add a second `InField
` method.
Alternatively, you can use the `InFields
` method or the `AndInField
` method. Their usage is demonstrated below.
If one or more fields are specified using methods described above, the search is no longer performed against the **All **field. However, you can explicitly request the search to use the InAllField method.
## Stem words
Stemming is the process of reducing a word to its root form. When using stemming in free text search, words with similar meanings, such as _car_ and _cars_, match.
Stemming is language-dependent, so you need to tell the search engine the language in which you are searching. To do this, pass an instance of the `Language
` class to the Clients `Search
` method. Instances of the `Language
` class that match all supported languages are available as static properties on the `Language
` class.
Note
Search with the **All** field cannot use stemming. This means that, for the language parameter to have any effect, you must specify search fields (such as `
InField
`) using methods described above. Below is a sample search request for _cars_that matches blog posts titled \_car_ or _A blue car_.
Often, you want to search a number of known fields with stemming but also match text that is not in those fields. Although the **All **field does not support stemming, you can still search it when using stemming. So, for example, to search the **Title **and **Content **fields for blog posts and still match blog posts that do not contain the word _cars_ in their Title or `Content
` fields but do contain the word in another field (such as tags), you can use the following code.
## Use AND as operator in multi-word queries
You can configure the free text search to use "AND" as operator instead of the default "OR", to match all search terms minus stop words in multi-word queries. For example, searching for "houses in Stockholm" may return many irrelevant results for "houses".
To avoid this, you can use `WithAndAsDefaultOperator()
` after a method that returns `IQueriedSearch<T>
`, such as `For()
`.
This will only return results matching both "houses" and "Stockholm".