Lucene Query Syntax

Lucene provides a powerful, text-based syntax for searching. Since many search engines (like Elasticsearch and OpenSearch) are built on top of Lucene, knowing how to use this syntax is key to make your analysis stronger.

The Basics: Terms and Fields

At its core, a query is broken down into Terms and Operators.

  • Single Term: A single word, like johndoe.

  • Phrase: A group of words surrounded by double quotes, like "John Doe".

  • Fields: You can search specific fields by typing the field name followed by a colon.

    • Example: title:"Unusual Login" AND description:johndoe


Boolean Operation

Operator

Description

Example

OR

The default joiner; finds documents with either term.

jakarta OR apache

AND

Finds documents where both terms exist.

jakarta AND apache

NOT

Excludes documents that contain the term after NOT.

jakarta NOT lucene

+

The term must exist in the document.

+jakarta lucene

-

The term must not exist in the document.

-jakarta lucene


Wildcard and Fuzzy Searches

When you aren't 100% sure of the spelling or want to find variations of a word.

  • Wildcards: * ? replaces a single character (e.g., te?t).

    • * replaces multiple characters (e.g., test*).

  • Fuzzy Search: Use the tilde ~ to find words with similar spellings based on the Damerau-Levenshtein distance.

    • Example: roam~ will find foam or roams.

  • Proximity Search: Find words within a specific distance of each other.

    • Example: "jakarta apache"~10 (finds "jakarta" and "apache" within 10 words of each other).


Ranges and Boosting

  • Range Searches: Use brackets for inclusive ranges [ ] and curly braces for exclusive ranges { }.

    • Inclusive: mod_date:[20230101 TO 20231231]

    • Exclusive: count:{1 TO 10}

  • Term Boosting: Use the caret ^ to make one term more relevant than another.

    • Example: jakarta^4 apache makes "jakarta" four times more important to the relevance score.


Grouping

You can use parentheses to group clauses to form sub-queries, which is very helpful for controlling Boolean logic.

Example: (jakarta OR apache) AND website This ensures that "website" must be present, along with either "jakarta" or "apache".


Escaping Characters

  • Escaping Characters: If you need to search for special characters that are part of the syntax (like + - && || ! ( ) { } [ ] ^ " ~ * ? : \), use a backslash \ before them.

  • Case Sensitivity: While field names are case-sensitive, the Boolean operators must be in ALL CAPS to function as operators.


Learn More about Lucene Syntax Query



tags:

  • KQL

  • Lucene Syntax

  • Query

  • Sentio Search