Class ScriptFilter

  • All Implemented Interfaces:
    IXMLConfigurable, IDocumentFilter, IOnMatchFilter, IImporterHandler

    public class ScriptFilter
    extends AbstractStringFilter

    Filter incoming documents using a scripting language. The default script engine is JavaScript.

    Refer to ScriptRunner for more information on using a scripting language with Norconex Importer.

    How to filter documents with scripting:

    The following are variables made available to your script for each document:

    • reference: Document unique reference as a string.
    • content: Document content, as a string (of maxReadSize length).
    • metadata: Document metadata as a Properties object.
    • parsed: Whether the document was already parsed, as a boolean.
    • sectionIndex: Content section index if it had to be split, as an integer.

    The expected return value from your script is a boolean indicating whether the document was matched or not.

    XML configuration usage:

    
    <handler
        class="com.norconex.importer.handler.filter.impl.ScriptFilter"
        maxReadSize="(max characters to read at once)"
        sourceCharset="(character encoding)"
        onMatch="[include|exclude]"
        engineName="(script engine name)">
      <!-- multiple "restrictTo" tags allowed (only one needs to match) -->
      <restrictTo>
        <fieldMatcher>(field-matching expression)</fieldMatcher>
        <valueMatcher>(value-matching expression)</valueMatcher>
      </restrictTo>
      <script>(your script)</script>
    </handler>

    Usage example:

    JavaScript:
    
    <handler
        class="ScriptFilter">
      <script>
        <![CDATA[
         var isAppleDoc = metadata.getString('fruit') == 'apple'
                 || content.indexOf('Apple') > -1;
         /&#42;return&#42;/ isAppleDoc;
       ]]>
      </script>
    </handler>
    Lua:
    
    <handler
        class="ScriptFilter"
        engineName="lua">
      <script>
        <![CDATA[
         local isAppleDoc = metadata:getString('fruit') == 'apple'
                 and content:find('Apple') ~= nil;
         return isAppleDoc;
       ]]>
      </script>
    </handler>
    Since:
    2.4.0
    Author:
    Pascal Essiembre
    See Also:
    ScriptRunner