Friday, April 4, 2014

Content Search Web Part “Hello World” using Query Rules

Helge Solheim did a post on how to do “Hello World” using the CSWP, where he retrieves a document with the title of “Hello World”.

Figured I’d do the same, using a Query Rule instead Smile

I first tried to create a rule which triggers on a search term, and then configuring the CBWS to execute that query. But turns out if you do this, the rule will not fire for some reason.

Hence it got a bit more complicated.

 

First off create a new Result Source, and accept all the defaults.

image

Next up I create Query Rule for this new result source “Hello World” with a promoted result. I make it trigger for any term and only on the “Hello World”  result source.image

image

Nest step is setting up the CBS webpart, triggering the rule. On the right hand you see the Special Term Result including the best bet I defined. I have also chosen the “Hello World” result source and no query what so ever.

image

I also configure CBS to return one item only, map Title and URL to be used in the template, and I pick SpecialTermResults as my result table, as this is the table containing promoted results.

image

Result looks something like the image below.

image

3 comments:

  1. Love it! Very creative to do this using a promoted result and not need to index anything!

    ReplyDelete
    Replies
    1. Thanks, but I sort of don't like that I couldn't make the CSWP trigger a rule by modifying the query. Any idea why as it works fine from the query tool?

      Delete
    2. Yes, this is by design, but perhaps not exactly intuitive. The text you enter in the query builder for the CSWP is considered a "Query Template", not "query text", and query rule conditions are not evaluated against this template. The purpose of the query rule mechanisms were to trigger against user input or otherwise varying parameters. So if you put e.g. {searchboxquery} into your query template, the query rule conditions would be evaluated against the expanded version of your template, i.e. the actual end-user query text.

      If you indeed want query rules to trigger on the query template in the CSWP you can define a result source like you did.

      Delete