需要jar包至少为 :
lucene-analyzers-common-4.4.0.jar
lucene-core-4.4.0.jar
lucene-queryparser-4.4.0.jar
是否需要依赖包不知道了
可以到http://lucene.apache.org/下载zip解压缩后到各个文件夹里面找
首先在D盘新建两个文件夹D:/luceneIndex和D:/luceneData,在luceneData下面随便添加几个文本文件
然后生成索引,然后再根据索引开始搜索
生成索引的代码:执行以后在luceneIndex下面会出现几个文件package com.huateng.lucene;
package com.huateng.lucene;
import java.io.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class TxtFileIndexer {
public static void main(String[] args) throws Exception {
File indexDir = new File("d:/luceneIndex");
File dataDir = new File("D:/luceneData");
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
File[] dataFiles = dataDir.listFiles();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44,
analyzer);
Directory dir = FSDirectory.open(indexDir);
IndexWriter indexWriter = new IndexWriter(dir, iwc);
for (File file : dataFiles) {
System.out.println(file.getPath());
FileInputStream fis = new FileInputStream(file);
Document doc = new Document();
Field pathField = new StringField("path", file.getPath(),
Field.Store.YES);
doc.add(pathField);
doc.add(new LongField("modified", file.lastModified(),
Field.Store.NO));
doc.add(new TextField("contents", new BufferedReader(
new InputStreamReader(fis, "UTF-8"))));
indexWriter.addDocument(doc);
}
indexWriter.close();
}
}
执行搜索的代码
package com.huateng.lucene;
import java.io.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.FSDirectory;
import org.apache.lucene.util.Version;
public class TxtFileSearcher {
public static void main(String[] args) throws Exception{
String field = "contents";
String queryStr = "lucene";
File indexDir = new File("D:/luceneIndex");
IndexReader reader = DirectoryReader.open(FSDirectory.open(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
Query query = parser.parse(queryStr);
System.out.println("Searching for: " + query.toString(field));
TopDocs results = searcher.search(query, 50);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
System.out.println(numTotalHits + " total matching documents");
for(ScoreDoc sd:hits){
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("path"));
}
}
}
执行以后输出:
Searching for: lucene
2 total matching documents
D:\luceneData\a.txt
D:\luceneData\b.txt