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
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?!
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);
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...
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....
interesting...
you've given me something to think about over the weekend!
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'
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...
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:
in my index i have two documents:
and the above search is returning:
no joy... on the advice of another post, i've also tried this:
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:
where as the above is return:
i just can't find how to do it in examine or lucene?!
Hi Jake
There is an overload for TypedSearch, that allows you to set 'useWildCards' to false: (default is true)
so would you be able to have:
??
regards
marc
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:
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...
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' - https://skrift.io/articles/archive/testing-the-performance-of-querying-umbraco/
For complex examine queries, I'd use ExamineManager SearchProvider to return an ISearchResults set...
regards
Marc
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....
interesting...
you've given me something to think about over the weekend!
have a good one,
cheers,
jake
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'
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
http://blogs.perl.org/users/mark_leighton_fisher/2012/01/stupid-lucene-tricks-exact-match-starts-with-ends-with.html
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 forNameExactMatch:"^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 searchNameExactMatch:"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?!
what i've ended up doing is having 4 fields:
Name - for fuzzy searching
ExactMatchName - this is the name value wrapped in the
lucenematch
textStartsWithName - this is the name value starting with the
^
characterEndsWithName - this is the name value ending with the
$
characterthis seems to cover all searching styles (so far) - happy days!
Thanks Jake - I just used this to fix a bug with exact matches that I couldn't get working! 🎉🎉🎉
Yes!
is working on a reply...