Extending QPL

From wiki.searchtechnologies.com
Revision as of 17:04, 3 March 2017 by Sdenny (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

For Information on Aspire 3.1 Click Here

This Page is Locked - Content no longer maintained - See QPL for the latest information.

You can extend qpl and/or create new builders. The complexity depends on where you need to call it from.

When you create the QPLShell in Java, you can specify a groovy file that includes the extensions you want. An example from the unit tests:

 public void testCreateExtensionShell() {
   QPLShell extShell = new QPLShell("QPLExtension");
   Object o = extShell.evaluate("boostedField(\"field_name\", \"field_value\");");
   assertTrue(o instanceof Operator);
   Operator op = (Operator) o;
   assertEquals("and(field_name:field(field_value)^10.0,term(field_value))", op.toString());
   
   extShell = new QPLShell("QPLExtension", QPLTestUtils.class);
   o = extShell.evaluate("bracket(\"some_text\");");
   assertTrue(o instanceof String);
   String s = (String) o;
   assertEquals("(some_text)", s);
 }

The Groovy script resides in the resources directory. In the example above it's a file called QPLExtension.groovy:

 abstract class QPLExtension extends QPLCore
 {
   def FIELD = OperatorType.create("FIELD");
 
   def boostedField(String field, String str) {
     f = new Operator(FIELD, field, str, Operator.NO_WINDOW, 10f);
     t = term(str);
     return and(f, t);
   }
 }

The second part of the test shows a Java class being passed as an extension too. The utility class is:

 package com.searchtechnologies.qpl;
 
 public class QPLTestUtils {
   public static final String bracket(String str) {
     return "(" + str + ")";
   }
 } 

If you need to create a builder, again you define the Groovy (too big to put here, but see https://svn.searchtechnologies.com/svn/qpl/trunk/qpl/qpl-fast/src/main/resources/FastBuilder.groovy)

Then when you need to use, load it:

   // Initialise the builder
   debug("Loading FAST builder");
   InputStream inStream = FQLParser.class.getResourceAsStream("/FastBuilder.groovy");
   try {
     QPLBuilder.loadBuilder(QPL_LOADER, FAST_BUILDER_TYPE, "FastBuilder.groovy", inStream);
   } catch (Exception e) {
     error(e, "Exception loading Fast builder");
     throw new AspireException("FASTQueryBuilder.load-builder-exception", e, "Exception loading Fast builder");
   }

And then use it:

     // Get the qpl from the job
     Operator op = (Operator) j.get(jobVariable);
     if (op == null) {
       warn("QPL null or missing from job: %s", j.getJobId());
       return;
     }
     
     // Build the FQL
     QPLBuilder builder = new QPLBuilder(FAST_BUILDER_TYPE);
     String fql = (String) builder.build(op);
     
     debug("QPL->FQL: %s -> %s", op.toString(true), fql);

That builder was only designed to produce text. See here for other information and examples.