You may need to customize the way queries are processed. This section focuses only on the processing of queries, once sent to the Search API. Customization points are possible, especially when using the Mashup API and the Mashup UI.
Note: For more information, see "Configuring Search Queries" in the Exalead CloudView Configuration Guide.
This section shows the overall query processing workflow as well as a detailed view of the query processing runner.
Figure 1. Query Processing Workflow
The following schema summarizes the workflow at the Query Processing Runner level.
Figure 2. Focus on Query Processing Runner
Add Custom Query Processors or Prefix Handlers
To customize your query, you may add either custom query processors or custom prefix handlers at specific stages of the user query processing.
Custom query processors apply to the whole AST. Custom prefix handlers apply to a subpart of the query, for example, date or text.
Where Can You Plug Them?
In the figure below, the icon indicates where you can plug custom processors:
Write Custom Query Processors
A query processor is the simplest component that you can write to customize query processing. It takes the whole QueryContext as input, and allows you to modify the context (including the AST in its current state).
In the sample below, a custom query processor refines the query by adding access restrictions.
This query processor looks into the context (HTML parameter) and finds the profile value given by our custom UI. Depending on that value, the query processor expands the query with a specific prefix handler (perimeter) and the corresponding restricting value.
You can plug this custom module at globalPreParse or preParse processors level (before syntactic parsing, that is to say AST creation.)
@CVComponentDescription("(Sample) Profile based query rewritting") public class ProfileBasedRestrictionQueryProcessor extends CustomQueryProcessor { public ProfileBasedRestrictionQueryProcessor(CVComponentConfig config) throws QueryProcessingException { super(config); }
@Override public void onInit(QueryProcessorContext logic) { System.out.println("Connection to the profile DataBase onInit"); }
@Override public void onDeinit(boolean allInstances) { System.out.println("Close the connection from the profile DataBase onDeinit"); }
public boolean hasRestrictedAccess(String profileId){ if (profileId.equals("admin")){ return false; } return true; }
@Override public void process(QueryContext context) throws QueryProcessingException { String profile = context.query.parameters.getParameterValue("profile"); if(profile != null){ if ( hasRestrictedAccess(profile) ){ context.currentNode = new RootNode(context.currentNode.accept(new MyQueryRewriter())); } } ; }
static class MyQueryRewriter extends NodeVisitor {
@Override public Node visit(UserQueryString queryString) { // add a prefix handler to restrict usage queryString.value += " perimeter:restrictive"; return queryString; } } }
Write Custom Prefix Handlers
You can write your own prefix handlers to customize query processing.
It takes the prefix handler value as input, and allows you to modify the context (including the AST in its current state).
3. Apply prefix handlers to your Exalead CloudView configuration. See "The different types of prefix handlers" in the Exalead CloudView Configuration Guide.
Sample Custom Prefix Handler
The Exalead CloudView default behavior is to use the AND operator between words. In this sample, you use a prefix handler to change it.
You can replace the default AND operator by custom operators (XOR, OR, AND, or FUZZYAND) using a custom prefix handler.
A FUZZYAND operator uses a ratio of minimum terms to match a document:
• FUZZYAND/d with d being a positive or negative number:
◦ FUZZYAND/2 means at least two of these words.
◦ FUZZYAND/-1 means all words but one can be missed.
• FUZZYAND (without suffix). In that case, the prefix handler asks for a minimum of half words.
@PropertyLabel(value = "Change Behavior") @CVComponentConfigClass(configClass=ChangeDefaultBehaviorPrefixHandlerConfig.class) @CVComponentDescription("(Sample) This prefix handler allows to change the default behavior by using another operator rather than the default one.") public class ChangeDefaultBehaviorPrefixHandler extends CustomPrefixHandler implements CVComponent {
private String operator = null;
public ChangeDefaultBehaviorPrefixHandler(ChangeDefaultBehaviorPrefixHandlerConfig config) { super(config); operator = config.getOperator(); }
public void addChildren(NaryNode newNode, String[] queryParts) {