Lucene Query sub-class

Source: Internet
Author: User

Queryparser (single domain query)

The Queryparser subclass creates query queries for a single domain query, and constructs a method that requires an incoming Lucene version number to retrieve the domain name and the word breaker.

new QueryParser(Version.LUCENE_43, field, analyzer);// 查询字符串Query query = parser.parse("key");
Multifieldqueryparser (multi-domain query)
new MultiFieldQueryParser(Version.LUCENE_43, fields, analyzer);Query query2 = parser2.parse(key);
Termquery (single key field query)
TermQuery tq=new TermQuery(newkey));
Prefixquery (prefix character query)

Prefixquery only need to specify a number of characters prefix prestr, prestr start will be matched, such as "Lu" can Match "Luke", "Lucene".

 PrefixQuery prefixQuery=new PrefixQuery(new Term("field,key));
Phrasequery (phrase query)
  PhraseQuery phraseQuery=new PhraseQuery();  phraseQuery.setSlop(3);//关键词间距离  phraseQuery.add(new"key1"));//关键词1  phraseQuery.add(new"key2"));//关键词2

The above query instance can match the keyword Key1 and the key2 spacing is not more than 3 of the document, field for the query domain.

Wildcardquery (wildcard query)
WildcardQuery wildcardQuery=new WildcardQuery(new"基于?"));

Match 1 characters, * match any number of characters.

Termrangequery (String range Search)
TermRangeQuery rangeQuery=new TermRangeQuery(field, lowerTerm, upperTerm, includeLower, includeUpper);

The five parameters are the domain name, the lower limit of the field, the upper limit of the field, whether the lower limit is included, and whether the upper limit is included. Like what:

TermRangeQuery rangeQuery=new"aab""azz"truefalse);
Numericrangequery (Digital range Search)
Query q = NumericRangeQuery.newFloatRange("weight"0.03f0.10ftruetrue);

Matches all documents whose float valued ' weight ' field ranges from 0.03 to 0.10, inclusive.

Booleanquery (Boolean query)

Multiple query objects grouped into one object

BooleanQuery bQuery=new BooleanQuery();            bQuery.add(new TermQuery(new Term("title""lucene")), Occur.MUST);            bQuery.add(new TermQuery(new Term("content""基于")), Occur.SHOULD);            bQuery.add(new TermQuery(new Term("name""java")), Occur.MUST_NOT);

The above example is used to query that the title must contain "Lucene" (and) or that the cotent contains "based on" (or) but the name does not necessarily contain "Java" (not) of the document.

Lucene Query sub-class

Related Article

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.