Example of query customization in Coveo for Sitecore?

Depending on certain settings (for example, user locale) I want to search only a few specific fields. For example, if the user is detected to be in the US, I want to search a USBiography field, but if the user is in Europe, I want to search the EuropeBiography field. I believe I have to do this in code, by adding a QRE to search only the specific field(s) for the expression the user entered.

What Iâd like is an example of this, and I havenât been able to find one. The few examples I did find (in the training guides, and here) showed how to add hardcoded expressions to the usersâ query, rather than modifying it to search just a specific field or set of fields. Would you have any short example of how to do this?

While you can choose which fields to return on the results, you cannot specify in which fields the free-text query will look into.

On the other hand, a field query is always done on a single field (e.g: @USBiography="architect"). So depending on the setting/condition, you can just change the field name used for the query to @EuropeBiography. Note that you cannot easily take free-text search input move it to a field query because:

(@field=value) can only contain one word for the value.
(@field="multiple word value") will match documents which contain the exact phrase "multiple word value" in the field.
(@field=multiple AND @field=word AND @field=value) would work but you have to split the words yourself beforehand and this can lead to a lot of problems. "A-Rating" is 2 words, splitting Asiatic languages words...

Additionally, adding such filtering expression will limit the search results to only those who contains the terms in the biography field. So if you have content other than people, it will be filtered out and the search page will be only able to search for people.

In conclusion, I believe there is no solution for your problem at the moment. What you would need would be a way to specify or limit the fields to use for free-text searches or a way to boost documents when the free text search terms were found in certain field values like $FreeTextFieldMatchingRankingExpression(field: @USBiography, modifier: 10). Unfortunately, none of these options exist at the moment. Those would make very nice improvement requests though. You can propose them on our ideas portal.

Simon, unfortunately while the fields are "regional", the content in them will all be in English :(

JF, I did see another post on this board ( https://answers.coveo.com/questions/6925/coveo-field-search ), where you suggested creating a new Sitecore index with only one computed (free text searchable) field...basically a combination of the various Biography fields, in my case. Do you think this would work, in this case?

I do not think this other post solution match your requirements since you cannot specify which Sitecore index to query in Coveo for Sitecore and the solution was for a very particular reporting use case.

Sitecore language version is perfectly suited for your use case. You can have an EN-US and a EN-GB versions of an item. EN-US would have all the fields set and the US biography. EN-GB would have only the bio set to the Europe version. All the other fields would be shared across languages or inheriting the EN-US version with the Language Fallback module.

At indexing time, with the Language Fallback module, each people item will be indexed twice. One EN-US USA version and one EN-GB Europe version.

At query time, depending on the location of your visitor, you just have to include a filter expression where XX is either US or GB:

@_language=="en-XX"

There is already such filter in the default Coveo for Sitecore options. You might have to disable it. And you have to translate the field name to the Coveo format of course.

Based on @jflheureux answer, it sounds like you're not going to find a great answer, but maybe there's a bad answer that could work? Before your components are initialized, you could drop a new field on Querybox.options to take in a field name (via data- attributes) which would be determined server-side based on the user's info. Then you'd monkey patch the handleBuildingQuery on the Querybox, call the original method, and then use data.queryBuilder.expression.remove to kill off the general query and replace it with a query over your specified field. Might be a terrible idea, but it also might work.

Fair enough. What about building it all into a new custom component instead of monkey patching? You could handle buildingQuery, remove the magicBox text from the expression, and then addFieldExpression with the field passed in via options.

This is better but still not perfect. The basic expression of the query box can't be removed because it is needed to match the searched terms in the other free-text searchable fields, body, title and URL of indexed documents. Your solution would create a search page to only search in a biography field, whatever the content type.