Try our conversational search powered by Generative AI!

Using TermFacets to return a list of product namespaces under a node

Vote:
 

Hi,

I need to run a Find Query to return a list of product namespaces which exist under a category node. So when the category loads, it will hit Find to ask what types of products exist as descendants in a multi level catalog. If there are 100 of product type A and 2 of product type B i should get two results with the namespace of each.

If i run this code, i get a list of localised content type names:

            // use Find to execute search with the content type id set as a facet
            var searchQuery = SearchClient.Instance.Search<BaseProduct>()
                .Filter(x =>
                    x.Ancestors().Match(currentContent.ContentLink.ToReferenceWithoutVersion().ToString()))
                        .Take(0).TermsFacetFor(x => x.ContentTypeName()).GetContentResult();

            // extract the content type id's form the result
            var terms = searchQuery.TermsFacetFor(x => x.ContentTypeName()).Terms;

However the localized content type name is not exactly what i need to i updated my code as follows which i hoped would include the Common Type namespaces:

            var searchQuery = SearchClient.Instance.Search<BaseProduct>()
                .Filter(x =>
                    x.Ancestors().Match(currentContent.ContentLink.ToReferenceWithoutVersion().ToString()))
                        .Take(0).TermsFacetFor(x => x.CommonType()).GetContentResult();

            // extract the content type id's form the result
            var terms = searchQuery.TermsFacetFor(x => x.CommonType()).Terms;

However on trying to facet the common type, i always get a null result.

The type is stored in the Find index so there must be a way of returning it as a facet? Any advice would be much appreciated?

Thanks,

Johnny

#200926
Jan 30, 2019 20:46
Vote:
 

Hi Johnny,

With these below extensions, the indexed field names are not the same as extension/property names:

I do not understand deeply why Episerver did like that. But if you want to reuse these available indexed fields for your purpose then you should change code like this:

var searchQuery = SearchClient.Instance.Search<BaseProduct>()
                .Filter(x =>
                    x.Ancestors().Match(currentContent.ContentLink.ToReferenceWithoutVersion().ToString()))
                        .Take(0).TermsFacetFor(x => x.CommonType(), request => request.Field = "_Type").GetContentResult();

            // extract the content type id's form the result
            var terms = searchQuery.TermsFacetFor(x => x.CommonType()).Terms;

One more note, the CommonType is full content type name within namespace name. If you just want to query facet for content type name without namespace name, you can use query as following as:

var searchQuery = SearchClient.Instance.Search<BaseProduct>()
                .Filter(x =>
                    x.Ancestors().Match(currentContent.ContentLink.ToReferenceWithoutVersion().ToString()))
                        .Take(0).TermsFacetFor(x => x.CommonTypeShortName(), request => request.Field = "_TypeShortNamme").GetContentResult();

            // extract the content type id's form the result
            var terms = searchQuery.TermsFacetFor(x => x.CommonTypeShortName()).Terms;
#200932
Edited, Jan 31, 2019 5:07
Vote:
 

That is excellent Binh. Done the trick perfectly. Once again thank you for your help. Much appreciated!

#200943
Jan 31, 2019 11:20
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.