1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
<meta content="Apache Forrest" name="Generator">
6
<meta name="Forrest-version" content="0.9">
7
<meta name="Forrest-skin-name" content="lucene">
9
Apache Lucene - Query Parser Syntax
11
<link type="text/css" href="skin/basic.css" rel="stylesheet">
12
<link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
13
<link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
14
<link type="text/css" href="skin/profile.css" rel="stylesheet">
15
<script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
16
<link rel="shortcut icon" href="images/favicon.ico">
18
<body onload="init()">
19
<script type="text/javascript">ndeSetTextSize();</script>
24
<div class="breadtrail">
25
<a href="http://www.apache.org/">Apache</a> > <a href="http://lucene.apache.org/">Lucene</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
34
<div class="grouplogo">
35
<a href="http://lucene.apache.org/"><img class="logoImage" alt="Lucene" src="http://www.apache.org/images/asf_logo_simple.png" title="Apache Lucene"></a>
43
<div class="projectlogo">
44
<a href="http://lucene.apache.org/java/"><img class="logoImage" alt="Lucene" src="http://lucene.apache.org/images/lucene_green_300.gif" title="Apache Lucene is a high-performance, full-featured text search engine library written entirely in
45
Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform."></a>
53
<div class="searchbox">
54
<form action="http://search.lucidimagination.com/p:lucene" method="get" class="roundtopsmall">
55
<input onFocus="getBlank (this, 'Search the site with Lucene');" size="25" name="q" id="query" type="text" value="Search the site with Lucene">
56
<input name="Search" value="Search" type="submit">
58
<div style="position: relative; top: -5px; left: -10px">Powered by <a href="http://www.lucidimagination.com" style="color: #033268">Lucid Imagination</a>
69
<a class="selected" href="http://lucene.apache.org/java/docs/">Main</a>
72
<a class="unselected" href="http://wiki.apache.org/lucene-java">Wiki</a>
75
<a class="selected" href="index.html">Lucene 3.5 Documentation</a>
84
<div id="publishedStrip">
88
<div id="level2tabs"></div>
92
<script type="text/javascript"><!--
93
document.write("Last Published: " + document.lastModified);
99
<div class="breadtrail">
104
|start Menu, mainarea
110
<div onclick="SwitchMenu('menu_selected_1.1', 'skin/')" id="menu_selected_1.1Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Documentation</div>
111
<div id="menu_selected_1.1" class="selectedmenuitemgroup" style="display: block;">
112
<div class="menuitem">
113
<a href="index.html">Overview</a>
115
<div onclick="SwitchMenu('menu_1.1.2', 'skin/')" id="menu_1.1.2Title" class="menutitle">Changes</div>
116
<div id="menu_1.1.2" class="menuitemgroup">
117
<div class="menuitem">
118
<a href="changes/Changes.html">Core</a>
120
<div class="menuitem">
121
<a href="changes/Contrib-Changes.html">Contrib</a>
124
<div onclick="SwitchMenu('menu_1.1.3', 'skin/')" id="menu_1.1.3Title" class="menutitle">Javadocs</div>
125
<div id="menu_1.1.3" class="menuitemgroup">
126
<div class="menuitem">
127
<a href="api/all/index.html">All</a>
129
<div class="menuitem">
130
<a href="api/core/index.html">Core</a>
132
<div class="menuitem">
133
<a href="api/test-framework/index.html">Test Framework</a>
135
<div onclick="SwitchMenu('menu_1.1.3.4', 'skin/')" id="menu_1.1.3.4Title" class="menutitle">Contrib</div>
136
<div id="menu_1.1.3.4" class="menuitemgroup">
137
<div class="menuitem">
138
<a href="api/contrib-analyzers/index.html">Analyzers</a>
140
<div class="menuitem">
141
<a href="api/contrib-smartcn/index.html">Smart Chinese Analyzer</a>
143
<div class="menuitem">
144
<a href="api/contrib-stempel/index.html">Stempel Polish Analyzer</a>
146
<div class="menuitem">
147
<a href="api/contrib-benchmark/index.html">Benchmark</a>
149
<div class="menuitem">
150
<a href="api/contrib-demo/index.html">Demo</a>
152
<div class="menuitem">
153
<a href="api/contrib-grouping/index.html">Grouping</a>
155
<div class="menuitem">
156
<a href="api/contrib-highlighter/index.html">Highlighter</a>
158
<div class="menuitem">
159
<a href="api/contrib-icu/index.html">ICU</a>
161
<div class="menuitem">
162
<a href="api/contrib-instantiated/index.html">Instantiated</a>
164
<div class="menuitem">
165
<a href="api/contrib-join/index.html">Join</a>
167
<div class="menuitem">
168
<a href="api/contrib-memory/index.html">Memory</a>
170
<div class="menuitem">
171
<a href="api/contrib-misc/index.html">Miscellaneous</a>
173
<div class="menuitem">
174
<a href="api/contrib-queries/index.html">Queries</a>
176
<div class="menuitem">
177
<a href="api/contrib-queryparser/index.html">Query Parser Framework</a>
179
<div class="menuitem">
180
<a href="api/contrib-remote/index.html">Remote</a>
182
<div class="menuitem">
183
<a href="api/contrib-spatial/index.html">Spatial</a>
185
<div class="menuitem">
186
<a href="api/contrib-spellchecker/index.html">Spellchecker</a>
188
<div class="menuitem">
189
<a href="api/contrib-xml-query-parser/index.html">XML Query Parser</a>
193
<div class="menuitem">
194
<a href="systemrequirements.html">System Requirements</a>
196
<div class="menuitem">
197
<a href="contributions.html">Contributions</a>
199
<div class="menuitem">
200
<a href="http://wiki.apache.org/lucene-java/LuceneFAQ">FAQ</a>
202
<div class="menuitem">
203
<a href="fileformats.html">File Formats</a>
205
<div class="menuitem">
206
<a href="gettingstarted.html">Getting Started</a>
208
<div class="menuitem">
209
<a href="lucene-contrib/index.html">Lucene Contrib</a>
211
<div class="menupage">
212
<div class="menupagetitle">Query Syntax</div>
214
<div class="menuitem">
215
<a href="scoring.html">Scoring</a>
217
<div class="menuitem">
218
<a href="http://wiki.apache.org/lucene-java">Wiki</a>
221
<div id="credit"></div>
222
<div id="roundbottom">
223
<img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
227
<div id="credit2"></div>
237
Apache Lucene - Query Parser Syntax
239
<div id="minitoc-area">
242
<a href="#Overview">Overview</a>
245
<a href="#Terms">Terms</a>
248
<a href="#Fields">Fields</a>
251
<a href="#Term Modifiers">Term Modifiers</a>
254
<a href="#Wildcard Searches">Wildcard Searches</a>
257
<a href="#Fuzzy Searches">Fuzzy Searches</a>
260
<a href="#Proximity Searches">Proximity Searches</a>
263
<a href="#Range Searches">Range Searches</a>
266
<a href="#Boosting a Term">Boosting a Term</a>
271
<a href="#Boolean operators">Boolean Operators</a>
277
<a href="#AND">AND</a>
283
<a href="#NOT">NOT</a>
291
<a href="#Grouping">Grouping</a>
294
<a href="#Field Grouping">Field Grouping</a>
297
<a href="#Escaping Special Characters">Escaping Special Characters</a>
302
<a name="N10013"></a><a name="Overview"></a>
303
<h2 class="boxed">Overview</h2>
304
<div class="section">
305
<p>Although Lucene provides the ability to create your own
306
queries through its API, it also provides a rich query
307
language through the Query Parser, a lexer which
308
interprets a string into a Lucene Query using JavaCC.
310
<p>Generally, the query parser syntax may change from
311
release to release. This page describes the syntax as of
312
the current release. If you are using a different
313
version of Lucene, please consult the copy of
314
<span class="codefrag">docs/queryparsersyntax.html</span> that was distributed
315
with the version you are using.
318
Before choosing to use the provided Query Parser, please consider the following:
321
<li>If you are programmatically generating a query string and then
322
parsing it with the query parser then you should seriously consider building
323
your queries directly with the query API. In other words, the query
324
parser is designed for human-entered text, not for program-generated
328
<li>Untokenized fields are best added directly to queries, and not
329
through the query parser. If a field's values are generated programmatically
330
by the application, then so should query clauses for this field.
331
An analyzer, which the query parser uses, is designed to convert human-entered
332
text to terms. Program-generated values, like dates, keywords, etc.,
333
should be consistently program-generated.</li>
336
<li>In a query form, fields which are general text should use the query
337
parser. All others, such as date ranges, keywords, etc. are better added
338
directly through the query API. A field with a limit set of values,
339
that can be specified with a pull-down menu should not be added to a
340
query string which is subsequently parsed, but rather added as a
341
TermQuery clause.</li>
349
<a name="N10032"></a><a name="Terms"></a>
350
<h2 class="boxed">Terms</h2>
351
<div class="section">
352
<p>A query is broken up into terms and operators. There are two types of terms: Single Terms and Phrases.</p>
353
<p>A Single Term is a single word such as "test" or "hello".</p>
354
<p>A Phrase is a group of words surrounded by double quotes such as "hello dolly".</p>
355
<p>Multiple terms can be combined together with Boolean operators to form a more complex query (see below).</p>
356
<p>Note: The analyzer used to create the index will be used on the terms and phrases in the query string.
357
So it is important to choose an analyzer that will not interfere with the terms used in the query string.</p>
361
<a name="N10048"></a><a name="Fields"></a>
362
<h2 class="boxed">Fields</h2>
363
<div class="section">
364
<p>Lucene supports fielded data. When performing a search you can either specify a field, or use the default field. The field names and default field is implementation specific.</p>
365
<p>You can search any field by typing the field name followed by a colon ":" and then the term you are looking for. </p>
366
<p>As an example, let's assume a Lucene index contains two fields, title and text and text is the default field.
367
If you want to find the document entitled "The Right Way" which contains the text "don't go this way", you can enter: </p>
368
<pre class="code">title:"The Right Way" AND text:go</pre>
370
<pre class="code">title:"Do it right" AND right</pre>
371
<p>Since text is the default field, the field indicator is not required.</p>
372
<p>Note: The field is only valid for the term that it directly precedes, so the query</p>
373
<pre class="code">title:Do it right</pre>
374
<p>Will only find "Do" in the title field. It will find "it" and "right" in the default field (in this case the text field). </p>
378
<a name="N1006D"></a><a name="Term Modifiers"></a>
379
<h2 class="boxed">Term Modifiers</h2>
380
<div class="section">
381
<p>Lucene supports modifying query terms to provide a wide range of searching options.</p>
382
<a name="N10076"></a><a name="Wildcard Searches"></a>
383
<h3 class="boxed">Wildcard Searches</h3>
384
<p>Lucene supports single and multiple character wildcard searches within single terms
385
(not within phrase queries).</p>
386
<p>To perform a single character wildcard search use the "?" symbol.</p>
387
<p>To perform a multiple character wildcard search use the "*" symbol.</p>
388
<p>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:</p>
389
<pre class="code">te?t</pre>
390
<p>Multiple character wildcard searches looks for 0 or more characters. For example, to search for test, tests or tester, you can use the search: </p>
391
<pre class="code">test*</pre>
392
<p>You can also use the wildcard searches in the middle of a term.</p>
393
<pre class="code">te*t</pre>
394
<p>Note: You cannot use a * or ? symbol as the first character of a search.</p>
395
<a name="N1009B"></a><a name="Fuzzy Searches"></a>
396
<h3 class="boxed">Fuzzy Searches</h3>
397
<p>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: </p>
398
<pre class="code">roam~</pre>
399
<p>This search will find terms like foam and roams.</p>
400
<p>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:</p>
401
<pre class="code">roam~0.8</pre>
402
<p>The default that is used if the parameter is not given is 0.5.</p>
403
<a name="N100B4"></a><a name="Proximity Searches"></a>
404
<h3 class="boxed">Proximity Searches</h3>
405
<p>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: </p>
406
<pre class="code">"jakarta apache"~10</pre>
407
<a name="N100C1"></a><a name="Range Searches"></a>
408
<h3 class="boxed">Range Searches</h3>
409
<p>Range Queries allow one to match documents whose field(s) values
410
are between the lower and upper bound specified by the Range Query.
411
Range Queries can be inclusive or exclusive of the upper and lower bounds.
412
Sorting is done lexicographically.</p>
413
<pre class="code">mod_date:[20020101 TO 20030101]</pre>
414
<p>This will find documents whose mod_date fields have values between 20020101 and 20030101, inclusive.
415
Note that Range Queries are not reserved for date fields. You could also use range queries with non-date fields:</p>
416
<pre class="code">title:{Aida TO Carmen}</pre>
417
<p>This will find all documents whose titles are between Aida and Carmen, but not including Aida and Carmen.</p>
418
<p>Inclusive range queries are denoted by square brackets. Exclusive range queries are denoted by
420
<a name="N100DA"></a><a name="Boosting a Term"></a>
421
<h3 class="boxed">Boosting a Term</h3>
422
<p>Lucene provides the relevance level of matching documents based on the terms found. To boost a term use the caret, "^", symbol with a boost factor (a number) at the end of the term you are searching. The higher the boost factor, the more relevant the term will be.</p>
423
<p>Boosting allows you to control the relevance of a document by boosting its term. For example, if you are searching for</p>
424
<pre class="code">jakarta apache</pre>
425
<p>and you want the term "jakarta" to be more relevant boost it using the ^ symbol along with the boost factor next to the term.
427
<pre class="code">jakarta^4 apache</pre>
428
<p>This will make documents with the term jakarta appear more relevant. You can also boost Phrase Terms as in the example: </p>
429
<pre class="code">"jakarta apache"^4 "Apache Lucene"</pre>
430
<p>By default, the boost factor is 1. Although the boost factor must be positive, it can be less than 1 (e.g. 0.2)</p>
435
<a name="N100FA"></a><a name="Boolean operators"></a>
436
<h2 class="boxed">Boolean Operators</h2>
437
<div class="section">
438
<p>Boolean operators allow terms to be combined through logic operators.
439
Lucene supports AND, "+", OR, NOT and "-" as Boolean operators(Note: Boolean operators must be ALL CAPS).</p>
440
<a name="N10103"></a><a name="OR"></a>
441
<h3 class="boxed"></h3>
442
<p>The OR operator is the default conjunction operator. This means that if there is no Boolean operator between two terms, the OR operator is used.
443
The OR operator links two terms and finds a matching document if either of the terms exist in a document. This is equivalent to a union using sets.
444
The symbol || can be used in place of the word OR.</p>
445
<p>To search for documents that contain either "jakarta apache" or just "jakarta" use the query:</p>
446
<pre class="code">"jakarta apache" jakarta</pre>
448
<pre class="code">"jakarta apache" OR jakarta</pre>
449
<a name="N10116"></a><a name="AND"></a>
450
<h3 class="boxed">AND</h3>
451
<p>The AND operator matches documents where both terms exist anywhere in the text of a single document.
452
This is equivalent to an intersection using sets. The symbol && can be used in place of the word AND.</p>
453
<p>To search for documents that contain "jakarta apache" and "Apache Lucene" use the query: </p>
454
<pre class="code">"jakarta apache" AND "Apache Lucene"</pre>
455
<a name="N10126"></a><a name="+"></a>
456
<h3 class="boxed">+</h3>
457
<p>The "+" or required operator requires that the term after the "+" symbol exist somewhere in a the field of a single document.</p>
458
<p>To search for documents that must contain "jakarta" and may contain "lucene" use the query:</p>
459
<pre class="code">+jakarta lucene</pre>
460
<a name="N10136"></a><a name="NOT"></a>
461
<h3 class="boxed">NOT</h3>
462
<p>The NOT operator excludes documents that contain the term after NOT.
463
This is equivalent to a difference using sets. The symbol ! can be used in place of the word NOT.</p>
464
<p>To search for documents that contain "jakarta apache" but not "Apache Lucene" use the query: </p>
465
<pre class="code">"jakarta apache" NOT "Apache Lucene"</pre>
466
<p>Note: The NOT operator cannot be used with just one term. For example, the following search will return no results:</p>
467
<pre class="code">NOT "jakarta apache"</pre>
468
<a name="N1014C"></a><a name="-"></a>
469
<h3 class="boxed">-</h3>
470
<p>The "-" or prohibit operator excludes documents that contain the term after the "-" symbol.</p>
471
<p>To search for documents that contain "jakarta apache" but not "Apache Lucene" use the query: </p>
472
<pre class="code">"jakarta apache" -"Apache Lucene"</pre>
476
<a name="N1015D"></a><a name="Grouping"></a>
477
<h2 class="boxed">Grouping</h2>
478
<div class="section">
479
<p>Lucene supports using parentheses to group clauses to form sub queries. This can be very useful if you want to control the boolean logic for a query.</p>
480
<p>To search for either "jakarta" or "apache" and "website" use the query:</p>
481
<pre class="code">(jakarta OR apache) AND website</pre>
482
<p>This eliminates any confusion and makes sure you that website must exist and either term jakarta or apache may exist.</p>
486
<a name="N10170"></a><a name="Field Grouping"></a>
487
<h2 class="boxed">Field Grouping</h2>
488
<div class="section">
489
<p>Lucene supports using parentheses to group multiple clauses to a single field.</p>
490
<p>To search for a title that contains both the word "return" and the phrase "pink panther" use the query:</p>
491
<pre class="code">title:(+return +"pink panther")</pre>
495
<a name="N10180"></a><a name="Escaping Special Characters"></a>
496
<h2 class="boxed">Escaping Special Characters</h2>
497
<div class="section">
498
<p>Lucene supports escaping special characters that are part of the query syntax. The current list special characters are</p>
499
<p>+ - && || ! ( ) { } [ ] ^ " ~ * ? : \</p>
500
<p>To escape these character use the \ before the character. For example to search for (1+1):2 use the query:</p>
501
<pre class="code">\(1\+1\)\:2</pre>
509
<div class="clearboth"> </div>
515
<div class="lastmodified">
516
<script type="text/javascript"><!--
517
document.write("Last Published: " + document.lastModified);
520
<div class="copyright">
522
2006 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>