The following Java snippet is an example of basic query rewriting,
replacing all occurrences of the word "rain
" by the
word "sun
".
package my.test;
import com.exalead.access.feedapi.AccessException;
import com.exalead.access.feedapi.Feed;
import com.exalead.access.feedapi.FeedTrigger;
import com.exalead.access.feedapi.QueryContext;
import com.exalead.access.feedapi.ResultFeed;
public class SampleTrigger implements FeedTrigger {
public Result beforeQuery(Feed feed, QueryContext context) throws AccessException {
// Fetches the original q parameter
String originalQuery = feed.getEvaluatedParameter(context, "q");
System.out.println("Original query: " + originalQuery);
// Computes the new query
String newQuery = originalQuery.replace("rain", "sun");
System.out.println("New query:" + newQuery);
// Forces the "q" parameter to the new query
feed.overrideParameter(context, "q", newQuery);
return Result.CONTINUE;
}
public Result afterQuery(Feed feed, QueryContext context, ResultFeed resultFeed) throws AccessException {
return Result.CONTINUE;
}
}
// To get your custom code running into your CloudView 360 instance, compile it into
// a .jar file and drop it in the javabin directory of your CloudView 360 kit.
// Finally, to plug your Trigger in the Access.xml configuration file,
// just add a <Trigger> tag to the targeted feed:
<Feed id="bi" enable="true" embed="true" className="com.exalead.access.basefeeds.BusinessItemFeed">
<Trigger className="my.test.SampleTrigger" />
<Parameters>
<Parameter name="searchAPIVersion">5.0</Parameter>
<Parameter name="searchapi">{access-api.searchapi.url}/search</Parameter>
<Parameter name="q">${page.params["q"]}</Parameter>
<Parameter name="type">all</Parameter>
<Parameter name="page">1</Parameter>
<Parameter name="defaultQuery">all</Parameter>
<Parameter name="per_page">10</Parameter>
</Parameters>
</Feed>