Copied to clipboard

Flag this post as spam?

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


  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Mar 27, 2017 @ 12:29
    Bjarne Fyrstenborg
    0

    Search single field using multiple keywords in Examine

    I am trying to search in a single field in Examine if it match any of the values from a comma separated list of values.

    Eg. it might be some regions/areas in Denmark like "midt, syd, vest".

    So for example: string area = "midt,syd"

    At the moment I have the following, but it only return result for first value in the array (when splitted with comma).

    var searcher = ExamineManager.Instance.SearchProviderCollection["OffersSearcher"];
    
    var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    IBooleanOperation query = searchCriteria.NodeTypeAlias("offer");
    
    var fieldsToSearch = new List<string> { "nodeName", "shortDescription", "offerCategory", "offerTypes", "offerProvider", "offerArea" };
    
    if (!string.IsNullOrWhiteSpace(q))
    {
        var keywords = StringHelper.SplitKeyWords(q);
        IBooleanOperation filter = query.And().GroupedOr(fieldsToSearch, keywords.First().MultipleCharacterWildcard());
    
        foreach (var term in keywords.Skip(1))
        {
            filter = filter.Or().GroupedOr(fieldsToSearch, term.MultipleCharacterWildcard());
        }
    
        query = filter;
    }
    
    if (!string.IsNullOrEmpty(area))
    {
        var areaValues = StringHelper.SplitKeyWords(area, ',');
    
        IBooleanOperation filter = query.And().GroupedOr(new string[] { "offerArea" }, areaValues.First());
        foreach (var val in areaValues.Skip(1))
        {
            filter = filter.Or().GroupedOr(new string[] { "offerArea" }, val);
        }
    
        query = filter;
    }
    
    var results = searcher.Search(query.Compile());
    

    It seems the query at the end is __NodeTypeAlias:offer +(offerArea:midt) (offerArea:syd)

    If I in Luke change the query to __NodeTypeAlias:offer +(offerArea:midt offerArea:syd), then I get results for both regions/areas.

    Is there a way to get this output from Examine?

    I am using Umbraco v7.5.11

    /Bjarne

  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 27, 2017 @ 14:16
    Sven Geusens
    0

    I think the problem lies with the way you are using the .GroupedOr().

    I think you need to do filter.GroupedOr(fieldsToSearch,keywords) according to the documentation found here https://github.com/Shazwazza/Examine/wiki/Grouped-Operations#groupedor

    Relevant bit:

    where 'id' or 'parentID' equals 1 or 2.

    Multi field + different number of vals, example:

    criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });

    Outputs

    +(id:1 id:2 id:3 parentID:1 parentID:2 parentID:3)

  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Mar 27, 2017 @ 15:35
    Bjarne Fyrstenborg
    1

    Hi Sven

    Thanks for your suggestions.. I had a closer look at the documentation and have now updated my code to the following, which search in the specified fields on multiple keywords.. and for area search in a single field for multiple values (selected checkbox values).

    var searcher = ExamineManager.Instance.SearchProviderCollection["OffersSearcher"];
    var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    IBooleanOperation query = searchCriteria.NodeTypeAlias("offer");
    
    var fieldsToSearch = new List<string> { "nodeName", "shortDescription", "offerCategory", "offerTypes", "offerProvider", "offerArea" };
    
    if (!string.IsNullOrWhiteSpace(q))
    {
        var keywords = StringHelper.SplitKeyWords(q);
        IBooleanOperation filter = query.And().GroupedOr(fieldsToSearch, keywords.Select(t => t.MultipleCharacterWildcard()).ToArray());
        query = filter;
    }
    if (!string.IsNullOrEmpty(area))
    {
        var areaValues = StringHelper.SplitKeyWords(area, ',');
        IBooleanOperation filter = query.And().GroupedOr(new string[] { "offerArea" }, areaValues.ToArray());
        query = filter;
    }
    
    var results = searcher.Search(query.Compile());
    
  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Mar 27, 2017 @ 16:17
    Bjarne Fyrstenborg
    1

    Do you know if it is possible to combine MultipleCharacterWildcard and Fuzzy?

    For example:

    IBooleanOperation filter = query.And().GroupedOr(fieldsToSearch, keywords.Select(t => t.MultipleCharacterWildcard().Value.Fuzzy()).ToArray());
    

    but it only seems to use fuzzy and a keyword - and not matching start of a word.

    A similar thread here: https://our.umbraco.org/forum/developers/api-questions/37433-MultipleCharacterWildcard-and-Boost-at-the-same-time-Examine

  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 28, 2017 @ 08:02
    Sven Geusens
    0

    I have tried this myself a while ago but I didn't get it to work either.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

    Continue discussion

Please Sign in or register to post replies