Lucene is a framework for full-text search, and the Apache organization provides an open source project for full-text search engines implemented in Java. Here we make a brief introduction to Apache's Lucene framework. willingly these four words, reveals a humble, but also has the courage which cannot hide.
First instance of Lucene, dependency Pom.xml introduced in Maven
<Properties> <lucene.version>6.6.0</lucene.version> <common-io.version>2.5</common-io.version></Properties><Dependencies> <!--the reliance of Lucene core - <Dependency> <groupId>Org.apache.lucene</groupId> <Artifactid>Lucene-core</Artifactid> <version>${lucene.version}</version> </Dependency> <Dependency> <groupId>Org.apache.lucene</groupId> <Artifactid>Lucene-queryparser</Artifactid> <version>${lucene.version}</version> </Dependency> <!--Commomsio's Dependence - <Dependency> <groupId>Commons-io</groupId> <Artifactid>Commons-io</Artifactid> <version>${common-io.version}</version> </Dependency></Dependencies>
Second, the Java code for Lucene is as follows
Packagecom.linux.huhx.lucene_1;Importorg.apache.commons.io.FileUtils;ImportOrg.apache.lucene.analysis.Analyzer;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field;ImportOrg.apache.lucene.document.StringField;ImportOrg.apache.lucene.document.TextField;ImportOrg.apache.lucene.index.DirectoryReader;ImportOrg.apache.lucene.index.IndexReader;ImportOrg.apache.lucene.index.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;ImportOrg.apache.lucene.queryparser.classic.QueryParser;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.Query;ImportOrg.apache.lucene.search.ScoreDoc;ImportOrg.apache.lucene.search.TopDocs;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.RAMDirectory;ImportJava.io.File;Importjava.io.IOException; Public classBaselucene_1 { Public Static voidMain (string[] args)throwsException {//Store in memoryDirectory dir =Newramdirectory (); Newbaselucene_1 (). CreateIndex (dir); Newbaselucene_1 (). Search (dir); } /*** Create an index*/ Public voidCreateIndex (Directory dir)throwsIOException {indexwriter indexwriter=NULL; //1. Create Directory (Index storage location), here is the parameter dir//2. Create IndexWriter Write IndexAnalyzer Analyzer =NewStandardAnalyzer (); Indexwriterconfig IWC=NewIndexwriterconfig (Analyzer); IndexWriter=NewIndexWriter (dir, IWC); //3. Create a Document Object fielddocument document; File File=NewFile ("File/example"); for(File f:file.listfiles ()) {document=NewDocument (); //4. Add field to DocumenDocument.add (NewField ("Content", fileutils.readfiletostring (F, "Utf-8")) , textfield.type_stored)); Document.add (NewTextField ("FileName", F.getname (), Field.Store.YES)); Document.add (NewStringfield ("FilePath", F.getabsolutepath (), Field.Store.YES)); //5. Adding documents to the index via IndexWriterindexwriter.adddocument (document); } indexwriter.close (); } /*** Search by content *@paramdir *@throwsException*/ Public voidSearch (Directory dir)throwsException {indexreader Indexreader=NULL; //1. Create the Directory, where the dir is passed through the parameters//Directory dir = fsdirectory.open (new File ("File/index"). Topath ());//Local Disk//2. Create IndexreaderIndexreader =Directoryreader.open (dir); //3. Create IndexsearchIndexsearcher Indexsearcher =NewIndexsearcher (Indexreader); //4. Create a query for the search//create parse to determine the content of the search, the second parameter is the search for the fileQueryparser Queryparser =NewQueryparser ("Content",NewStandardAnalyzer ()); //Create a query that represents the content in the search domainQuery query = Queryparser.parse ("Love"); //5. Search for and return to TopdocsTopdocs Topdocs = indexsearcher.search (query, 10); //6. Obtained scoredocs according to Topdocsscoredoc[] Socredocs =Topdocs.scoredocs; for(Scoredoc doc:socredocs) {//Get Document ObjectDocument document =Indexsearcher.doc (Doc.doc); //get the values you want based on the Document objectSystem.out.println (Document.get ("FileName")); System.out.println (Document.get ("Content")); } indexreader.close (); }}
Third, the results of the operation are as follows
Java1.txti love your, Java.java2.txtI love you, Linux.java3.txtI love you, Huhx.
Friendship Link
Java Framework----Use of >lucene (i)