1

I'm creating a condition for the segment builder inheriting from TypedQueryableStringOperatorCondition.

I'm overriding the method GetResultPredicate to apply my filter like this

return base.GetCompareExpression((IndexedContact c) => c["contact.myfield"], "myvalue");

This is translated into the following query:

contact.myfield:myvalue

So good so far, but if the value I'm looking for has more than one word or pipe to split terms it adds double quotes to the query:

return base.GetCompareExpression((IndexedContact c) => c["contact.myfield"], "my value");

This is translated into the following query:

contact.myfield:"my value"

In my case the query I'm trying to get is:

contact.myfield:my value

To get result containing "my" or "value" I have tried using the Operator contains, but it has been translated into a massive spanNear([SpanWildcardQuery.

Any idea how to achieve the query:

contact.myfield:my value

Trying to make it easier or clearer what I'm really trying to do is to find contacts containing any of the "words" in that field. It's fine If I have to change the approach.

  • What's wrong with contact.myfield:"my value"? To me it looks like the more correct syntax. – Dmytro Shevchenko Nov 29 '16 at 12:49
  • 1
    I'm trying to find any of the 2 terms, with the double quotes it becomes one single term. As my index field is tokenized, it doesn't find. If i don't save it tokenized and I store "my red value" as a single term, It wouldn't find "my value" neither. As I mentioned before I tried the "contains", but it didn't work neither – Vicent Galiana Nov 29 '16 at 12:52
  • What is the condition that you have set in the rule for this action? – Richard Seal Nov 29 '16 at 13:02
  • It's a custom condition. – Vicent Galiana Nov 29 '16 at 13:12
  • Sorry, I mean the comparison operator - like Equals etc.... – Richard Seal Nov 29 '16 at 13:13
1

What you need is an "or"-based expression that has a separate condition for every search term:

using Sitecore.ContentSearch.Linq.Utilities;

// ...

IEnumerable<string> keywords = new string[] { "my", "value" };

Expression<Func<IndexedContact, bool>> keywordsPredicate =
    keywords.Aggregate(
        PredicateBuilder.False<IndexedContact>(),
            (current, keyword) => current.Or(
                c => base.GetCompareExpression(c["contact.myfield"], keyword)));

return keywordsPredicate;

Please note that GetCompareExpression() may return different operators. It's up to you what to do when the operator is not "equals".

  • 1
    I think the problem is that the operator here is set by the rules engine, not hard coded. The GetResultPredicate ultimately works off the selected StringConditionOperator to work out what the operator is. – Richard Seal Nov 29 '16 at 13:02
  • @RichardSeal Thanks, I updated the answer to get the operator properly. – Dmytro Shevchenko Nov 29 '16 at 13:03
  • I'm not allowing users to select the operator as this is my logic and requirement I'm hard coding it. Using the predicate builder did the trick, thanks. – Vicent Galiana Nov 29 '16 at 14: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.