0. Introduction to Grammar:
1. Common part code same as seven
//Indexreader Indexsearcher PublicIndexsearcher Getindexsearcher ()throwsException {//The first step: Create a Directory object, which is where the index inventory is placed. Directory directory = Fsdirectory.open (NewFile ("E:\\lucene&solr\\index"));//Disk//Step Two: Create a Indexreader object, you need to specify the directory object. Indexreader Indexreader =directoryreader.open (directory); //Step Three: Create a Indexsearcher object, you need to specify the Indexreader object return NewIndexsearcher (Indexreader); } //execute the results of the query Public voidPrintresult (indexsearcher indexsearcher, query query)throwsException {//Fifth Step: Execute the query. Topdocs Topdocs = indexsearcher.search (query, 10); //Sixth step: Return the query results. Traverse the query results and output. scoredoc[] Scoredocs =Topdocs.scoredocs; for(Scoredoc scoredoc:scoredocs) {intDoc =Scoredoc.doc; Document Document=Indexsearcher.doc (DOC); //file nameString filename = document.get ("filename"); System.out.println (FileName); //File ContentsString filecontent = Document.get ("Filecontent"); System.out.println (filecontent); //File SizeString fileSize = Document.get ("FileSize"); System.out.println (fileSize); //file pathString FilePath = Document.get ("FilePath"); System.out.println (FilePath); System.out.println ("------------"); } }
2. Query all: (parser will be the query criteria for word breaker)
Syntax: *:*
//object query for conditional interpretation@Test Public voidTestqueryparser ()throwsException {indexsearcher indexsearcher=Getindexsearcher (); //parameter 1: domain of the default query//Parameter 2: The parser usedQueryparser Queryparser =NewQueryparser ("FileName",NewIkanalyzer ()); //*:* Domain: ValueQuery query = Queryparser.parse ("*: *"); Printresult (indexsearcher, query); //Close ResourceIndexsearcher.getindexreader (). Close (); }
3. Domains using the default query
Querying for documents with computer index names
@Test Public voidTestqueryparser ()throwsException {indexsearcher indexsearcher=Getindexsearcher (); //parameter 1: domain of the default query//Parameter 2: The parser usedQueryparser Queryparser =NewQueryparser ("FileName",NewIkanalyzer ()); //*:* Domain: ValueQuery query = queryparser.parse ("Computer"); Printresult (indexsearcher, query); //Close ResourceIndexsearcher.getindexreader (). Close (); }
Results:
Do Do Do shopping in a Supermarke336E:\lucene&solr\searchfiles\computer.txt------------
4. Scope Query
Range Query not supported
5. Combination query ( combined query only with modify syntax, + indicates must,-indicates must not have, nothing to indicate dispensable )
Query filename must be in Java and must not have a document with struts.
Grammar +filename:java-filename:struts
//object query for conditional interpretation@Test Public voidTestqueryparser ()throwsException {indexsearcher indexsearcher=Getindexsearcher (); //parameter 1: domain of the default query//Parameter 2: The parser usedQueryparser Queryparser =NewQueryparser ("FileName",NewIkanalyzer ()); //*:* Domain: ValueQuery query = queryparser.parse ("+filename:java-filename:struts"); Printresult (indexsearcher, query); //Close ResourceIndexsearcher.getindexreader (). Close (); }
Results:
Do When happy or play wit309E:\lucene&solr\searchfiles\java Basic. txt------------ Java advanced. txt???? Java????????????????????? E:\lucene&Solr\searchfiles\java advanced. txt------------
6. Complex queries
Query fileName with javaweb or computer, or filecontent with javaweb or computer
The Ikanalyzer parser first participles the Java is Apach, then the javaweb is computer, and then queries the document with the word after the parser word based on the condition.
//object query for conditional interpretation@Test Public voidTestqueryparser ()throwsException {indexsearcher indexsearcher=Getindexsearcher (); //parameter 1: domain of the default query//Parameter 2: The parser usedQueryparser Queryparser =NewQueryparser ("FileName",NewIkanalyzer ()); //*:* Domain: ValueQuery query = Queryparser.parse ("Filename:javaweb is computer OR filecontent:javaweb is Computer"); Printresult (indexsearcher, query); //Close ResourceIndexsearcher.getindexreader (). Close (); }
Results:
load Extension Dictionary: Ext.dic Load Extension stop dictionary: Stopword.diccomputer.txt?? Computers is changing our life. You can DoA lot of things with a computer. Such as, can use a computer to write articles, watch video CDs, play games and DoOffice work. But the most important use of a computer are to join the Internet.we don?? t need to leave home to borrow books from a library or to Doshopping in a Supermarke336E:\lucene&Solr\searchfiles\computer.txt------------1javaweb. txt Thisis javaweb dsbadfsabjkfsdf njdfndsj Njaj Spring53E:\lucene&solr\searchfiles\1javaweb. txt------------
querying multiple meditation domains--------------------conditional-resolved objects-----------------------
7. Parser with Multifieldqueryparser
Query fileName with javaweb or computer, or filecontent with Javaweb or computer ( equivalent to 6 function )
Does not work very much, in the query condition one uses the query domain default query domain to be obsolete.
//conditional-Resolved object queries multiple meditation domains@Test Public voidTestmultifieldqueryparser ()throwsException {indexsearcher indexsearcher=Getindexsearcher (); String[] Fields= {"FileName", "Filecontent" }; //parameter 1: domain of the default query//Parameter 2: The parser usedMultifieldqueryparser Queryparser =NewMultifieldqueryparser (Fields,NewIkanalyzer ()); //*:* Domain: ValueQuery query = Queryparser.parse ("Javaweb is Computer"); Printresult (indexsearcher, query); //Close ResourceIndexsearcher.getindexreader (). Close (); }
Summary:
This can set the query condition as a sentence, theikanalyzer parser will process the sentence word processing, and then the query index.
// parameter 1: domain of the default query // Parameter 2: The parser used New New Ikanalyzer ()); // *:* Domain: value Query query = Queryparser.parse ("Filename:java is Apache OR filecontent:computers be changing our life");
Lucene Query Index parsing query--(eight)