Lucene4.8 Tutorial Six Queryparser and query subclasses: How to build a query object

Source: Internet
Author: User
Tags java web java keywords


I. Overview

1. For a search, its core statements are:

Searcher.search (query, 10);
At this point, the most important parameter is a Qeury object. There are 2 ways to construct a query object:

(1) Use the subclass of query to create an object.

(2) Create an object using Queryparser.parse ().

The common query subclass for instantiating an object has the following 13:
Termquery
Booleanquery
Wildcardquery
Phrasequery
Prefixquery
Multiphrasequery
Fuzzyquery
Regexpquery
Termrangequery
Numericrangequery
Constantscorequery
Disjunctionmaxquery
Matchalldocsquery

2. Quick Start

"All to search for Java keywords in the contents domain for example"

(1) using the subclass of query, directly instantiate an external:

Searcher.search (New Termquery ("Contents", "Java") (new Term), 10);

The following statement structure is more clear

Term term= New Term ("Contents", "Java");
Termquery TQ = new Termquery (term);
Searcher.search (TQ, 10);

In addition, search for documents that include Java in the contents domain.

(2) Use the Queryparser Parse () method to interpret the incoming search keyword and return to the query object.

Queryparser parser = new Queryparser (version.lucene_48, "contents", New Simpleanalyzer (version.lucene_48));
query query = NULL;
try {
	query = parser.parse ("java");
} catch (ParseException e) {
	e.printstacktrace ();
}
Topdocs docs = searcher.search (query, 10);
The above statement creates a queryparser whose default search field is contents, and then converts the search term into a query object.

If the default search domain for the specified queryparser is all. How to specify a search field for query.


Second, Termquery

1. How to create

The termquery is used in this quick start.

2. Scope of application

(1) Direct use of termquery, do not make any analysis of the search terms, including the case is not converted, and the use of Queryparser, you can be based on the type of analyzer analysis.

(2) Termquery suitable for full matching search, such as search ID number, two-dimensional code, name and so on.


Third, Booleanquery

1. How to create

(1) Using Booleanquery

Multiple search conditions make up the Booleanquey object, which later complements the example.

(2) using Queryparser

For a queryparser, if it does not specify what logical operation it is, it defaults to operator.or. Therefore, if you search the Java Web, it will consider the Java OR Web.

For Chinese, each word is used as a word when using the Standartanalyzer parser. If you search for "learning", search for "Learn OR".

Therefore, to specify a search for "learning" or "Java Web", you must display the specified logical operation as Operator.and.

Queryparser parser = new Queryparser (version.lucene_48, "Contents",
				new StandardAnalyzer (version.lucene_48));
Parser.setdefaultoperator (Operator.and);
Query Query  = parser.parse (term);

2. Scope of application

(1) Booleanquery method, because the search content needs to generate multiple query objects, and then logical combination, therefore, only applicable to multiple search criteria for the case. Such as:

Select multiple filter criteria on the above page, then each filter condition generates a Query object, and then the individual query objects are logically grouped.

(2) Queryparser mode, suitable for a single search box, the use of natural language to search the situation. such as Google, Baidu search box.


Four, Phrasequery

1. Simple description

When you enclose search keywords in double quotes, you search for a phrase that is a whole. Such as:




The former has only 534 return results, and the latter more than 2 million returns results. For the former, you will search for documents with less than a specific slop value between Java and Sunshine.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.