Query string syntax

The FulltextExpression supports the compact Lucene query string syntax, which allows to specify AND OR NOT conditions and multi-field search within a single query string.

In addition, picturesafe-search offers a preprocessor that allows a less strict, more intuitive query string, especially regarding logical operators and parentheses:

  • Synonyms for the operator AND
  • Synonyms for the operator OR
  • Synonyms for the operator NOT
  • Adding missing operators automatically
  • Adding brackets automatically
  • Token delimiters

The query string preprocessor is enabled by default. Settings can be changed in elasticsearch.properties:

# --- Query string preprocessor ---

## Enable preprocessing of fulltext query strings (will be enabled per default when empty)
#elasticsearch.querystring_preprocessor.enabled=true

## Enable adding brackets automatically in preprocessor (will be enabled per default when empty)
#elasticsearch.querystring_preprocessor.auto_bracket=true

## Enable adding missing operators automatically in preprocessor (will be enabled per default when empty)
#elasticsearch.querystring_preprocessor.insert_missing_operators=true

## Token delimiters in preprocessor (will be set to the following default value when empty)
#elasticsearch.querystring_preprocessor.token_delimiters:, "(){}[]:=\\/^~

## Synonyms for the operator AND (separated by blank, will be set to 'and und & +' when empty)
#elasticsearch.querystring_preprocessor.synonyms.AND=and und & +

## Synonyms for the operator OR (separated by blank, will be set to 'or oder | ,' when empty)
#elasticsearch.querystring_preprocessor.synonyms.OR=or oder | ,

## Synonyms for the operator NOT (separated by blank, will be set to 'not nicht -' when empty)
#elasticsearch.querystring_preprocessor.synonyms.NOT=not nicht -

Synonyms for logical operators AND OR NOT

The following examples use different synonyms for the AND operator, but result in the same logical fulltext expression:

Expression expression = new FulltextExpression("+Berlin +Germany");

Expression expression = new FulltextExpression("Berlin AND Germany");

Expression expression = new FulltextExpression("Berlin and Germany");

Expression expression = new FulltextExpression("Berlin & Germany");

Expression expression = new FulltextExpression("Berlin && Germany");

// adding missing operators automatically
Expression expression = new FulltextExpression("Berlin Germany");

The following examples use different synonyms for the OR operator, but result in the same logical fulltext expression:

Expression expression = new FulltextExpression("Berlin OR London or Washington | Paris");
           
Expression expression = new FulltextExpression("Berlin,London,Washington,Paris");

The following examples use different synonyms for the NOT operator, but result in the same logical fulltext expression:

Expression expression = new FulltextExpression("capital -Berlin");
           
Expression expression = new FulltextExpression("capital NOT Berlin");

// Synonyms can also be configured language-specific, here e.g. for German
Expression expression = new FulltextExpression("capital nicht Berlin");

Brackets

The familiar boolean operators AND, OR and NOT are also supported by the Elasticsearch query string syntax, but beware that they do not honor the usual precedence rules (so Elasticsearch recommends to use parentheses whenever multiple operators are used together).

For this reason, picturesafe-search extends parentheses internally by default when using logical operators.

The following logical expressions have the same precedence:

Expression expression 
    = new FulltextExpression("(Berlin AND Germany) OR (Paris AND France) OR Washington");
             
Expression expression 
    = new FulltextExpression("Berlin AND Germany OR Paris AND France OR Washington");

A query string syntax sample can be found here.