https://discord.umbraco.com logo
#help-with-umbraco
Search for multiple fields
# help-with-umbraco
p

pascaleugster

01/29/2024, 6:04 AM
Hey everyone 🙂 I've wanted to ask if there is a possibility to search for multiple fields with the Query Builder. We've did it in the past with examine Raw Queries but it seems not to work with the query builder itself. This is what my code currently looks like
Copy code
C#
        private IEnumerable<Glossary> DoIndexSearch(string query, int page, int pageSize, IIndex index,
            out long totalResults)
        {
            var results = index.Searcher
                .CreateQuery(IndexTypes.Content)
                .NodeTypeAlias(Glossary.ModelTypeAlias);
            
            if (!query.IsNullOrWhiteSpace())
            {
                var queryFields = new[] { "nodeName", "teaser" };
                results = results.And()
                    .GroupedOr(queryFields, query.MultipleCharacterWildcard());
            }

            var searchResults = results.Execute();

            if (searchResults == null) return DefaultSearchResult(out totalResults);

            totalResults = searchResults.TotalItemCount;
            var pagedResults = GetSearchResults(page, pageSize, searchResults);

            return pagedResults;
        }
I've found a problem with my code. It seems that in the index there is only a "teaser_de-ch" field because its a multilingual page. Does anyone know how i can make this search based on which language is activated?
d

d_inventor

01/29/2024, 7:11 AM
Hi there! We fix this by explicitly passing the current culture to the method. In an API request, we let the frontend send the current culture and in MVC requests, we grab the culture from
IVariationContextAccessor
. Then it's only a matter of appending the culture to the fieldname.
p

pascaleugster

01/29/2024, 7:19 AM
You're my hero! Ill give it a try and let you know 🙂 Thanks @d_inventor 🙂
3 Views