Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 06, 2017 @ 13:50
    Lewis Smith
    0

    Complex Query for Multiple Tags

    Hi All,

    I'm trying to implement pagination into my website and as it stands this is breaking because of how im formatting my tags.

    Currently I have a textstring where you can enter multiple tags which are comma spaces. (For example: Tag 1, Tag 2, Tag 3, Tag 4)

    These are then broken up and added to a list using the code below:

    string[] tags = item.GetPropertyValue<string>("blogTags").ToString().Split(',');
    List<string> tagList = new List<string>();
    foreach(var tagItem in tags)
    {
        tagList.Add(tagItem);
    }
    if(tagList.Contains(urlTag))
    {
        @blogInner(item);
    }
    

    The variable urlTag is a URL request (code below)

    var urlTag = Request["tag"];
    

    This enables tags to be searched using the URL.

    Basically I need a query that will only select the blog posts that have the tag of the variable urlTag straight off the bat instead having all the blog posts then filtering out like it currently does.

    I had the same issue for the date search but I now have this query. So something like this should work, if its possible of course.

    var dateBlogs = Model.Children().Where(x => x.GetPropertyValue<DateTime>("blogDate").ToString("d-MMM-yyyy").ToLower() == urlDate.ToLower());
    

    Thanks, Lewis

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Sep 06, 2017 @ 16:51
    Alex Skrypnyk
    100

    Hi Lewis

    Will this code work for you?

    var blogTagsBlogs = Model.Children().Where(x => x.GetPropertyValue<string>("blogTags").Contains(Request["tag"]));
    

    Alex

  • Marcio Goularte 388 posts 1361 karma points
    Sep 06, 2017 @ 21:32
    Marcio Goularte
    0

    I recommend using the Examine. I'll share a code I used to make my blog http://www.marciogoularte.com.br

    I created a document type blog and I used renderMvcController to do the search.

    Examine.SearchCriteria.IBooleanOperation filter;
      var Searcher = ExamineManager.Instance.DefaultSearchProvider;
    
      var searchCriteria = Searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
      filter = searchCriteria.NodeTypeAlias("post");
    
    
      foreach (var tag in tags)
      {
          filter = filter.And().Field("tags", tag);
      }
    
      //Pesquisa
      searchResults = Searcher.Search(filter.Not().Field("umbracoNaviHide", "1").Compile()).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.5f);
    
    List<IPublishedContent> posts  = new List<IPublishedContent>();
    
    //if you have pagination
     foreach (var item in searchResults.Skip((viewmodel.Pagination.CurrentPage - 1) * viewmodel.Pagination.PageSize).Take(viewmodel.Pagination.PageSize))
          {
              posts.Add(Umbraco.TypedContent(item.Id));
          }
    
  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 07, 2017 @ 07:51
    Lewis Smith
    1

    Thanks, Alex and Marcio,

    I went for Alex's answer as it was the most simple to implement.

    Thanks, Lewis

Please Sign in or register to post replies

Write your reply to:

Draft