Faceted Search
# help-with-umbraco
t
We are building a faceted search for a client, but there's a couple of features I'm having trouble finding documentation on: 1. How do we return counts for the other (non-selected) facets? 2. How do we deal with facets that are filtering multi node tree picker values? 3. This is a multilingual site, and I'm having trouble finding specific information on targeting a specific culture's content
j
1. Best way I found was to simply do 2 searches - one without any filters for the "unselected" facets, and then one with the filters. 2. You can hook into the TransformingIndexValues event and add a new index field with whatever info you'd like to be in the facet instead of the UDI from the node. (Be aware you may need to do something else to ensure the value is kept up to date: https://dev.to/jemayn/the-problem-of-referenced-content-in-examine-indexing-umbraco-11-4355) 3. Not sure what the problem is? The fields will be appended with a culture string, so you can access them by
$"fieldName_{cultureString.ToLowerInvariant()}"
. The culture is accessible from the currentpage
t
1. That's exactly my plan. 2. I'll look into that, thank you. 3. The issue was that this isn't documented. I noticed this when inspecting the external index, but wasn't sure on best practices - whether there's a helper method to generate the field name, or helper methods in the query itself to specify a culture, etc. From your answer, it sounds like I will need to manually generate the field name based on the current culture, and that's fine - I just wish that was documented somewhere.
j
Good point on the lack of documentation for variant content - the best way to remedy that would be to raise an issue with the documentation team so they are aware 🙂 https://github.com/umbraco/UmbracoDocs/issues Or second best.. Best way would be to raise a PR with an explanation 😉
t
For #1 though, I think we'll actually need to perform several searches. For example, imagine the first facet is a color, and we have red, green and blue options. The 2nd facet is a size, and we have small, medium and large. Now, the user has selected green and medium options. Now, I'll need to perform the following searches: the base search (if keyword is entered), a search for each combination of color:green and each size (to get the counts for each size option), a search for each combination of size:medium and each color (to get the counts for each color option), and finally the search for color:green and size:medium (may not be necessary, but since the previous searches are just counts, we want to get the results). This results in 1 + 3 + 3 + 1 searches, which if you scale this to a dozen facets with a dozen options each, can get out of hand quickly.
I wonder if it'd be more performant to do the base search, get everything into memory, and calculate counts directly on the in-memory results.
j
I think you are misunderstanding the way the facets work. They give you a count based on the total search results, so if you do a "blank" search you will get all possible results in a big list with all facets and their total counts. Then you can do your actual search with whatever filters are selected, and at this point you will get the filtered subset of all results with its corresponding facet values
t
Are you saying there's built-in facet functionality somewhere? If I do the "blank" search, where will the count for all of the red items be?
j
Sorry I may have jumped to some conclutions about your setup, are you doing facetted search with Examine running the Examine v4 beta version containing facets?
t
Note that the count of red items based on my example above (with user selecting green & medium) should be items that are both red and medium (i.e. based on other non-color selections)
No - I started down that path, but I noticed that the version is still in beta from 2023, and I read elsewhere (in this Discord) that multilingual support isn't quite there.
j
Oh ok, well basic Examine doesn't have facet functionality, so you'd be better off not doing it with Examine at all, and instead use some other search service with facet functionality
The beta works pretty well though, am using it on several Umbraco 13 sites also with multicultural setups - just beware there is a breaking change in 13.6+ that is not yet resolved in the package
t
I get what you're saying, but I think with your help, I still have enough to go on. Wish me luck!
j
Good luck 🙂 With the beta package you can do something like this:
Copy code
csharp
nativeQuery = index.Searcher.CreateQuery().NativeQuery(rawQuery);

foreach (var productSearchFilter in filtersFromDb)
{
    nativeQuery.WithFacets(x =>
        x.FacetString(
            IndexingHelper.GetIndexFieldName(
                $"SearchFilter_{productSearchFilter.FilterAlias}_Value",
                culture
            ),
            c => c.MaxCount(200)
        )
    );
}

var searchResults = nativeQuery.Execute(
    new QueryOptions(0, productRequest.PageSize * (productRequest.CurrentPage))
);
And then the searchResults contain the regular searchresults but also contain a list of facet results with a filtername and count value so its super easy to get facet results based on a standard search query
4 Views