Gravatar for jason.wilkerson@rbaconsulting.com

Question by jason_wilkerson, Jul 21, 2016 6:20 PM

Custom field won't display in results

I have the following two computed fields configured in my index:

<field fieldName="articleimageurl" sourceField="Thumbnail Image">Coveo.SearchProvider.ComputedFields.ImageUrlComputedField, Coveo.SearchProviderBase</field>
<field fieldName="articleimagealt" sourceField="Thumbnail Image">Coveo.SearchProvider.ComputedFields.ImageAltComputedField, Coveo.SearchProviderBase</field>

If I look at the index in the coveo admin console, the fields are indeed indexed with the proper values. However, when I try to access those fields with the below code, nothing is output.

<img src='{{=raw.@(Model.ToCoveoFieldName("articleimageurl", false))}}' />

If I set a breakpoint and examine the Model in the razor view, the value is in the Model.SearchResults[x].Fields collection.

alt text

But my markup is rendered as <img src />.

Thoughts? Jason

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jul 22, 2016 11:10 AM

That is very weird.

You mention you are debugging the Razor code. However, at this time, only the @(Model.ToCoveoFieldName("articleimageurl", false)) code is run on the server side. This code is simply outputting the right field name in the JavaScript Underscore JS template.

I don't get where you did inspect the fields and their values. Can you give me more context on your UI code here?

Also, when you inspect the field values in the index, from which source are these documents? Master/Web? Can it be that your fields are only indexed in the master source but you are querying against the web source while debugging?

Gravatar for jason.wilkerson@rbaconsulting.com

Comment by jason_wilkerson, Jul 22, 2016 11:44 AM

I inspected the coveomasterindex and I'm running against master.

We have a custom search view with the following markup:

@if (Model.IsConfigured) {
    <script type="text/javascript" src="/Coveo/js/cultures/@(Model.CultureName).js"></script>
    <script type="text/javascript">
        if (!String.format) {
            String.format = function(format) {
                var args = Array.prototype.slice.call(arguments, 1);
                return format.replace(/{(\d+)}/g, function(match, number) {
                    return typeof args[number] != 'undefined'
                      ? args[number]
                      : match
                    ;
                });
            };
        }

        Coveo.$(function() {
            CoveoForSitecore.componentsOptions = @(Html.Raw(Model.GetJavaScriptInitializationOptions()));

            Coveo.$('#search')
                .on("buildingQuery", function(e, args) {
                    var searchExp = String.format('@Html.Action("UserMetadataAccess", "Common")', '@(Model.ToCoveoFieldName("brands"))' , '@(Model.ToCoveoFieldName("departments"))', '@(Model.ToCoveoFieldName("markets"))', '@(Model.ToCoveoFieldName("countries"))', '@(Model.ToCoveoFieldName("vehicle_centers"))') +
                        '(@(Model.ToCoveoFieldName("templatename")) == "Communication Page"' +
                        ' OR @(Model.ToCoveoFieldName("templatename")) == "Policy Page"' +
                        ' OR @(Model.ToCoveoFieldName("templatename")) == "Training Page")' +
                        ' AND (@(Model.ToCoveoFieldName("displayname")) <> "__Standard Values")' +
                        ' AND (@(Model.ToCoveoFieldName("expiration date")) >= today)';

                    //console.log('searchExp: ' + searchExp);
                    args.queryBuilder.constantExpression.add(searchExp);

                    args.queryBuilder.constantExpression.add("$qre(expression:'@(Model.ToCoveoFieldName("boostvalue")) > 1', modifier:'100')");
                }).coveoForSitecore('init', CoveoForSitecore.componentsOptions);

            $('.coveo-filter-clear').click(function () {
                Coveo.$('.CoveoFacet').coveo('reset');
                Coveo.$("#search").coveo("executeQuery");
            });

        });
    </script>

    @Html.Partial("../Common/_SearchResultView", Model)
}

I set a breakpoint at that last line, rendering the Partial and inspected that Model property. That's where I found that the value I need is actually in the result set.

I also replaced articleimageurl with another valid property, like "Title" and got the output I expected (a broken image), so I know my markup around the ToCoveoFieldName is proper.

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jul 22, 2016 2:54 PM

The Model object you are inspecting is a .Net object that contains the search page component configuration options. It is not the results of a query to the index.

The query is really made on the client side in the browser and then each result is rendered using your result list component Underscore JS template.

You should do all your field values inspection in the browser developer tools, not in Visual Studio backend code.

1 Reply
Gravatar for jason.wilkerson@rbaconsulting.com

Answer by jason_wilkerson, Jul 22, 2016 3:38 PM

Nevermind. I had two content items that were nearly identical and had modified the wrong one.

facepalm.

Ask a question