Copied to clipboard

Flag this post as spam?

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

  • jake williamson 203 posts 845 karma points
    Oct 01, 2018 @ 18:49
    jake williamson

    is it possible to return a single document exact match using examine?

    i'm having a complete brain melt moment so if the answer to this question is so obvious it's painful please forgive me...

    so i've got a search set up and i want to get an exact match. at the moment i have this:

    var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"];
    var searchCriteria = searcher.CreateSearchCriteria();
    var query = searchCriteria.Field("Name", "ribs");
    var compiledQuery = query.Compile();
    var searchResults = _umbracoWrapper.TypedSearch(0, 0, out totalRecords, compiledQuery, searcher);

    in my index i have two documents:

    name: ribs digital ltd
    name: ribs

    and the above search is returning:

    ribs digital ltd

    no joy... on the advice of another post, i've also tried this:

    var query = searchCriteria.GroupedAnd(new List<string> { "Name" }, "ribs");

    but again, no joy - it still returns two records...

    how do i get it to return the absolute match?!

    in my head the sql equivalent would be:

    select * from MyTable where Name = 'ribs'

    where as the above is return:

    select * from MyTable where Name like '%ribs%'

    i just can't find how to do it in examine or lucene?!

  • Marc Goodson 1775 posts 11810 karma points MVP 7x c-trib
    Oct 01, 2018 @ 20:48
    Marc Goodson

    Hi Jake

    There is an overload for TypedSearch, that allows you to set 'useWildCards' to false: (default is true)

    public IEnumerable<IPublishedContent> TypedSearch(int skip, int take, out int totalRecords, string term, bool useWildCards = true, string searchProvider = null)

    so would you be able to have:

    var searchResults = _umbracoWrapper.TypedSearch(0, 0, out totalRecords, "ribs", false, searcher);




  • jake williamson 203 posts 845 karma points
    Oct 03, 2018 @ 18:34
    jake williamson

    hi marc,

    sorry for the late reply... i thought it had posted but when i checked back here a draft button popped up at the bottom of the screen!

    so, i see where you are going... and i'd attempted the same...

    the pain is that need to pass my compiled query in as there are other items that comprise the search...

    there doesn't seem to be an overload where i can pass a compiled query along with a search and set wildcards to false?

    i had a dig in the umbraco source and it seems you can do this:

    ISearchResults Search(string searchText, bool useWildcards)

    which makes me think i could call the search but that gets put through 'ConvertSearchResultToPublishedContent' which requires the content cache to be passed to it...

    sorry, just reread that and it's a bit of a soup!

    the long and short appears to be that i can't achieve what i want to do using the methods in the umbraco helper...

    which means i can do it using the search but then i need to figure out how the hell to turn the return into IPublishedContent!

    this is turning out to be trickier than i thought...

  • Marc Goodson 1775 posts 11810 karma points MVP 7x c-trib
    Oct 05, 2018 @ 11:34
    Marc Goodson

    Hi Jake

    Yep, but main thing is your question is answered, eg why is it matching everything... it's because of useWildCards setting set to true by default!

    Why so hung up on using Umbraco.TypedSearch?

    Usually when listing search results you don't need ALL the properties of an IPublishedContent object to list out the results?

    Also it is 'slower' -

    For complex examine queries, I'd use ExamineManager SearchProvider to return an ISearchResults set...



  • jake williamson 203 posts 845 karma points
    Oct 05, 2018 @ 14:54
    jake williamson

    hi marc,

    this is indeed true, the main question is answered for which i thank you ;)

    funny you mention being hung up on using 'Umbraco.TypedSearch'...

    simple answer is convience! i'd not really looked into the speed aspect as i made the assumption that it'd be quicker than querying the content tree which based on my testing it is, but from reading the article you provided on the skrift site the performance may not be as good as i thought it was...

    my search class is for a particular type of doctype which returns either single or multiple IPublishedContent nodes which i then run through ditto to turn them into strongly typed models for use in the site.

    from what you are suggesting, i'd be better off getting a set of 'SearchResult' items that i would them map to a strongly typed model?

    i wonder if there's a ditto equivalent out there that'll handle the mapping....


    you've given me something to think about over the weekend!

    have a good one,



  • Marc Goodson 1775 posts 11810 karma points MVP 7x c-trib
    Oct 07, 2018 @ 23:12
    Marc Goodson

    Thinking wildcards or not though, search for "ribs" will always match "ribs digital ltd"...

    What you could do is use a gathering nodes event to add an extra field to the index, called perhaps exactMatchNodeName and populated it with the nodeName prefixed by a particular character and ended by another eg:

    ^ + nodeName + $

    so in your example your exactMatchNodeName would contain ^ribs$ ^ribs digital ltd$

    .. then when searching for an exact match you could prefix and end your search term with the same characters eg

    a search for "ribs"

    would be a search for "^ribs$"

    which would only bring back the single 'exact match'

  • jake williamson 203 posts 845 karma points
    Oct 08, 2018 @ 10:13
    jake williamson

    hi mark,

    interestingly, we'd come to the same conclusion ;)

    i had a really good play with this over the weekend and this post gave me the clue

    however... the weird thing was that it's not working as i'd expect it to... i'd created the NameExactMatch field and the name was going into the index as ^ribs$ and ^ribs digital ltd$ but when i searched for NameExactMatch:"^ribs$" both records were coming back...

    i read and reread the article and the bit i'd missed is that it's suggestions are for three things: an exact match, starts with and an ends with. the search i was doing was matching the starts with and ends with combined which is why i got the two records back...

    the exact match suggestion is that the field needs to be indexed as lucenematch NameValue lucenematch. now when i search NameExactMatch:"lucenematch ribs lucenematch" i get the exact match back ;)

    i guess in my head i thought that combining the starts with and ends with solutions would result in an exact match but that's not the case...

    so i think that's problem solved?!

  • jake williamson 203 posts 845 karma points
    Oct 08, 2018 @ 10:27
    jake williamson

    what i've ended up doing is having 4 fields:

    Name - for fuzzy searching

    ExactMatchName - this is the name value wrapped in the lucenematch text

    StartsWithName - this is the name value starting with the ^ character

    EndsWithName - this is the name value ending with the $ character

    this seems to cover all searching styles (so far) - happy days!

  • Maff 141 posts 465 karma points
    Sep 06, 2021 @ 12:31

    Thanks Jake - I just used this to fix a bug with exact matches that I couldn't get working! 🎉🎉🎉

  • Marc Goodson 1775 posts 11810 karma points MVP 7x c-trib
    Oct 08, 2018 @ 17:57
    Marc Goodson


Please Sign in or register to post replies

Write your reply to: