6

The Sitecore ContentSearch Linq API provides several methods that can be used to search against string fields in different ways:

  • Like
  • Matches
  • MatchWildcard
  • Contains
  • StartsWith/EndsWith

From the doc site I have been able to get a vague sense of the purpose for each and I have successfully used some of these in the past, but I have not found a clear comparison anywhere. When should I use each of these methods? Is there a significant difference in performance between any of them? Is there any notable difference in how they behave with different search providers?

5

This answer is for Lucene, but as Solr is based un Lucene I'm expecting to have very similar result on it

  • Like: This one does a fuzzy search. It gets the top terms most similar to the term you introduced and then gives you the documents containing these terms. The similarity can be adjusted. It is based on the Damerau-Levenshtein (optimal string alignment) algorithm. This is is a heavy operation, but it safe to be executed. It's translated to: fieldname:value~0.5 (The number represent how similar the terms are, from 0 to 1)
  • Matches: It expects a regular expresion instead of a term to match documents. If you use "plain text" it performs like "equals".
  • MatchWildcard: It looks for terms containing the referenced text, then it creates a query for any of the terms found. I discourage this method as it usually triggers a "too many clauses" exception. Let's say it translate each word found in the whole index for that field into o field == word 1 or filed==word2, etc. By default Lucene only support 1024 clauses. It can be modified in the config file, but it will affect performance. By default, I recomend the like search to return more related result and with a safe execution than this one. The query is translated to field:*text*.
  • Contains: It performs a matchwildcard
  • StartsWith/EndsWith: It's like the matchwildcard but only at the beginning or end of the term: The query is translated to field:*value / field:value*As terms are index alphabetically based on each character from start to end, "starts with" performs much better as it can identify the terms easily within the index while "Ends with" forces Lucene to transverse the whole term set to identify those "matching terms".
  • Equals: This is the basic search and is term based. It will return only documents with the whole "word". The query is translated to: field:value
  • If MatchWildcard and SartsWith are not recommended, what would be the best way to search for terms that start with a specific letter? Is Matches a "safe" query? Like seems more intended for spelling correction scenarios. – Ben Golden Dec 2 '16 at 15:14
  • 1
    It's not recommended if your requirements are "implement a search". If your requirement is explicitly "find terms starting with", then you'll have to use it, the key is if you understand the impact in performance. Usually, you can try to reduce the impact on the search by moving the load to the crawling process. As an example, "ends with" is much heavier than "starts with", so you could overload your crawling process and store the terms backwards and then use a starts with during search. – Vicent Galiana Dec 5 '16 at 8:02
  • 1
    I'm not sure about how safe is to use matches as it seems to use the top terms found instead of all of them (Which is safe). but I couldn't confirm it. I tried to replicate a MatchWildcard query wich triggers the maxclausecount exception with a regular expression and it didn't fail, so apparently it's "safe". – Vicent Galiana Dec 5 '16 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.