Hermit Notebook

Lucene Query Language

Reminder: you can left-scroll and right-scroll the long code blocks

Introduction

Lucene is a free and open-source information retrieval library supported by the Apache Software Foundation.

It powers the Apache Solr search engine as well as Elasticsearch.

In both of these engines, you sometimes want to make queries directly to Lucene. The following presents the Lucene Query Language expressions that I use the most.

General operators

+
The term must be present
-
The term must be absent
_exists_
The field must not be empty
^
Boosts the term
AND, OR, NOT (also noted &&, ||, !)
Boolean operators
/regx/
Regular expressions
[xx TO yy ]
Inclusive range
{xx TO yy}
Exclusive range
1
2
3
4
5
6
_exists_:author
title:(quick brown +fox -news)
name:("john" "regine"^2 ("patrick" "fatima")^4)
firstname:/joh?n(ath[oa]n)/
age:[1 TO 5}
_exists_:author && title:(quick brown +fox -news) && name:("josh" "alex"^2 ("fatim" "georges")^4)

Various examples

Numbers and numeric operators

1
2
3
4
5
6
7
8
9
age:30
age:>=10
age:<10
age:<=10
age:(>=10 AND <20)
age:(+>=10 +<20)
age:[1 TO 5]
age:[1 TO 5}
age:[10 TO *]

Dates

1
2
3
4
5
date:2012-01-01
date:[2012-01-01 TO 2012-12-31]
date:[now-2d/d TO now]
date:{* TO 2012-01-01}
See more on date math here.

Strings

1
2
3
4
5
6
7
8
author:"John Smith"
title:(quick brown)
title:(quick OR brown)
title:((quick OR brown) AND fox AND NOT news)
title:(quick AND brown)
title:(qu?ck bro*)
title:(quikc~ brwn~ foks~)
name:/joh?n(ath[oa]n)/

Conclusion

That’s it for the Lucene Query Language expressions that I use the most. Which ones do you use the most ?

Let me know in the comments below 😉

See you soon !
Keep learning !

Contents

  1. 1. Introduction
  2. 2. General operators
  3. 3. Various examples
    1. 3.1. Numbers and numeric operators
    2. 3.2. Dates
    3. 3.3. Strings
  4. 4. Conclusion