Class SearchQueryParser

java.lang.Object
org.graylog2.search.SearchQueryParser

public class SearchQueryParser extends Object
Parses a simple query language for use in list filtering of data sitting in MongoDB.

The syntax is as follows:

*
The * or empty string is the match-all operator, it should not be combined with others
sometext
Searches for sometext in the default field
field:sometext
Searches in the given field named field
-field:sometext
Searches in the given field field but negates the search condition
field:some,text
Adds field twice, as if field:some field:text was given

Whitespace is used to separate words, if whitespace is important you can quote the string with single or double quotes: field:'hello world'.

The class needs two parameters, the default field name to use for query text and the set of allowed fields to search in. The default field name must be the database name.

If the external field names should be different to what is used by the database, you can optionally pass the allowed fields as a Map. The parser will then try to replace each field name with the corresponding value in the map.

If the key has no mapping the default field name is used instead and the field name is recorded as being invalid. This lets the caller execute the query but still allows for error checking. It is the callers responsibility to decide which behavior is applicable.

Instances of this class are safe to use from multiple threads.
  • Field Details

  • Constructor Details

    • SearchQueryParser

      public SearchQueryParser(@Nonnull String defaultField, @Nonnull Set<String> allowedFields)
      Constructs a new parser without field mapping.
      Parameters:
      defaultField - the name of the default field
      allowedFields - the names of allowed fields in the query
    • SearchQueryParser

      public SearchQueryParser(@Nonnull String defaultField, @Nonnull Set<String> allowedFields, String fieldPrefix)
      Constructs a new parser without field mapping.
      Parameters:
      defaultField - the name of the default field
      allowedFields - the names of allowed fields in the query
      fieldPrefix - Prefix appended to ALL the fields in the query. Useful if querying nested fields.
    • SearchQueryParser

      public SearchQueryParser(@Nonnull String defaultField, @Nonnull Map<String,SearchQueryField> allowedFieldsWithMapping)
      Constructs a new parser with explicit field mapping.
      Parameters:
      defaultField - the name of the default field (already mapped)
      allowedFieldsWithMapping - the map of field mappings, keys are the allowed fields, values are the replacements
  • Method Details