Lucene Query Syntax Cheat Sheet

Lucene query syntax is used for searching within the Lucene search engine. It provides a powerful and flexible way to construct queries. Here’s a cheat sheet for Lucene query syntax:

Basic Queries

Term Query:

  • Search for a specific term.
term:java

Boolean Operators:

  • Use AND, OR, NOT for combining terms.
java AND lucene

Wildcards and Fuzzy Search

Wildcard (*):

  • Match terms with wildcards.
term:pro*

Fuzzy Search (~):

  • Search for terms with approximate matching.
term:java~

Phrase Queries

Exact Phrase:

  • Search for an exact phrase.
"exact phrase"

Proximity Search:

  • Find terms within a specified distance of each other.
"java lucene"~5

Range Queries

Numeric Range:

  • Search for a numeric range.
range:[10 TO 100]

Date Range:

  • Search for a date range.
date:[20220101 TO 20221231]

Boosting

Boosting:

  • Boost the importance of a term.
java^2 lucene

Grouping

Grouping:

  • Group terms using parentheses.
(java OR python) AND lucene

Field-specific Queries

Field-specific Query:

  • Search within a specific field.
title:java

Prohibited Terms

Prohibited Term:

  • Exclude specific terms.
java NOT legacy

Special Characters:

  1. Escape Special Characters:
    • Escape characters with a backslash.
special\:term

Lucene Query Parser

Lucene Query Parser:

  • Use the QueryParser class in Lucene to parse queries programmatically.
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query query = parser.parse("java AND lucene");

Notes

  • Lucene query syntax is case-sensitive.
  • The default operator is OR if no operator is specified.
  • The Lucene Query Parser is commonly used in programming to parse user input and construct queries dynamically.

This cheat sheet provides a quick reference for constructing queries using Lucene query syntax. Adjust the examples based on your specific use case and index structure.