Copied to clipboard

Flag this post as spam?

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


  • Ben McKean 272 posts 549 karma points
    Aug 17, 2015 @ 12:30
    Ben McKean
    1

    Examine Search with whitespace

    Hi All,

    My examine search seems to be having issues with whitespace. I'm creating a search form of Members. Users should be able to search on node name which could could include a space

            var searcher = ExamineManager.Instance.SearchProviderCollection["InternalMemberSearcher"];
            var searchCriteria = searcher.CreateSearchCriteria();
    
            var query = searchCriteria.Field("nodeName", searchQuery.MultipleCharacterWildcard()).Compile();
            var searchResults = searcher.Search(query);
    

    The above works fine when I search without a space but with a space returns no results.

    For example, I have two members, Tommy Tester and John Doe. "Tommy" and "John" brings back results but "Tommy T", or anything after a space doesn't.

    Thanks

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Aug 17, 2015 @ 14:52
    Ismail Mayat
    0

    Check if your search term has space if it does then split and do or on each item in the query. Without split its doing phrase search.

  • Ben McKean 272 posts 549 karma points
    Aug 17, 2015 @ 15:05
    Ben McKean
    0

    I found a post of yours a while back stating this https://our.umbraco.org/forum/developers/api-questions/26004-Examine-Search-with-whitespace-no-good

    What is the type of your queryToBuild in that example? and what should I then pass into searcher.Search?

    Here is what I have so far which obviously doesn't work!

            var searchCriteria = searcher.CreateSearchCriteria();
    
            if (searchQuery.Contains(" "))
            {
                string[] terms = searchQuery.Split(' ');
    
                foreach (var term in terms)
                {
                    searchCriteria = queryToBuild.Or().Field("nodeName", term); // ??
                }
            }
    
    
            //var query = searchCriteria.Field("nodeName", searchQuery.MultipleCharacterWildcard()).Compile();
            var searchResults = searcher.Search(query);
    
  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Aug 17, 2015 @ 15:27
    Ismail Mayat
    0

    Wildcard the term and on initial criteria creation you may need to set to default or sorry no PC ATM I'm on hols so doing this from phone and old man's memory ;-)

  • Ben McKean 272 posts 549 karma points
    Aug 17, 2015 @ 16:09
    Ben McKean
    104

    I think I've managed to get it working how I want it to. I found a good post here http://sleslie.me/2014/search-umbraco-using-examine-and-razor/ by Scott Leslie which helped me out.

    I've tweaked that, to And conditions. For my purposes this acts as a filter. So no criteria entered - all results are returned and the more the user enters, the result get restricted down.

    Working solution is here :

            var searcher = ExamineManager.Instance.SearchProviderCollection["InternalMemberSearcher"];
            var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.And);
    
            var q_split = searchQuery.Split(' ');
    
            var fieldsToSearch = new[]{"nodeName"};
            IBooleanOperation filter = searchCriteria.GroupedAnd(fieldsToSearch, q_split.First().MultipleCharacterWildcard());
            foreach (var term in q_split.Skip(1))
            {
                filter = filter.And().GroupedAnd(fieldsToSearch, term.MultipleCharacterWildcard());
            }
    
            var searchResults = searcher.Search(filter.Compile());
    
Please Sign in or register to post replies

Write your reply to:

Draft