About
Resources
Download
|
Overview
|
Although Lucene provides the ability to create your own
queries through its API, it also provides a rich query
language through the Query Parser.
This page
provides syntax of Lucene's Query Parser, a lexer which
interprets a string into a Lucene Query using JavaCC.
Before choosing to use the provided Query Parser, please consider the following:
- If you are programmatically generating a query string and then
parsing it with the query parser then you should seriously consider building
your queries directly with the query API. In other words, the query
parser is designed for human-entered text, not for program-generated
text.
- Untokenized fields are best added directly to queries, and not
through the query parser. If a field's values are generated programmatically
by the application, then so should query clauses for this field.
An analyzer, which the query parser uses, is designed to convert human-entered
text to terms. Program-generated values, like dates, keywords, etc.,
should be consistently program-generated.
- In a query form, fields which are general text should use the query
parser. All others, such as date ranges, keywords, etc. are better added
directly through the query API. A field with a limit set of values,
that can be specified with a pull-down menu should not be added to a
query string which is subsequently parsed, but rather added as a
TermQuery clause.
|
|
Terms
|
A query is broken up into terms and operators. There are two types of terms: Single Terms and Phrases.
A Single Term is a single word such as "test" or "hello".
A Phrase is a group of words surrounded by double quotes such as "hello dolly".
Multiple terms can be combined together with Boolean operators to form a more complex query (see below).
Note: The analyzer used to create the index will be used on the terms and phrases in the query string.
So it is important to choose an analyzer that will not interfere with the terms used in the query string.
|
|
Term Modifiers
|
Lucene supports modifying query terms to provide a wide range of searching options.
Wildcard Searches
|
Lucene supports single and multiple character wildcard searches.
To perform a single character wildcard search use the "?" symbol.
To perform a multiple character wildcard search use the "*" symbol.
The single character wildcard search looks for terms that match that with the single character replaced. For example, to search for "text" or "test" you can use the search:
Multiple character wildcard searches looks for 0 or more characters. For example, to search for test, tests or tester, you can use the search:
You can also use the wildcard searches in the middle of a term.
Note: You cannot use a * or ? symbol as the first character of a search.
|
|
Fuzzy Searches
|
Lucene supports fuzzy searches based on the Levenshtein Distance, or Edit Distance algorithm. To do a fuzzy search use the tilde, "~", symbol at the end of a Single word Term. For example to search for a term similar in spelling to "roam" use the fuzzy search:
This search will find terms like foam and roams.
Starting with Lucene 1.9 an additional (optional) parameter can specify the required similarity. The value is between 0 and 1, with a value closer to 1 only terms with a higher similarity will be matched. For example:
The default that is used if the parameter is not given is 0.5.
|
|
Proximity Searches
|
Lucene supports finding words are a within a specific distance away. To do a proximity search use the tilde, "~", symbol at the end of a Phrase. For example to search for a "apache" and "jakarta" within 10 words of each other in a document use the search:
|
|
|
|
Boolean operators
|
Boolean operators allow terms to be combined through logic operators.
Lucene supports AND, "+", OR, NOT and "-" as Boolean operators(Note: Boolean operators must be ALL CAPS).
+
|
The "+" or required operator requires that the term after the "+" symbol exist somewhere in a the field of a single document.
To search for documents that must contain "jakarta" and may contain "lucene" use the query:
|
|
|
|
Escaping Special Characters
|
Lucene supports escaping special characters that are part of the query syntax. The current list special characters are
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
To escape these character use the \ before the character. For example to search for (1+1):2 use the query:
|
|
|