Support for scored search
BerntA opened this issue · comments
Bernt Andreas Eide commented
Are there any plans to add support for boost, score, etc, to allow us to retrieve ranked search result based on relevancy towards an input search query? (sanity-io/sanity#1444)
Bernt Andreas Eide commented
A viable solution is to just use the SanityClient's FetchAsync to execute a raw query:
*[((_type == "article") || (_type == "recommendation")) && ((((!(_id in path("drafts.**")) && (defined(articleContent) && articleContent != null)) && (defined(slug) && slug != null)) && (defined(slug.current) && slug.current != null)))] | score(-- match on various fields here --) | order(_score desc) {"id":_id,"type":_type,title,...,"score": _score }
public async Task<List<T>?> Search<T>(string query, int from = 0, int to = 20)
{
if (string.IsNullOrEmpty(query)) return null;
var search = await _sanity.Client.FetchAsync<List<T>>($"{query} [{from}..{(to+from) - 1}] [score > 0]");
return search.Result;
}
var result = await Search<SomeObject>(--query here--);