Multiple searches in one request
Describes how to run several Optimizely Search & Navigation searches in one request, which speeds up your search.
When you run multiple searches in one request, an IEnumerable<SearchResults<T>
is returned. The following code shows a simple multi-search example where the specified search returns only ten records unless you specify the page size for each search with Take()
function in a multi-search.
results = service.MultiSearch<Article>()
.Search<Article>(x => x.For("Banana").InField(y => y.Title)).Skip(0).Take(100)
.Search<Article>(x => x.For("Annanas").InField(y => y.Title)).Skip(0).Take(100)
.GetResult();
The next example queries search and return the Article
type. Alternatively, you can search for and return different types using Optimizely Search & Navigation projections, as illustrated in the following example.
IEnumerable<SearchResults<string>> results = service.MultiSearch<string>()
.Search<Article, string>(x => x.For(indexedArticle.Title).InField(y => y.Title).Select(y => y.Title))
.Search<User, string>(x => x.For(indexedUser.Name).InField(y => y.Name).Select(y => y.Name)).GetResult();
Optimizely Search & Navigation returns results the same way you search for them. So, the first result is related to the first query, and so on. Also, these searches only count as one query against the index.
Multi-unified search
The following code shows a multiple unified search equivalent.
results = service.MultiUnifiedSearch()
.UnifiedSearch(x => x.For("Banana"))
.UnifiedSearch(x => x.For("Annanas"))
.GetResult();
As illustrated in the following example, you can specify your hit specification with customized highlighting for each search.
results = service.MultiUnifiedSearch()
.UnifiedSearch(x => x.For("Banana"), new HitSpecification {
HighlightTitle = true,
HighlightExcerpt = true,
PostTagForAllHighlights = "</strong>",
PreTagForAllHighlights = "<strong>"
})
.UnifiedSearch(x => x.For("Annanas"), new HitSpecification {
HighlightTitle = true,
HighlightExcerpt = true,
PostTagForAllHighlights = "</em>",
PreTagForAllHighlights = "<em>"
})
.GetResult();
Maximum number of searches
For performance reasons, one multi-search request can create up to 10 searches.
Updated 22 days ago