Lucene Getting Started Query index--(III)

Source: Internet
Author: User
Tags solr

1. User interface (Lucene does not provide)

2. Create a query

3. Execute the Query

4. Rendering Results:

5. Process Analysis

Query the contents of the index library according to the keyword:

1) Create Indexsearcher Object

2) Create Queryparser Object

3) Create a query object to encapsulate the keywords

4) Use the Indexsearcher object to search the index library for the first 100 records that meet the criteria, less than 100 records, whichever is the actual

5) Get the qualifying number

6) Use the Indexsearcher object to index the document object corresponding to the query number in the library

7) Remove all the attributes from the Document object, and then encapsulate them back into the JavaBean object and save them in the collection for use

Indexsearcher Object Search Method:

6. Code implementation:
1     //Search Index2 @Test3      Public voidTestsearch ()throwsException {4         //The first step: Create a Directory object, which is where the index inventory is placed. 5Directory directory = Fsdirectory.open (NewFile ("E:\\lucene&solr\\index"));//Disk6         //Step Two: Create a Indexreader object, you need to specify the directory object. 7Indexreader Indexreader =directoryreader.open (directory);8         //Step Three: Create a Indexsearcher object, you need to specify the Indexreader object9Indexsearcher Indexsearcher =NewIndexsearcher (indexreader);Ten         //Fourth Step: Create a Termquery object that specifies the domain of the query and the keyword of the query.  OneQuery query =NewTermquery (NewTerm ("FileName", "Java")); A         //Fifth Step: Execute the query.  -Topdocs Topdocs = indexsearcher.search (query, 10); -         //Sixth step: Return the query results. Traverse the query results and output.  thescoredoc[] Scoredocs =Topdocs.scoredocs; -          for(Scoredoc scoredoc:scoredocs) { -             intDoc =Scoredoc.doc; -Document document =Indexsearcher.doc (DOC); +             //file name -String filename = document.get ("filename"); + System.out.println (fileName); A             //File Contents atString filecontent = Document.get ("Filecontent"); - System.out.println (filecontent); -             //File Size -String fileSize = Document.get ("FileSize"); - System.out.println (fileSize); -             //file path inString FilePath = Document.get ("FilePath"); - System.out.println (filePath); toSystem.out.println ("------------"); +         } -         //Seventh Step: Close the Indexreader object the indexreader.close (); *  $}

Results:

Java Struts.txt Think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene1\searchfiles\java struts.txt------------Java Struts-. txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene&solr\searchfiles\java Struts-. txt------------Java Struts.txt Think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene&Solr\searchfiles\java struts.txt------------Java struts Springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene&Solr\searchfiles\java Struts Springmvc.txt------------Java struts Spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene&Solr\searchfiles\java Struts Spring.txt------------

Put the fifth step above the

Topdocs Topdocs = indexsearcher.search (query, ten);
Topdocs Topdocs = indexsearcher.search (query, 2);


Result: Output only two records found
Java Struts.txt Think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene1\searchfiles\java struts.txt------------Java Struts-. txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and has a good day. If you aren?? T happy, you can smile, and then you'll feel happy. Someone may say,?? But I don?? T feel happy.?? Then I would say,?? Smile As you DoWhen you are happy or play wit309E:\lucene&solr\searchfiles\java Struts-. txt------------

Lucene Getting Started Query index--(III)

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.