Read async API for .NET Client
Describes async methods for read operation
Note
Optimizely Search & Navigation has asynchronous methods that are supported only in the .NET Core version.
You can leverage the benefits of asynchronous programming and performance improvements with the Read and Write async APIs, which are particularly necessary and useful if you are building your app with a microservices architecture or loading data from the FIND service and UI independently for better user experiences.
Get results
GetContentResultAsync()
Perform an asynchronous request of ITypeSearch
and allow caching for your search results. When executed in context of a user without edit or admin access cacheForEditorsAndAdmins
is true the search request is cached for cacheForSeconds
seconds.
//single search
var result = await _searchClient.Search<MyDocument>().For("banana").GetContentResultAsync();
//multiple search
var results = await _searchClient.MultiSearch<MyDocument>()
.Search(s1 => s1.For("banana").InField(x => x.Name))
.Search(s2 => s2.For("strawberry").InField(x => x.SearchTitle()))
.GetContentResultAsync();
- Parameters –
- Required parameters – no required parameter.
- Optional parameters –
cacheForSeconds
the results will be cached in seconds, default is -1 ifcacheForEditorsAndAdmins
is false or 60 seconds ifcacheForEditorsAndAdmins
is true.
- Return value –
- Return type an awaitable
Task<IContentResult<TContentData>>
if you do a single search. - Return an awaitable
Task<IEnumerable<IContentResult<TContentData>>>
if you do a multiple search.
- Return type an awaitable
GetPagesResultAsync()
Perform a non-blocking search by using GetResultAsync
. This will get PageData
type only based on the language (auto detect).
var articalePages = await _searchClient.Search<ArticlePage>().For("alloy").GetPagesResultAsync();
- Parameters – no required parameter.
- Return value – return an awaitable
Task<PagesResult<TPageData>>
whereTPageData
is assignable from PageData.
GetResultAsync()
Perform an non-blocking search request to FIND backend.
var results = await _searchClient.Search<IContent>()
.For("banana")
.GetResultAsync();
- Parameters – no required parameter.
- Return Value –
- Return an awaitable
Task<SearchResults<TResult>>
with normal search. - Return an awaitable
Task<UnifiedSearchResults>
if you do a UnifiedSearch.
- Return an awaitable
GetAsync()
Perform a non-blocking request to retrieve the document(s); this will help the client obtain the documents simultaneously.
- Parameters – Require a document id or an
IEnumerable
of ids. Have some other overloading method. - Return value –
- Returns an awaitable
Task<TSource>
if parameter is a document’s id. - Returns an awaitable
Task<IEnumerable<GetResult<TSource>>>
if parameter is anIEnumerable
of id(s).
- Returns an awaitable
var doc = await _searchClient.GetAsync("your_existing_document_id");
var docs = await _searchClient.GetAsync(new List<int>(){1,2,3});//get documents with id=1,2,3
var mydocs = await _searchClient.GetAsync<MyDocument>(1, 2, 3);//if ClientExtensions is used.
GetWithMetaAsync()
Similar to GetAsync()
, the only difference is this method returns the indexed object wrapped in a GetResult
object. A typical use case for this method is retrieving the object and its version number, which you can use in optimistic concurrency checks (and update objects only if they have not changed because they are being fetched).
DeleteAsync()
– Perform a non-blocking delete document(s) to your index.
- Parameters – Require a document id and type of document.
- Return value – Return an awaitable
Task<DeleteResult>
.
await _searchClient.DeleteAsync<MyDocument>(123);
//or
await _searchClient.DeleteAsync(typeof(MyDocument), 123, null);
Searches
SearchAsync()
Design to perform an asynchronous search request. If your site startup depends on FIND, this could be useful while your page’s areas could load in parallel. This method is used in GetResultAsync()
.
- Parameters – Require an awaitable
SearchRequestBody
object when you call this method directly or useGetResultAsync
when use normal search. - Return value – Return an awaitable
Task<SearchResults<TResult>>
.
var docs1 = await _searchClient.Search<MyDocument>()
.For("banana")
.GetResultAsync();
//or you can build your own query
var criteria = new TermsQuery("tags", new FieldFilterValue[] { "vegetable", "fruits" });
var docs2 = await _searchClient.SearchAsync<MyDocument>(new SearchRequestBody(){Query=criteria}, cmd=> cmd.LanguageRouting = "en");
MultiSearchAsync()
Similar the SearchAsync
this method perform a non-blocking request of multiple searches. This method is used in GetContentResultAsync()
.
- Parameters – Require an
IEnumerable
ofSearchRequestBody
or just call toGetContentResultAsync
. - Return value –
- Returns an awaitable
Task<IEnumerable<SearchResults<TResult>>>
when you call toMultiSearchAsync
directly. - Returns an awaitable
Task<IEnumerable<IContentResult<TContentData>>>
when call toGetContentResultAsync
.
- Returns an awaitable
var docs= await _searchClient.MultiSearch<MyDocument>()
.Search<IContent>(x => x.For("banana").InField(f => f.Name))
.Search<IContent>(x => x.For("apple").InField(f => f.Name))
.GetContentResultAsync();
SearchForTypeAsync()
Like SearchAsync
, this method is used for searching documents of a type.
- Parameters – Require a
SearchRequestBody
. - Return value – Return an awaitable
Task<SearchResults<TResult>>
.
var searchRequest = new SearchRequestBody();
searchRequest.Query = new QueryStringQuery("Bacon");
searchRequest.Highlight.Fields.Add(fieldName);
searchResult= await _searchClient.SearchForTypeAsync<MyDocument>(searchRequest, null);
Statistics
GetAutocompleteAsync()/StatisticsAutocompleteAsync()
Perform a non-blocking request to get autocomplete list by a prefix string. This is used in StatisticsAutocompleteAsync
when using Find.Statistics
extensions.
- Parameters –
- Required parameters – Prefix string parameter is required.
- Optional parameter – Size and Tags are optional parameters.
- Return value – Return an awaitable
Task<AutocompleteResult>
contains list of autocomplete inAutocompleteResult
object.
await _searchClient.Statistics().StatisticsAutocompleteAsync("prefix",size=5);
GetDidYouMeanAsync()/StatisticsDidYouMeanAsync()
Perform a non-blocking request to get list of related query. Use StatisticsDidYouMeanAsync
as the same functionality when using Find.Statistics
extensions.
- Parameters –
- Required parameters – A query string is required.
- Optional parameter –
Size
andTags
are optional parameters.
- Return value – Return an awaitable
Task<DidYouMeanResult>
contains list ofdidyoumean
items inDidYouMeanResult
object.
await _searchClient.Statistics().StatisticsDidYouMeanAsync("fruits",size=3);
GetSpellcheckAsync()/StatisticsSpellcheckAsync()
Perform a non-blocking request to check spelling of a query. Use StatisticsSpellcheckAsync
as the same functionality when using Find.Statistics
extensions.
- Parameters –
- Required parameters – A query string is required.
- Optional parameter –
Size
andTags
are optional parameters.
- Return value – Return an awaitable
Task<SpellcheckResult>
.
await _searchClient.Statistics().StatisticsSpellcheckAsync("banan",size=1);
Updated about 9 hours ago