Gravatar for

Question by helen, Mar 2, 2016 2:14 AM

ConstantExpression partial match

HI Team,

In ConstantExpression condition, how to add a partial match ? e.g. args.queryBuilder.constantExpression.add('@(Model.ToCoveoFieldName("parentpath")) == "/sitecore/content/home/news"');

Regards, Helen

2 Replies
Gravatar for

Answer by Jean-François L'Heureux, Mar 2, 2016 10:41 AM

Hi Helen,

As @mlaporte suggested, your initial problem is that you are using the "==" (equal) operator instead of the "=" (contains keyword) operator.

This query would return all the descendants of the "news" item but will not give you the best performance due to the "contains" operator:


Secondly, there is more performant alternatives than matching a Sitecore item starts path to get the result you want. The best way is to use the "_path" Sitecore field which is a multi-value field that contains the ID of the item and the IDs of its parents. Let's say your "/sitecore/content/home/news" item has the ID aea9e38c-38a2-4d86-922b-53ee1dd8b52e.

This query would return the "news" item and all of its descendants.


To exclude the "news" item itself, you can add an exclusion like this. There's an implicit AND between the 2 field expressions.

@(Model.ToCoveoFieldName("_path"))=="aea9e38c38a24d86922b53ee1dd8b52e" @(Model.ToCoveoFieldName("_id"))<>"aea9e38c38a24d86922b53ee1dd8b52e"

Lastly, I want to warn you on using the constantExpression for your filters.

The constant expression is a special expression of a query that should never change in time. It should be very static. Each time you change a constant expression by modifying your code, the expression gets added to a cache in Coveo Enterprise Search. This cache is never cleared and contains all the constant expressions the index received since its creation. Each time an index transaction is committed (after a rebuild, after an item creation/modification/delete in Sitecore, after a publish…), all the constant expressions in the cache are queried to refresh their results for performance. If the index have too many constant expressions in its cache, it will cause performance issues at each transaction commit. Thus, using the constant expressions with a lot of care in your code.

I suggest you to add those filters in the advancedExpression instead. This expression is also hidden to the end user but not added in a cache. It is the place where to add search page filters.

I hope this answers your question.


Gravatar for

Comment by helen, Mar 3, 2016 6:21 AM

Hi Jeff, Many thanks for your clarification and it works like a champ! May I ask how to find this kind of details on developers portal

Regards, Helen

Gravatar for

Answer by Martin Laporte, Mar 2, 2016 2:50 AM

Have a look at the $some query extension:$some

Gravatar for

Comment by helen, Mar 2, 2016 3:43 AM

HI Mlaporte,

Thanks! May I ask if $some only works on search results instead of building the query? Any sample to share? Can we apply the partial match directly on query? args.queryBuilder.constantExpression.add('@(Model.ToCoveoFieldName("parentpath")) == "/sitecore/content/home/news"');

Regards, Helen

Gravatar for

Comment by Martin Laporte, Mar 2, 2016 3:55 AM

I'm not sure I understand your question. $some will filter the query results to include only those matching the specified % of keywords (or an absolute number, depending on what arguments you pass). The keywords in question are the one you pass to the extension. Ex:

$some(keywords: foo bar, best: 50%)

Note that you can't apply this logic when matching keywords inside a field (@field=foo) -- it only work for free text searches across document bodies.

Gravatar for

Comment by helen, Mar 2, 2016 5:34 AM

HI Malporte,

Thanks! In my case, we need to find out all the items under a sitecore folder, which mean its parent path starts with "/sitecore/content/home/news". Therefore I need to create a query to filter the it a condition. The query here only return child items but I need all the descendants. args.queryBuilder.constantExpression.add('@(Model.ToCoveoFieldName("parentpath")) == "/sitecore/content/home/news"');

Regards, Helen

Gravatar for

Comment by Martin Laporte, Mar 2, 2016 6:41 AM

OK. Well this has nothing to do with partial match aka the feature that allows matching a subset of the query keywords. I kinda misunderstood your initial question.

In your case the fix is very simple: replace the == by a =, this way you will match all items for which the field contains the value instead of being equal to it.

Ask a question