Goal
I want to run a query with one set of parameters, and if it yields no results, re-run it with a slightly different set of parameters. How do I do that?
Environment
Fusion
The strategy will work with all versions of Fusion, but the specific code may be slightly different.
Guide
A common use case is running a query with very selective parameters, and if it yields no results, rerun it with more permissive parameters. A couple of examples would be:
- Run a query against a small set of high-quality content fields, but expand to a larger set of fields if needed.
- Start with "mm" at 100%, but relax it to 50% if needed.
The two most common approaches to this situation are:
- Handle it in the UI or application code that generates the request to Fusion.
- Utilize the Fusion query pipeline to handle the fallback behavior.
Some things to consider when choosing the strategy are:
- Frequency of changing the logic, and workflow of making such a change.
- Complexity of the logic. For example, does the fallback behavior depend on data that is only available to the UI/app code?
- Way in which UX metrics are gathered.
Since this is a Fusion How-To, we'll only discuss the approach of using the Fusion query pipeline to do the fallback. The specific use case we'll evaluate is one where we want to change which fields are queried against (i.e. the 'qf' parameter).
The full pipeline (with the 2 solution stages highlighted) is:
The stages from "Solr Query" and above handle the logic for the initial query. In our case, the important thing would be setting the initial set of fields correctly in "Query Fields", and then sending the query to Solr with "Solr Query".
The two additional stages are:
- ExpandQueryFieldsIfNoResults (an "Additional Query Parameter" stage with appropriate Condition set)
- SolrQueryAfterNoResults(a Solr Query stage with the same appropriate Condition set)
The ExpandQueryFieldsIfNoResults stage would look like:
- The Condition triggers this stage only if the Solr response from the previous "Solr Query" returns no results. For clarity, the Javascript code used here is:
response && response.get() && response.get().getInnerResponse().getNumFound().get() == 0
- A 'qf' is "append" ed to the query to include an additional field to query against. For a different use case, it might be "replace" ing 'mm' with a different value.
- A special 'resubmit' parameter with a unique value is also added. This isn't truly necessary, but it would ensure that the Fusion response signals could be filtered based on whether they were a resubmission or not.
The SolrQueryAfterNoResults stage will look identical to the "Solr Query", with one change of including the same "Condition" Javascript that was used in the ExpandQueryFieldsIfNoResults.
Comments
0 comments
Article is closed for comments.