suppose you are searching the web for pages that contain both the words java and net but not the word dot. what if search engines made you type in something like the following for this simple query?
java and net not dotthis article assumes you have some basic understanding of lucene, including building and querying an index. you may want to take a look back at the lucene intro published in july on java.net. the focus here is on lucenes queryparser, providing an in-depth look at its capabilities and caveats. 【程序编程相关:你知道数据大小吗?】
booleanquery query = new booleanquery(); query.add(new termquery(new term("contents","java")), true, false); query.add(new termquery(new term("contents", "net")), true, false); query.add(new termquery(new term("contents", "dot")), false, true);that would be a real drag. thankfully, google, nutch, and other search engines are friendlier than that, allowing you to enter something much more succinct: 【推荐阅读:小议局部类】
using queryparser 【扩展信息:BeanShell】
first well see what is involved to use queryparser in an application. then, lucenes query api is shown in relation to the corresponding queryparser syntax. elaboration on the details of queryparser syntax is then followed by how queryparsers features can be customized.
using queryparser is quite straightforward. three things are needed: an expression, the default field name to use for unqualified fields in the expression, and an analyzer to pieces of the expression.
field-selection qualifiers are discussed in the query syntax section. analysis, specific to query parsing, is covered in the "analysis paralysis" section. for a more detailed look at lucene analyzers, refer to the lucene intro article.
now, lets parse an expression:
string humanquery = gethumanquery(); query query = queryparser.parse(humanquery, "contents", new standardanalyzer());once youve obtained a query object, searching is done the same as if the query had been created directly through the api. from the intro article, here is a full method to search an existing index with a user-entered query string, and display the results to the console:
... 下一页