Import Org.apache.commons.io.FileUtils;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.document.NumericDocValuesField;
Import Org.apache.lucene.document.TextField;
Import Org.apache.lucene.index.DirectoryReader;
Import Org.apache.lucene.index.IndexReader;
Import Org.apache.lucene.index.IndexWriter;
Import Org.apache.lucene.index.IndexWriterConfig;
Import org.apache.lucene.queryparser.classic.ParseException;
Import Org.apache.lucene.queryparser.classic.QueryParser;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.Query;
Import Org.apache.lucene.search.ScoreDoc;
Import Org.apache.lucene.search.TopDocs;
Import Org.apache.lucene.store.Directory;
Import Org.apache.lucene.store.FSDirectory;
Import Org.junit.Test;
Import Java.io.File;
Import java.io.IOException;
Import Java.nio.file.Path;
Import java.nio.file.Paths;
public class Day01 {
Public String datadir= "E:\\data";
2.search Search
@Test
public void Search () throws IOException, ParseException {
01.
Path path=paths.get ("./luceneindex/");
Directory Directory=fsdirectory.open (path);
Indexreader indexreader=directoryreader.open (directory);
Index query Tool
Indexsearcher indexsearcher=new Indexsearcher (Indexreader);
String word= "Battle";
Queryparser queryparser=new queryparser ("filename", new StandardAnalyzer ());
Query query=queryparser.parse (word);
Topdocs Topdocs = indexsearcher.search (query, 10);
Long Count = topdocs.totalhits;
SYSTEM.OUT.PRINTLN ("Total number of records:" +count);
scoredoc[] Scoredocs = Topdocs.scoredocs;
for (Scoredoc Item:scoredocs) {
int docid = Item.doc; Document number
Document document = Indexreader.document (DOCID);
String filename = document.get ("filename");
SYSTEM.OUT.PRINTLN (filename);
}
}
@Test
1. Create an index
public void CreateIndex () throws IOException {
01. Prepare a directory to write to the index
Path PATH = Paths.get ("./luceneindex/");
02. Bind the path to the directory
Directory Directory=fsdirectory.open (path);
03. This line of code at the bottom of the default has been associated with a standardanalyzer standard word breaker
Indexwriterconfig config=new indexwriterconfig ();
04. Set a schema for config Create: Erase the original index file each time and rebuild the new index file
Config.setopenmode (IndexWriterConfig.OpenMode.CREATE);
05. The index writer requires two entry parameters, one is the directory, the other is the configuration
IndexWriter writer=new IndexWriter (directory,config);
06. Prepare to add content to the index library
File File = new file (DataDir);
07. A collection of files under a directory
file[] files = file.listfiles ();
for (File item:files) {
08. Turn Content into----->document
Document Document=new document ();
String Filename=item.getname (); File--->string
SYSTEM.OUT.PRINTLN (filename);
System.out.println ("===============================");
String filecontent=fileutils.readfiletostring (item);
Long modifydate=item.lastmodified ();
09. Real Field Assignment to document
Document.add (New TextField ("filename", filename,field.store.yes));
Document.add (New TextField ("Filecontent", Filecontent,field.store.yes));
Document.add (New Numericdocvaluesfield ("Modifydate", modifydate));
10. Save a single document (a record) in the index library
Writer.adddocument (document);
}
11. Close the Index library
Writer.close ();
SYSTEM.OUT.PRINTLN ("Add Indexes ok!");
}
}
Write and read the index library using Lucene's Java API