About
Resources
Download
Jakarta
|
Overview
|
Although Lucene provides the ability to create your own query's though its API, it also provides a rich query language through the QueryParser.
This page provides syntax of Lucene's Query Parser, a lexer which interprets a string into a Lucene Query using JavaCC.
|
|
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
Note:Terms found by the fuzzy search will automatically get a boost factor of 0.2
|
|
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:
|
|
|