The code to create the index is as follows:
PackageCom.search.lucene;ImportJava.io.File;ImportOrg.apache.lucene.analysis.Analyzer;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field.Store;ImportOrg.apache.lucene.document.StringField;ImportOrg.apache.lucene.document.TextField;ImportOrg.apache.lucene.index.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.FSDirectory;Importorg.apache.lucene.util.Version;ImportOrg.junit.Before;Importorg.junit.Test; Public classIndexfile {protectedString[] ids={"1", "2"}; protectedString[] content={"Amsterdam have lost of add cancals", "I love Add this girl"}; protectedString[] city={"Amsterdam", "Venice"}; PrivateDirectory dir; /*** Initial Add document *@throwsException*/@Test Public voidInit ()throwsException {String pathfile= "D://lucene/index"; Dir=fsdirectory.open (NewFile (pathfile)); IndexWriter writer=getwriter (); for(inti=0; i < ids.length; i++) {Document doc=NewDocument (); Doc.add (NewStringfield ("id", Ids[i], store.yes)); Doc.add (NewTextField ("Content", Content[i], store.yes)); Doc.add (NewStringfield ("City", City[i], store.yes)); Writer.adddocument (DOC); } System.out.println ("Init OK?"); Writer.close (); } /*** Get IndexWriter Object *@return * @throwsException*/ PublicIndexWriter getwriter ()throwsException {Analyzer Analyzer=NewStandardAnalyzer (version.lucene_40); Indexwriterconfig IWC=NewIndexwriterconfig (version.lucene_40, analyzer); return NewIndexWriter (dir, IWC); }}
Description under: lucene4.0 has core packages and other packages: I import
PackageCom.search.lucene;ImportJava.io.File;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.index.DirectoryReader;ImportOrg.apache.lucene.index.IndexReader;Importorg.apache.lucene.index.Term;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.ScoreDoc;ImportOrg.apache.lucene.search.TermQuery;ImportOrg.apache.lucene.search.TopDocs;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.FSDirectory;Importorg.junit.Test; Public classIndexsearch {/*** Enquiry *@throwsException*/@Test Public voidSearch ()throwsException {String FilePath= "D://lucene/index"; Directory dir=fsdirectory.open (NewFile (FilePath)); Indexreader Reader=Directoryreader.open (dir); Indexsearcher Searcher=NewIndexsearcher (reader); term=NewTerm ("content", "add"); Termquery Query=NewTermquery (term); Topdocs Topdocs=searcher.search (Query, 5); Scoredoc[] Scoredocs=Topdocs.scoredocs; System.out.println ("Total number of query results---" + topdocs.totalhits+ "max rating--" +Topdocs.getmaxscore ()); for(inti=0; i < scoredocs.length; i++) { intDoc =Scoredocs[i].doc; Document Document=Searcher.doc (DOC); System.out.println ("content====" +document.get ("content")); System.out.println ("id--" + Scoredocs[i].doc + "---scors--" + scoredocs[i].score+ "---index--" +Scoredocs[i].shardindex); } reader.close (); }}
Result
Total Query Results---2 maximum rating--0.2972674
Content====i love this girlid--1---scors--0.2972674---index---1content= = Amsterdam has lost of add cancalsid--0---scors--0.26010898---index---1
Lucene Simple Example