Class SearchQueryParser
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 iffield: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.-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final SearchQueryOperator
static final SearchQueryOperator
-
Constructor Summary
ConstructorDescriptionSearchQueryParser
(String defaultField, Map<String, SearchQueryField> allowedFieldsWithMapping) Constructs a new parser with explicit field mapping.SearchQueryParser
(String defaultField, Set<String> allowedFields) Constructs a new parser without field mapping.SearchQueryParser
(String defaultField, Set<String> allowedFields, String fieldPrefix) Constructs a new parser without field mapping. -
Method Summary
-
Field Details
-
DEFAULT_STRING_OPERATOR
-
DEFAULT_OPERATOR
-
-
Constructor Details
-
SearchQueryParser
Constructs a new parser without field mapping.- Parameters:
defaultField
- the name of the default fieldallowedFields
- 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 fieldallowedFields
- the names of allowed fields in the queryfieldPrefix
- 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
-
parse
-