Lucene Exploration-Query

Source: Internet
Author: User

Lucene is intended for the back of SOLR. If you skip Lucene and go straight to SOLR, you are probably a little bit ignorant.

Due to the time of the relationship, Lucene query method also has a number, so separate out.

I. EXACT query

     /*** Get Find Object *@return     * @throwsException*/    PrivateIndexsearcher Getsearcher ()throwsException {//1. Create a directory object, which is where the index inventory is placedDirectory directory = Fsdirectory.open (NewFile (Indexdir)); //2. Create a Indexreader object, you need to specify the directoryIndexreader Indexreader =directoryreader.open (directory); //3. Create a Indexsearcher object, you need to specify the Indexreader objectIndexsearcher Indexsearcher =NewIndexsearcher (Indexreader); returnIndexsearcher; }     /*** Output information to the console *@paramIndexsearcher *@paramQuery *@throwsException*/     Public voidSout (indexsearcher indexsearcher, query query)throwsException {//5. Execute the queryTopdocs Topdocs = indexsearcher.search (query, 5); //6. Returning Query Resultsscoredoc[] Scoredocs =Topdocs.scoredocs;  for(Scoredoc scoredoc:scoredocs) {//Get Document ID            intDoc =Scoredoc.doc; //get documents based on document IDDocument document =Indexsearcher.doc (DOC); //file nameString filename = document.get ("filename"); //File SizeString fileSize = Document.get ("FileSize"); //file pathString FilePath = Document.get ("FilePath"); //File ContentsString filecontent = Document.get ("Filecontent"); System.out.println ("FileName:" +fileName); System.out.println ("FileSize:" +fileSize); System.out.println ("FilePath:" +FilePath); System.out.println ("Filecontent:" +filecontent); System.out.println ("-----------------------"); }    }     /*** Exact Query * *@throwsException*/@Test Public voidSearchindex ()throwsException {        //1. Get the Query ObjectIndexsearcher Indexsearcher =Getsearcher (); //2. Create a Termquery object that specifies the domain of the query and the keyword of the queryQuery query =New Termquery(NewTerm ("FileName", "Life"));        Sout (indexsearcher, query); //3. Close the Indexreader objectIndexsearcher.getindexreader (). Close (); }

In the query, create a new Term object, enter the exact match. As mentioned in the previous article, each word or passage that is divided by a word breaker is a term.

Here in the new term, the incoming domain name and the word to search.

Here, a term object, there is only one domain, then what if I want to query multiple domains?

Two. Combination Query

/*** Combination Query*/@Test Public voidQueryboolean ()throwsException {Indexsearcher searcher=Getsearcher (); booleanquery Query=NewBooleanquery (); Query Query1=NewTermquery (NewTerm ("FileName", "Life")); Query Query2=NewTermquery (NewTerm ("Filecontent", "Life"));    Query.add (Query1, BooleanClause.Occur.MUST);    Query.add (Query2, BooleanClause.Occur.SHOULD); //System.out.println (query);sout (searcher, query); Searcher.getindexreader (). Close ();}

Here the occur enumeration values, there are three, must, should, Must_not.

Must: equivalent to and connected to SQL

Should: equivalent to or, can have no

Must_not: Equals! =, does not contain

If you print query here, it will show: +filename: Life filecontent: Life

This is a syntax for Lucene, and Lucene can query the data according to the syntax. will be mentioned later. If Must_not, a minus sign is used.

Such as: The above query2 using Must_not connection, then displayed as: +filename: Life-filecontent: Life

Three. Query all

When querying a database generally, a getAll method is provided to query all data that satisfies the condition, and when the condition is not passed, it queries all

Lucene also provides a way to query all of the methods: Matchalldocsquery

/**@throws */@Test public void throws Exception {    = getsearcher ();     New  matchalldocsquery();    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

Four. Numeric interval query

/**@throws */@Test public void throws Exception {    = getsearcher ();     Numericrangequery true true );    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

The syntax output here is: filesize:[40 to 647]

This is because the last two of me are set to True, which indicates the containment relationship. If all is set to False, it is {647}

Five. Word breaker parsing query

As mentioned earlier, I entered a sentence query, the results of the show is not according to my input of the full match results.

That is because before the query, the input information, the word breaker parsing, and then based on the results of the analysis, and then to query the data.

/*** Conditional Parse Object query * *@throwsException*/@Test Public voidQueryparser ()throwsException {Indexsearcher searcher=Getsearcher (); queryparser Queryparser=NewQueryparser ("FileName",NewIkanalyzer ()); //query query = Queryparser.parse ("*:*");Query query = Queryparser.parse ("FileName: This flower is so beautiful"); //query query = queryparser.parse ("flower");sout (searcher, query); Searcher.getindexreader (). Close ();}

*:* means query all. Regardless of which domain.

FileName: This flower is beautiful: in the filename field, the word "This flower is nice" is parsed and queried

Flowers: In the filename field, query for flowers. Because when Queryparse was created, the domain was named FileName

Even if I specify the domain to query in Queryparser, I can reassign the domain in parse.

It is important to note that in the above value range query, if I directly write grammar into the query, is not found out. Because the numeric type has changed. Passed through the syntax into a string type.

From the results can be seen, I input this flower is very beautiful, find out is the army of green flowers. This is the role of participle.

Six. Multi-domain Word segmentation query

/*** Conditional Parse Object query * *@throwsException*/@Test Public voidQuerymultiparser ()throwsException {Indexsearcher searcher=Getsearcher (); String[] Fields= {"FileName", "Filecontent"}; multifieldqueryparser Queryparser=NewMultifieldqueryparser (Fields,NewIkanalyzer ()); Query Query= Queryparser.parse ("Life Big Bang");    Sout (searcher, query); Searcher.getindexreader (). Close ();}

Multi-domain Word search, there is nothing to say.

Lucene Exploration-Query

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.