Gravatar for alexander.kravchuk@apiqu.com

Question by Alexander Kay, Nov 12, 2017 11:19 PM

Linq Query Multivalue Field for Multiple Matches

Hey folks,

I'm using Coveo 4.1 Cloud (October release) and Sitecore 8.1 (rev. 160302). I've a multivalue computed field:

<fieldType fieldName="CommodityTags" isSortable="true" isMultiValue="true" settingType="Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework" />
public class CommodityTagsComputedField : CommodityComputedField
    {
        #region Overrides of CommodityComputedField
    public override object GetComputedFieldValue(IEnumerable&lt;Item&gt; datasources, Database database)
    {
        object computedFieldValue = null;
        var commodityItem = GetCommodityItem(datasources);
        if (commodityItem != null)
        {
            var commodityItemField = (MultilistField) commodityItem.Fields[CommodityItem.TagsFieldName];
            if (!string.IsNullOrWhiteSpace(commodityItemField?.Value))
                computedFieldValue = string.Join(";", commodityItemField.GetItems().Select(x =&gt; x.Fields[TagItem.NameFieldName].Value));
        }
        return computedFieldValue;
    }


    #endregion
}

The facets and everything works flawless. However, let's say I have an item in sitecore with multiple tags and I want to find related items based on these tags in coveo index. I was trying to implement it in LINQ query but I stuck. I'm looking for something like this, but for multivalue field:

var results = new List<SearchResultItem>();
            using (var context = CoveoExtensions.GetCoveoIndex().CreateSearchContext())
            {
                var queryable = context.GetQueryable<SearchResultItem>()
                    .Where(x => x.TemplateId == BlogPostPageTemplateId ||
                                x.TemplateId == ResourcePageTemplateId)
                    .OrderByDescending(x => x.GetDateFieldValue("commoditypublishdate"))
                    .Take(maximumItems);
                results = queryable.ToList();
            }
            return results;

Does anybody have idea how I can achieve that with LINQ?

Thanks a lot,

Alexander.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Nov 13, 2017 1:25 PM

What have you tried => What worked, what didn't?

A multivalue field is no exception, the query that you posted there should work.

Have you tried a similar query without LINQ, using the `/coveo/rest` endpoint?

Do you have the right `IDs`? They have to be in the same format as in the index. (We use ShortID).

Gravatar for alexander.kravchuk@apiqu.com

Comment by Alexander Kay, Nov 13, 2017 2:07 PM

Hi @François Lachance-Guillemette, I didn't try /coveo/rest directly. I'm not even sure how I suppose to do that, because I should get related items based on the multiple tags. Like many-to-many search.

1 Reply
Gravatar for alexander.kravchuk@apiqu.com

Answer by Alexander Kay, Nov 16, 2017 5:44 AM

It's better to stick with the existing coveo components. So I end up with customized search view resources and search view components. Moreover, I have Coveo query in javascript which does all the work for me.

Ask a question