lucene创立和查询索引Demo
package com.git.lucene;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
/**
* 应用lucene进行索引的增删改查时 视察具体的源码履行进程
* 目前先看查询
* @author songqinghu
*
*/
public class LuceneSourceReadTest {
public static void main(String[] args) throws Exception {
// fullIndex();
readIndex();
}
/**
*
* @throws IOException
* @描写:读取索引信息
*/
public static void readIndex() throws IOException{
Directory directory = FSDirectory.open(new File("E:\lucene\index")) ;
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher index = new IndexSearcher(reader);
Term t = new Term("content", "apache");
Query query = new TermQuery(t);
TopDocs topdocs = index.search(query,20);
int totalHits = topdocs.totalHits;
System.out.println("总条目: "+totalHits);
ScoreDoc[] scoreDocs = topdocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
int docid = scoreDoc.doc;
System.out.println(docid);
Document doc = index.doc(docid);
System.out.println("fileName : "+doc.getField("fileName"));
System.out.println("content : "+doc.getField("content"));
}
}
/**
* @throws IOException
* @描写:索引的树立
*/
public static void fullIndex() throws IOException{
//索引寄存
Directory d = SimpleFSDirectory.open(new File("E:\lucene\index")) ;
//分词器
Analyzer analyzer = new MMSegAnalyzer();
//写入设置
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
//写入器
IndexWriter indexWriter = new IndexWriter(d, conf);
ArrayList<Document> docs = new ArrayList<Document>();
File filepath = new File("E:\lucene\file");
if(filepath.isDirectory()){
File[] files = filepath.listFiles();
for (File file : files) {
String fileName = file.getName();
long fileSize = FileUtils.sizeOf(file);
String content = FileUtils.readFileToString(file);
if(content.length()>100){
content = content.substring(0,100);
}
String path = file.getPath();
Document doc = new Document();
StringField fileNameField = new StringField("fileName", fileName, Store.YES);
doc.add(fileNameField);
StringField pathField = new StringField("path", path, Store.YES);
doc.add(fileNameField);
LongField fileSizeField = new LongField("fileSize", fileSize, Store.YES);
doc.add(fileSizeField);
TextField contentField = new TextField("content", content, Store.YES);
doc.add(contentField);
docs.add(doc);
}
}
indexWriter.addDocuments(docs);
indexWriter.commit();
System.out.println("全量索引诱入停止!");
}
}package com.git.lucene;
impor