(20:40:15)
Reprinted
Lucene:
1. Example: Eclipse help;
2. Create an index: only text data can be indexed.
HTML (remove tags), PDF, etc. can be converted to text using corresponding tools;
3. Principle: Dictionary
Key ---> recordnum
AB ---> 5, 7, 10
Document;
5
7
10
4. Use:
4.1 create a Java project;
4.2 Add *. jar; [No configuration file] the current version is 2.4;
Core; Word divider; Highlight;
4.3 write code test;
Firsttest
Stringindexpath = "C:/javasedemoindex /";
Analyzeranalyzer = new standardanalyer ();
@ Test
Testcreateindex (){
Stringtitle = "XXX ";
Stringcontent = "Yyy ";
// 1. Create an index
Maxfieldlengthmaxfieldlength = maxfieldlength. Limited;
// If the index library does not exist, it is created;
Indexwriterindexwriter = new indexwriter (indexpath, analyzer, maxfieldlength );
// Document, Field
Documentdoc = new document ();
Doc. Add (Newfield ("title", title, store. Yes, index. Analyzed ));
Doc. Add (Newfield ("content", content, store. Yes, index. Analyzed ));
Indexwriter. adddocument (DOC );
// Close after use
Indexwriter. Close ();
}
Voidtestsearch (){
Stringquerystring = "document ";
Indexsearcherindexsearcher = new indexsearcher (indexpath );
Stringdefafielfieldname = "content ";
Queryparserqueryparser = new queryparser (defaultfieldname, analyzer );
QueryQuery= Queryparser. parse (querystring ); // Query Conditions
FilterFilter= NULL; // Filter Condition
IntNdocs= 100; // Returns the number of matched results.
// Return results
Topdocstopdocs = indexsearcher. Search (query, filter, ndocs );
System. Out. println ("total [" + topdocs. totalhits + "matching records ");
List <document> docs = newarraylist <document> ();
For (scoredoc: topdocs. scoredocs ){
Intdocnum = scoredoc.doc; // Document ID in the index Library
Documentdoc = indexsearcher.doc (docnum); // retrieve the corresponding document by serial number
Doc. Add (DOC );
}
Indexsearcher. Close ();