Advanced query of Lucene search

Source: Internet
Author: User

    • Use Query sub-class queries

Matchalldocsquery

Termquery

Numericrangequery

Booleanquery

    • Use Queryparser

Queryparser

Mulitfieldqueryparser

Extract the public code first

Private voidDosearch (query query)throwsIOException {//give the index library locationFsdirectory directory = Fsdirectory.open (NewFile ("E:\\upload\\lucene")); //Create IndexreaderDirectoryreader reader =indexreader.open (directory); //Create a Search objectIndexsearcher searcher =NewIndexsearcher (reader); Topdocs Topdocs= Searcher.search (Query, 10);        System.out.println (query); Scoredoc[] Scoredocs=Topdocs.scoredocs; System.out.println (topdocs.totalhits); //number of hits in query         for(Scoredoc scoredoc:scoredocs) {intid = scoredoc.doc;//ID of the documentDocument doc = Searcher.doc (ID);//Get Doc ObjectSystem.out.println (Doc.get ("id")); System.out.println (Doc.get ("Name"));//System.out.println (Doc.get ("price"));//System.out.println (Doc.get ("pic"));//System.out.print (Doc.get ("description"));} reader.close (); }

The first matchalldocequery, is the full match, the matching syntax is *:*

@Test      Public void  throws Exception {// Create query Object        new  matchalldocsquery ();        Dosearch (query);    }

The second one is Termquery, which is the exact match, and the following matching syntax is: Description: Project

New Termquery (new term ("description", "project"));
Dosearch (query);

A third numericrangequery, a number match, a range of expressions

First parameter: Name of the domain

Second parameter: Minimum value

Third parameter: Maximum value

Fourth parameter: Whether the minimum value is included

Fifth parameter: Whether the maximum value is included

domain +":"+[ value to value ] Represents a range of values and includes numeric values. If the value is not included with "{}"

Like what:

price:[55.0 to 70.0] equals 55=< price <=70

price:{55.0 to 70.0] equals < price <=70

@Test      Public void  throws Exception {// Create query Object      truetrue);    Dosearch (price);    }

The fourth one is booleanquery, multi-criteria query

1.must and must indicate the relationship between "and", that is, "intersection".

2,must and must_not The former contains the latter does not contain.

3,must_not and must_not no meaning

4,should and must said must,should lost meaning;

5,should and must_not equivalent to must and must_ not.

     6, should should

  @Test  public  void  dobooleanquery () throws  Exception { //   booleanquery query  = new   Booleanquery (); Termquery query1  = new  termquery (new         term ("name", "Java"  <Float> query2 = Numericrangequery.newfloatrange ("Price", 55F, 56F, true , true  );        Query.add (Query1, BooleanClause.Occur.SHOULD);        Query.add (Query2, BooleanClause.Occur.SHOULD);    Dosearch (query); }

the subclass of query does not need to specify a word breaker and cannot specify the syntax of the query

Queryparser you need to specify a word breaker, and you can specify the syntax of the query

Fifth one Queryparse

Termquery is a full-match search, if you want to search for a piece of text, you need to match the whole line, if you want to search for the words contained in this text, you need to the text word. In order to search. queryparser can specify a word breaker, and then parse, query.

@Test Public voidDoqueryparse ()throwsException {//Create a Query objectAnalyzer analyzer=NewIkanalyzer (); Queryparser Parser=NewQueryparser ("name", analyzer);//query query = Parser.parse ("*:*");//query query = Parser.parse ("Lucene java");//query query = Parser.parse ("Name:java and Name: Programming");Query query = Parser.parse ("+name:java +name: Programming");    Dosearch (query); }

Sixth Multifieldqueryparser, multiple domain matching possible

@Test      Public void  throws Exception {        Analyzer i=new  Ikanalyzer ();        String[]fields ={"name", "description"};         New multifieldqueryparser (fields, i);         = Queryparser.parse ("Lucene");        Dosearch (query);    }

The query syntax for numeric range classes is not supported in Queryparser . There is no error in the syntax. It can be queried in SOLR.

Combination Criteria Query

Occur.must query conditions must be met, equivalent to and

+(plus)

Occur.should query conditions are optional, equivalent to or

Empty (without symbols)

Occur.must_not query condition is not satisfied, equivalent to not non-

-(minus)

1)+ condition 1 + condition 2: The relationship between two conditions is and and

Example:+filename:apache +content:apache

2) + condition 1 condition 2: First condition must be met, second condition ignored

Example:+filename:apache Content:apache

3) conditions 1 conditions 2: Two conditions to satisfy one of them.

Example:filename:apache Content:apache

4)- condition 1 condition 2: Must not meet the condition 1, to satisfy the condition 2

Example:-filename:apache Content:apache

The second way:

condition 1 and condition 2

condition 1 OR condition 2

condition 1 not condition 2

Attention:

the Search method needs to specify the number of matching records n:indexsearcher.search (query, N)

Topdocs.totalhits: Is the number that matches all records in the index library

Topdocs.scoredocs: Match the high correlation of the front record array, the length of scoredocs is less than equal to the search method specified by the parameter N

Advanced query of Lucene search

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.