Try our conversational search powered by Generative AI!

Geta.Tags facet

Vote:
 

Hi,

I'm not sure if this is the right part of the forum, if not, sorry about it.

Im using Geta.Tags on my project, as a categorylist, and I'm trying to use it as a facet. It works, although, my tags is printed for each page, not for each tag. Example:

Restaurantpage1-tags: Fish, pizza, pasta

Restaurantpage2-tags: Fish, pizza

And the output is 

Tags:
Fish,pizza,pasta

Fish,pizza

I would like it to be:

Fish (2)

Pizza (2)

Pasta (1).

I've tried to split it but I can't get it to work.. any ideas?

    public class SearchResultPageController : PageController
    {
        public ActionResult Index(SearchResultPage currentPage, string query, string tags, string cities)
        {
            if (query == null && tags == null && cities == null)
            {
                return View();
            }

            var q = SearchClient.Instance.Search()
                .For(query)
                .TermsFacetFor(x => x.Tags)
                .TermsFacetFor(x => x.RestaurantCity);


            if (!string.IsNullOrWhiteSpace(tags))
            {
                q = q.Filter(x => x.Tags.Match(tags));
            }
            if (!string.IsNullOrWhiteSpace(cities))
            {
                q = q.Filter(x => x.RestaurantCity.MatchCaseInsensitive(cities));
            }

           var results = q.Select(x => new SearchHit
            {
                Title = x.HeadLine,
                Url = x.LinkURL,
                Tag = x.Tags,
                Adress = x.Adress,
                Latitude = x.Latitude,
                Longitude = x.Longitude,
                Image = x.RestaurantImage,
                City = x.RestaurantCity 

            }).GetResult();


            ViewBag.Query = query;
            ViewBag.Id = results.ProcessingInfo.ServerDuration;
            ViewBag.Hits = results.TotalMatching;

            ////Lägg till facets till resultat
            var facets = new List();
            var tagFacet = (TermsFacet) results.Facets["Tags"];
            var tagLinks = new FacetResult("Tags", tagFacet.Select(x => new FacetLink
            {
                Text = x.Term,
                Count = x.Count,
                Url =
                    Url.QueryBuilder(currentPage.ContentLink)
                        .AddSegment("?query=" + query + "&tags=" + x.Term)
                        .ToString()
            }));
            facets.Add(tagLinks);

            var cityFacet = (TermsFacet) results.Facets["RestaurantCity"];
            var cityLinks = new FacetResult("Cities", cityFacet.Terms.Select(x => new FacetLink
            {
                Text = x.Term,
                Count = x.Count,
                Url =
                    Url.QueryBuilder(currentPage.ContentLink)
                        .AddSegment("?query=" + query + "&cities=" + x.Term)
                        .ToString()
            }));
            facets.Add(cityLinks);

            //Ta bort facets från resultat
            ViewBag.Filters = new List();
            if (!string.IsNullOrEmpty(tags))
            {
                ViewBag.Filters.Add(new FacetLink
                {
                    Text = tags,
                    Url = Url.QueryBuilder(currentPage.ContentLink).AddSegment("?query=" + query).ToString()
                });
            }

            if (!string.IsNullOrEmpty(cities))
            {
                ViewBag.Filters.Add(new FacetLink
                {
                    Text = cities,
                    Url = Url.QueryBuilder(currentPage.ContentLink).AddSegment("?query=" + query).ToString()
                });
            }

            return View(new SearchResult(results, query) {Facets = facets});
}
#138929
Sep 23, 2015 19:39
Vote:
 

I suppose you could use .TermsFacetForWordsIn() instead of .TermsFacetFor(). If not, check out this thread

#138935
Sep 23, 2015 20:09
Vote:
 

So the Tags value are stored comma separated. What you could also do is create a separate property that returns a collection/array instead (basically return Tags.Split(',');).

Hope this helps

Frederik

#138936
Sep 23, 2015 20:25
Vote:
 

Yeah, that's exactly what is being done in the thread I linked to :-)

#138940
Sep 23, 2015 21:15
Vote:
 

Edit:

Got it to work with SearchClient.Instance.Conventions.ForInstancesOf<RestaurantPage>().IncludeField(x => x.Tags.Split(','));  in my InitializationModule!

Although I've noticed that when I'm clicking my links, I only get one result with the tag that've clicked, and the tags that only have one tag doesn't show any result. Any ideas? 

Example:

Drinks(2) - And when I click it, it only shows one page, not both with the tag.

Fish(1) - Not showing any results even though there's one page with the tag on it.

#138956
Edited, Sep 24, 2015 8:33
Vote:
 
<pre class="brush:html;auto-links:false;toolbar:false" contenteditable="false"></pre> <p>Also, I'm curious if you do have a better way then the way I'm linking to the filteringen. My facetlinks looks like this:</p> <pre class="brush:html;auto-links:false;toolbar:false" contenteditable="false"></pre> <pre class="brush:html;auto-links:false;toolbar:false;highlight:Url.QueryBilder" contenteditable="false"> var facets = new List&lt;FacetResult&gt;(); var foodFacet = (TermsFacet)results.Facets["FoodCategory.Split"]; var foodLinks = new FacetResult("Cuisines", foodFacet.Select(x =&gt; new FacetLink { Text = x.Term, Count = x.Count, Url = Url.QueryBuilder(currentPage.ContentLink).AddSegment("?query=" + query + "&amp;cuisines=" + x.Term + "&amp;cities=" + cities).ToString() })); facets.Add(foodLinks); </pre> <p><br /><br /></p>
#138961
Sep 24, 2015 9:53
Vote:
 
<p>I would just use the Toggle&nbsp;method of a&nbsp;QueryStringBuilder created from the Request.RawUrl. That way you will persist all other querystrings and you will also be able to remove the facet by clicking the link one more time:<br /><br /></p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">Url.QueryBuilder(Request.RawUrl) .Toggle("cuisines", x.Term) .ToString()</pre> <p>If you don't want the toggle functionality you can just do like this:</p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false">Url.QueryBuilder(Request.RawUrl) .Remove("cuisines") .Add("cuisines", x.Term) .ToString()</pre> <p></p>
#138966
Sep 24, 2015 11:15
Vote:
 

Thank you Mattias.

It seems like when I'm filtering, the .Split is skipping the first value in the array, since I get 3 results when the facetscount says 4, and 0 when it says 1, any tips how to fix this? I'm new to both EPiServer and Find/Facets/Tags.. 

#138967
Sep 24, 2015 11:23
* 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.