Gravatar for adam@borsi.ca

Question by Adam, Jul 23, 2015 11:14 AM

Running a Query on page load then ignoring it while user searches

Greetings,

I was wondering if it were possible to run a query on page load and then have that query be ignored once the user starts to use the search?

I am aware of the Hidden search filter under Scope, but this seems to apply to all searches.

Essentially I want to have featured results show first and then allow the user to search freely.

Can I mix-n-match .NET and JS? … because I suspect the easiest would be to pass something via JS.

Implementation Specs: CES7, Sitecore 6.5, .NET/ASP implementation.

Any and all help is appreciated.

Thank you kindly,

Adam

3 Replies
Gravatar for lbergeron@coveo.com

Answer by Luc Bergeron, Jul 23, 2015 11:42 AM

Hi Adam,

I am not aware of anything that does exactly what you want. But you should be able to do it with some custom code. I think you can do it this way:

  1. Inherit a custom search control (from Coveo.CES.Web.Search.Controls.SearchControl).
  2. Override the OnSetupSearchBuilder method. You can modify the search expressions as you want in there.
  3. Detect whether the user performed some search. You probably want to check the SearchBuilder.BasicExpression for this. It will contain the search terms entered by the user in the search box.
  4. When applicable, apply your hardcoded search terms using the AdvancedExpression. It is better than the BasicExpression, because you probably do not want the hardcoded terms to show up in the query box. It would also conflict with the detection.

It may be tricky to remove the hardcoded terms from the AdvancedExpression once the user starts to search though. You will need to exclude you hardcoded terms somehow. Unfortunately, the QueryExpression class does not expose the list of expressions that were added to it.

I hope this will help you get started.

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 3:15 PM

Thank you lbergeron for your advice. I'll give both a try.

I may have to ping you for further help/clarification.

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 3:45 PM

Hi lbergeron,

Could just be the time of day, but I'm a little unclear where I'd hijack the usual process and inset my custom coded one. Any insight?

Thank you kindly,

Adam

Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Jul 23, 2015 4:42 PM

The code snippet was too long to be posted as a comment. See my new answer.

Gravatar for slangevin@coveo.com

Answer by Simon, Jul 23, 2015 12:14 PM

Alternatively, if you want to do it without coding, you could use a simple Top Result based on @sysuri, which is the query sent by default on page load. https://onlinehelp.coveo.com/en/ces/7.0/administrator/managingtopresults.htm

I do prefer the flexibility of lbergeron's answer, but if you are to modify the "recommended results" often, you might prefer to work directly in the Coveo Admin Tools.

Cheers,
Simon

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 3:23 PM

Thank you Simon for your answer. Gonna give both a try, but I took a look through the system and I don't understand how I can do top results on a blank query i.e. page load. I have it set to "always execute query".

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 3:29 PM

Sorry… I figrued it out… I think. @sysuri is the default state query.

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 4:56 PM

Quick one Simon,

So when using Top Results is there a way to clear the Top Results when only using the facets? I've noticed that if I don't enter a search term/keyword and only use the facets to filter the default data set I still get the Top Results displaying even though they're not relevant to the filtered data in the facets.

Just comes off a little misleading when the starred items aren't relevant to the filters applied.

Thank you kindly,

Adam

Gravatar for slangevin@coveo.com

Comment by Simon, Jul 24, 2015 7:36 AM

Hi Adam, I guess you found the limitation of my solution. clicking on facet will add an advanced query, not a basic one. Top results are based on basic queries so indeed, if you only use a facet, the top result will stick.

So I guess lbergeron's solution would be the way to go.

Gravatar for lbergeron@coveo.com

Answer by Luc Bergeron, Jul 23, 2015 4:41 PM

Finally, it was easier than I first thought. To make things simple, I simply added a script block at the end of the CoveoSearch.ascx file of the skin. I recommend to create your own class, then make the ASCX file inherit from it.

Here is the method override.

<script runat="server">
    protected override void OnSetupSearchBuilder(SetupSearchBuilderEventArgs p_Args)
    {
        base.OnSetupSearchBuilder(p_Args);

        if (p_Args.Builder.BasicExpression.Empty || p_Args.Builder.BasicExpression.ToString() == "@uri") {
            p_Args.Builder.AdvancedExpression.AddExpression("test");
        }
    }
</script>
  1. It overrides the OnSetupSearchBuilder method.
  2. When the BasicExpression is empty, you'll want to put your own expression. I also added the @uri expression, because you may not want to return all the documents when searching for it.
  3. My default expression is "test", but you can put any expression you want there.

I was worried about removing the hardcoded expression, but the search builder instance is reset on every search. So the term disappear automatically.

Let me know how it goes.

UPDATE: In the code snippet, the "test" expression is added to the AdvancedExpression instead of the BasicExpression. Using the BasicExpression was simply more handy to see the query being executed in the CES Console.

Gravatar for adam@borsi.ca

Comment by Adam, Jul 23, 2015 4:46 PM

Awesome, thank you.

I'll give this a go.

Ask a question