Using QPL in Java
For Information on Aspire 3.1 Click Here
The following code loads a QPL script file and executes it, returning a QPL Operator tree.
import groovy.lang.Script; QPLShell qplShell = new QPLShell(); Script groovyScript = qplShell.parse(new File("MyScript.qpl")); Operator queryRoot = (Operator) groovyScript.run();
- You can reuse the same qplShell to parse many scripts.
- You can reuse the groovyScript as well
- But only a single thread can use the groovyScript at the same time.
- Therefore, you may wish to consider creating a pool of groovyScript's.
Executing a Builder
Once you have an operator tree, you'll want to use a Builder to convert it to a search engine query of some sort.
Initializing the Builder
Builders first require an initialization step which loads the builder and assigns it to a "builder type".
This version loads the builder based on a stream:
QPLBuilder.loadBuilder("lucene", "LuceneBuilder.groovy", this.getClass().getResourceAsStream("/LuceneBuilder.groovy"));
You can also load it using just a file name:
During initialization, the builder attaches methods to every OperatorType which describe how that OperatorType is to be built.
Multiple builders of different types (for example, "lucene", "fast", "google", etc.) can be loaded at the same time.
Running the Builder
You will need to construct a QPLBuilder object to run the builder.
QPLBuilder builder = new QPLBuilder("lucene"); Query q = (Query)builder.build(queryRoot);
Note that the argument to build() above will be the QPL operator tree which was returned by the original QPL script.
The output of the builder will be an object which is appropriate to the destination search engine. In the case of Lucene, this will be a Lucene Query object which can be returned directly from a Solr QParser. For FAST, it will be a String which holds FAST FQL.