- Lucene 4 Cookbook
- Edwood Ng Vineeth Mohan
- 421字
- 2021-07-16 14:07:49
Creating queries with the Lucene QueryParser
Now, we understand that we need to create Query objects to perform a search. We will look at QueryParser and show you how it's done. Lucene supports a powerful query engine that allows for a wide range of query types. You can use search modifier or operator to tell Lucene how matches are done. You can also use fuzzy search and wild card matching.
Internally, Lucene processes Query objects to execute a search. QueryParser is an interpreter that parses a query string into Query objects. It provides the utility to convert textual input into objects. The key method in QueryParser is parse (String). If you want more control over how a search is performed, you can create Query objects directly without using QueryParser, but this would be a much more complicated process. The query string syntax Lucene uses has a few rules. Here is an excerpt from Lucene's Javadoc: https://lucene.apache.org/core/4_10_0/queryparser/org/apache/lucene/queryparser/classic/QueryParser.html.
The syntax for query strings is as follows: a Query is a series of clauses. A clause can be prefixed by:
- A plus (+) or minus (-) sign, indicating that the clause is required or prohibited, respectively.
- Alternatively, a term followed by a colon, indicating the field to be searched. This enables us to construct queries that search multiple fields.
A clause can be:
- A term, indicating all the documents that contain this term.
- Alternatively, a nested query, enclosed in parentheses. Note that this can be used with a+/- prefix to require any of the set of terms.
Thus, in BNF, the query grammar is:
Query ::= ( Clause )* Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
Note
Note that you need to import lucene-queryparser
package to use QueryParser. It is not a part of the lucene-core package.
The Backus Normal Form (BNF) is a notation technique to specify syntax for a language, and is often used in computer science.
How to do it...
Here is a code snippet:
QueryParser parser = new QueryParser("Content", analyzer); Query query = parser.parse("Lucene");
How it works…
Assuming an analyzer is already declared and available as a variable, we pass it into QueryParser to initialize the parser. The second parameter is the name of the field where we will perform a search. In this case, we are searching a field called Content. Then, we call parse(String) to interpret the search string Lucene into Query object. Note that, at this point, we only return a Query object. We have not actually executed a search yet.
- Python量化投資指南:基礎(chǔ)、數(shù)據(jù)與實戰(zhàn)
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Reactive Programming with Swift
- 數(shù)據(jù)結(jié)構(gòu)(Java語言描述)
- 我的第一本算法書
- 軟件測試項目實戰(zhàn)之性能測試篇
- Mastering Scientific Computing with R
- Spring Cloud、Nginx高并發(fā)核心編程
- SQL基礎(chǔ)教程(第2版)
- jQuery炫酷應(yīng)用實例集錦
- Unity&VR游戲美術(shù)設(shè)計實戰(zhàn)
- Machine Learning With Go
- Vue.js應(yīng)用測試
- Mastering Concurrency in Python
- 人人都能開發(fā)RPA機器人:UiPath從入門到實戰(zhàn)