Gravatar for emil.christiansen@kraftvaerk.com

Question by emil_christiansen, Jun 28, 2016 2:01 AM

Coveo For Sitecore - Omnibox Result List - Only search in one specific field

Hi there!

Is it possible to tweak the "Omnibox Result List" so the suggestions only are based on a single field - e.g. the title of the pages?

So if i enter: "dentist" in the search box - the suggestions that will show up, are only the results where "dentist" are present in the title of the pages?

We have already made some modifications to the SearchBoxView.cshtml file - e.g. so the results only are pages with "haslayout" like this:

Coveo.$('#@Model.SearchBoxUniqueId')
            .on("buildingQuery", function(e, args) {
                args.queryBuilder.constantExpression.add('(@Model.ToCoveoFieldName("haslayout")) == "1" AND (@Model.ToCoveoFieldName("customsite")) == @currentSite');
            })
            .coveoForSitecore('initSearchBox',CoveoForSitecore.componentsOptions);

Could we add something to the queryBuiler above - or am i looking at the wrong place?

As strange as it sounds, the functionality should actually be: "only show suggestions where the search phrase is present in the title of the pages, but if you make the search (e.g. click the search button) and end up on the search result page - it should be a 'normal' search"

We are using the Coveo For Sitecore Free Edition.

Thanks in advance!

1 Reply
Gravatar for sbelzile@coveo.com

Answer by Sébastien Belzile, Jun 28, 2016 8:11 AM

Hi,

Providing an easy way to perform query based on what the user typed is on the roadmap.

  1. You can find what the user typed in the query builder. I don't have a running Sitecore close to tell you exactly where, but if you add a break point in your function, I'm pretty sure you will find the information in the expression of the queryBuilder. If not, try the event "doneBuildingQuery".
  2. Do not add this new query part in the "constantExpression" part of your query builder. Add it to the "expression", or to the "AdvancedExpression" part. Otherwise this will slow down your index with time. (Constant expressions are optimized on the index side. Having a lot of them reduces performance.)
Gravatar for emil.christiansen@kraftvaerk.com

Comment by emil_christiansen, Jun 29, 2016 4:40 AM

Thanks!

Do you know which parameter i should use to only search in a single field?

I have seen a "includeField" parameter somewhere in the documentation - but don't know if that is the right one?

Also, if i should add the new query-part to the expression/advancedexpression - how would that be done using the below code as base?

Coveo.$('#@Model.SearchBoxUniqueId') .on("buildingQuery", function(e, args) {
    args.queryBuilder.constantExpression.add('(@Model.ToCoveoFieldName("haslayout")) == "1" AND (@Model.ToCoveoFieldName("customsite")) == @currentSite');
})
.coveoForSitecore('initSearchBox',CoveoForSitecore.componentsOptions);

Thanks!

Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Jun 29, 2016 8:10 AM

Hi,

The code will look like this:

Coveo.$('#@Model.SearchBoxUniqueId') .on("buildingQuery", function(e, args) {   
    args.queryBuilder.expression.add('(@Model.ToCoveoFieldName("myField"))  == ' + args.queryBuilder.expression.parts[0] ); })
.coveoForSitecore('initSearchBox',CoveoForSitecore.componentsOptions);

Replace "expression" either by advancedExpression or disjunctionExpression or keep it as is. Don't forget null check on parts[0] -> the user does not always type something.

Another way of doing what you are trying to do would be to override the function CoveoForSitecore.populateOmniboxResultList (CoveoForSitecore.js). I think this code is easier to understand:

if (wordToSearch !== '') {
    queryBuilder.expression.add(('(@Model.ToCoveoFieldName("myField"))  == ' + wordToSearch);
}
Gravatar for emil.christiansen@kraftvaerk.com

Comment by emil_christiansen, Jun 30, 2016 9:55 AM

Thanks again!

Maybe a stupid question - but how do i combine the "args.queryBuilder.expression.add" from your example and the "args.queryBuilder.constantExpression.add" from my example - so i get one whole query?

Thanks!

Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Jun 30, 2016 10:00 AM

Don't worry about this. The search API will do it for you: The query that the index will receive in the end will be: (q AND aq AND cq) OR dq

where q: expression, aq: advancedExpression, cq: constantExpression, dq: disjunctionExpression

Ask a question