Class VirtualSearchFieldMapper

java.lang.Object
org.dlese.dpc.index.VirtualSearchFieldMapper

public class VirtualSearchFieldMapper extends Object
Maps virtual search field/term pairs to Lucene Queries. For example, the field/term pair cats:cougar might be mapped to the Lucene Query (cougar OR puma OR "mountain lion"). To define the mappings, the API can be given one or more XML files that contain <virtualSearchField> elements that contain XML queries parsed by the XMLQueryParser class. See sample XML file . Mappings can also be added or removed directly using the API. The API returns Queries for the given field/term pairs, and a FieldExpansionQueryParser may be used to apply the field/term mappings when supplied in regular Lucene query strings.
Author:
John Weatherley
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    VirtualSearchFieldMapper(org.apache.lucene.queryParser.QueryParser parser)
    Constructor for the VirtualSearchFieldMapper object
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration file, replacing any previous definitions.
    void
    Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration String, replacing any previous definitions.
    void
    Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration file, replacing any previous definitions.
    void
    Clears all field/term definitions that are defined.
    boolean
    getIsTermConfiguredForField(String virtualField, String virtualTerm)
    Determines whether the given virtual term is configured for the given field.
    int
    Gets the number of virtual terms configured for a given virtual field.
    org.apache.lucene.search.Query
    getQuery(String virtualField, String virtualTerm)
    Gets the Lucene Query that is defined for the given virtual field/term pair, or null if none avaialable.
    getQueryString(String virtualField, String virtualTerm)
    Gets the Lucene Query as a String for the given virtual field and term, or null if not avaialable.
    Gets the virtual fields that are defined.
    Gets the virtual terms that are defined for the given field, or null.
    boolean
    remove(String virtualField)
    Removes all term definitions that are defined for the given field.
    boolean
    remove(String virtualField, String virtualTerm)
    Removes the definition that is defined for the given field and term.
    void
    setQuery(String virtualField, String virtualTerm, String query)
    Sets the Query assigned for the given field and term, overwriting any previous definition.
    void
    setQuery(String virtualField, String virtualTerm, org.apache.lucene.search.Query query)
    Sets the Query assigned for the given field and term, overwriting any previous definition.
    Outputs a String representation of the fields/terms/queries that are defined in this VirtulSearchFieldMapper.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • VirtualSearchFieldMapper

      public VirtualSearchFieldMapper(org.apache.lucene.queryParser.QueryParser parser)
      Constructor for the VirtualSearchFieldMapper object
      Parameters:
      parser - The QueryParser used to parse Lucene queries that are defined in the configuration file
  • Method Details

    • getQuery

      public org.apache.lucene.search.Query getQuery(String virtualField, String virtualTerm)
      Gets the Lucene Query that is defined for the given virtual field/term pair, or null if none avaialable.
      Parameters:
      virtualField - The virtual search field
      virtualTerm - The virtual search term
      Returns:
      The Lucene Query, or null if none avaialable
    • getQueryString

      public String getQueryString(String virtualField, String virtualTerm)
      Gets the Lucene Query as a String for the given virtual field and term, or null if not avaialable.
      Parameters:
      virtualField - The virtual search field
      virtualTerm - The virtual search term
      Returns:
      The Lucene Query as a String, or null if not avaialable
    • getNumTermsConfiguredForField

      public int getNumTermsConfiguredForField(String virtualField)
      Gets the number of virtual terms configured for a given virtual field.
      Parameters:
      virtualField - The virtual search field
      Returns:
      The number of virtual terms configured for this field or zero if the given field does not exist
    • getIsTermConfiguredForField

      public boolean getIsTermConfiguredForField(String virtualField, String virtualTerm)
      Determines whether the given virtual term is configured for the given field.
      Parameters:
      virtualField - The virtual search field
      virtualTerm - The virtual search term
      Returns:
      True if the given virtual field exists and the given virtual term is configured for that field
    • setQuery

      public void setQuery(String virtualField, String virtualTerm, org.apache.lucene.search.Query query)
      Sets the Query assigned for the given field and term, overwriting any previous definition.
      Parameters:
      virtualField - The field
      virtualTerm - The term
      query - The new Query value for this field/term
    • setQuery

      public void setQuery(String virtualField, String virtualTerm, String query) throws org.apache.lucene.queryParser.ParseException
      Sets the Query assigned for the given field and term, overwriting any previous definition. The query String provided must conform to the grammar understood by the QueryParser Object that was passed into the constructor.
      Parameters:
      virtualField - The field
      virtualTerm - The term
      query - The new Query value for this field/term
      Throws:
      org.apache.lucene.queryParser.ParseException - If error parsing the query String
    • remove

      public boolean remove(String virtualField, String virtualTerm)
      Removes the definition that is defined for the given field and term. After removal, subsequent calls to getQuery(java.lang.String, java.lang.String) or getQueryString(java.lang.String, java.lang.String) for this field/term pair will return null.
      Parameters:
      virtualField - The field
      virtualTerm - The term
      Returns:
      True if a mapping existed for this field/term pair and was removed, otherwise false
    • remove

      public boolean remove(String virtualField)
      Removes all term definitions that are defined for the given field. After removal, subsequent calls to getQuery(java.lang.String, java.lang.String) or getQueryString(java.lang.String, java.lang.String) will return null for all references to this field.
      Parameters:
      virtualField - The field to remove
      Returns:
      True if one or more mappings existed for this field and were removed, otherwise false
    • clear

      public void clear()
      Clears all field/term definitions that are defined. After calling this method, subsequent calls to getQuery(java.lang.String, java.lang.String) or getQueryString(java.lang.String, java.lang.String) will return null for all calls.
    • getVirtualFields

      public String[] getVirtualFields()
      Gets the virtual fields that are defined.
      Returns:
      The virtual fields
    • getVirtualTerms

      public String[] getVirtualTerms(String field)
      Gets the virtual terms that are defined for the given field, or null.
      Parameters:
      field - The field
      Returns:
      The virtual terms defined for the given field, or null
    • addVirtualFieldConfiguration

      public void addVirtualFieldConfiguration(String xmlConfig) throws Exception
      Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration String, replacing any previous definitions.
      Parameters:
      xmlConfig - The VirtualSearchFieldMapper configuration XML string
      Throws:
      Exception - If error parsing the String
    • addVirtualFieldConfiguration

      public void addVirtualFieldConfiguration(File xmlConfigFile) throws Exception
      Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration file, replacing any previous definitions.
      Parameters:
      xmlConfigFile - The VirtualSearchFieldMapper configuration XML file
      Throws:
      Exception - If error parsing or reading the file
    • addVirtualFieldConfiguration

      public void addVirtualFieldConfiguration(URL xmlConfig) throws Exception
      Adds all virtual field/term query definitions (virtualSearchField elements) contained in the given XML configuration file, replacing any previous definitions.
      Parameters:
      xmlConfig - The URL to the VirtualSearchFieldMapper configuration XML file
      Throws:
      Exception - If error parsing or reading the url
    • toString

      public String toString()
      Outputs a String representation of the fields/terms/queries that are defined in this VirtulSearchFieldMapper.
      Overrides:
      toString in class Object
      Returns:
      A String representation of the fields/terms/queries