Copied to clipboard

Flag this post as spam?

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


  • Mark 255 posts 612 karma points
    Nov 27, 2013 @ 17:45
    Mark
    0

    Examine Search Confusion

    I have this code snippet that is working fine:

    ISearchCriteria query;

    query = searcher.CreateSearchCriteria(BooleanOperation.Or)
    .Field("IsProtected", "false")
    .Or()
    .GroupedAnd(new string[] { "IsProtected", "AccessRoles" }, new string[] { "true", "Staff" })
    .And()
    .GroupedOr(new string[] { "pageTitle", "pageHeading", "pageIntroduction", "bodyText" }, Query)
    .Compile();

    But I need to dynamically add more than one GroupedAnd method outside of the fluent "one liner". I.e:

    ISearchCriteria query;

    query = searcher.CreateSearchCriteria(BooleanOperation.Or)
    .Field("IsProtected", "false")
    .Or()
    .GroupedAnd(new string[] { "IsProtected", "AccessRoles" }, new string[] { "true", "Staff" });

    query.Or()
    .GroupedAnd(new string[] { "IsProtected", "AccessRoles" }, new string[] { "true", "Partner" });

    query.And()
    .GroupedOr(new string[] { "pageTitle", "pageHeading", "pageIntroduction", "bodyText" }, Query)
    .Compile();

    I know this isn't going to work, but hopefully you get what I'm trying to do. Do I need to do something with IQuery? I can't find any code examples on how to deal with spreading the generation of the query across more than one fluent "one liner".

  • Mark 255 posts 612 karma points
    Nov 27, 2013 @ 21:34
    Mark
    100

    Solved it. Turns out I needed to use the IBooleanOperation type to build the query, e.g:

    var searcher = ExamineManager.Instance.SearchProviderCollection["SiteSearchContentSearcher"];

    IBooleanOperation query = searcher.CreateSearchCriteria(BooleanOperation.Or).Field("IsProtected", "false");

    Then append to that query, e.g:

    foreach(string role in Roles.GetRolesForUser())
    {
    query = query.Or().GroupedAnd(new string[] { "IsProtected", "AccessRoles" }, new string[] { "true", role });
    }

    Then, finally, do the search, e.g:

    var results = searcher.Search(query.Compile());

    In case this helps someone else... Or me if I forget this in another 6 months... :-)

Please Sign in or register to post replies

Write your reply to:

Draft