package com.jr.demo;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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.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 com.jr.util.Util;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
public class Demo1 {
public static void main(String[] args) throws Exception {
//index("d://index");
QueryContent("搜");
}
/**
* 创建索引
* @param strindex
*/
public static void index(String strindex){
Path ps=Paths.get("d://");
File f=ps.toFile();
File []files=f.listFiles();
try {
/* Path p=Paths.get(strindex);
Directory directory=FSDirectory.open(p);*/
Util.OpenDirectory("d://index");
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(new StandardAnalyzer());
IndexWriter indexWriter=new IndexWriter(Util.directory, indexWriterConfig);
for (File file:files) {
Document document=new Document();
String filename=file.getName();
String prefixpathname=file.getCanonicalPath();
String path=file.getPath();
document.add(new TextField("filename", filename, Field.Store.YES));
document.add(new TextField("prefixpathname", prefixpathname, Field.Store.YES));
document.add(new TextField("path", path, Field.Store.YES));
indexWriter.addDocument(document);
// System.out.println(filename);
}
indexWriter.commit();
indexWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List QueryContent(String s){
Util.OpenDirectory("d://index");
try {
IndexReader indexReader=DirectoryReader.open(Util.directory);
IndexSearcher searcher=new IndexSearcher(indexReader);
QueryParser parser=new QueryParser("filename", new StandardAnalyzer());
Query query=parser.parse(s);
TopDocs docs=searcher.search(query, 10000);
ScoreDoc[] docs2=docs.scoreDocs;
for (ScoreDoc doc:docs2) {
Document document=searcher.doc(doc.doc);
System.out.println("filename:"+document.get("filename"));
System.out.println("prefixpathname:"+document.get("prefixpathname"));
System.out.println("path:"+document.get("path"));
}
indexReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
工具类:
package com.jr.util;
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* 创建Directory工具类
*
*
*/
public class Util {
public static Directory directory;
public static boolean OpenDirectory(String path){
try {
directory=FSDirectory.open(Paths.get(path));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}