今天看了下Lucene的更新,已经到4.4.0的版本了,而且新的API变化也比较多,对于老版本的Lucene想要升级到最新的版本,不是简单的更新jar包就可以的。
下面写了个简单的小例子。可以看一下
1.创建maven工程
在eclipse里面创建个maven项目,一个简单的项目即可,下面是对于的pom.xml文件,需要将Lucene的jar包引入进来。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jacksoft</groupId> <artifactId>Lucene-test</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <lucene.version>4.4.0</lucene.version> </properties> <dependencies> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>${lucene.version}</version> </dependency> </dependencies> </project>
这里只是简单的添加依赖关系,下载好jar包之后,我在本地创建了3个txt文件,如下:
准备工作就差不多了,下面就开始进行编码工作
2. 编码
由于是多个文件的搜索,这里创建一个工具类来递归目录,找到这三个txt文件,我将这三个txt文件放在本地:D:\lucene\luceneData目录中
package com.jacksoft.lucene.util;
import java.io.File;
import java.util.List;
public class FileUtils {
public static void listFile(File f,List<String> fileList){
if(f.isDirectory()){
File[] files = f.listFiles();
for(int i=0;i<files.length ;i++)
{
listFile(files[i],fileList) ;
}
}else{
fileList.add(f.getAbsolutePath());
}
}
}
这样返回的fileList中就包含了我们需要的txt文件,当然这里还可以设置过滤器来指定后缀名。
接下来就是创建索引和查询的过程:
package com.jacksoft.lucene;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
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.index.IndexWriterConfig.OpenMode;
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.apache.lucene.util.Version;
import com.jacksoft.lucene.util.FileUtils;
/**
* 多文件搜索
* @Project Lucene-test
*
* @Filename MultLuceneTest2.java
*
* @author Jack.Zhou
*
* @Date 2013-8-29
*
*/
public class LuceneTest {
private static final String QUERY_STR = "四川";
private static final String FILE_TARGET = "D:\\lucene\\luceneData";
private static final String FILE_INDEX = "D:\\lucene\\luceneIndex";
public static void main(String[] args) {
try {
LuceneTest t = new LuceneTest();
t.createIndex();
t.searchByKeyWords(QUERY_STR);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建索引
* @throws Exception
*/
private void createIndex() throws Exception{
Long startTime = System.currentTimeMillis();
File indexDir = new File(FILE_INDEX);
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, luceneAnalyzer);
config.setOpenMode(OpenMode.CREATE);
Directory directory = FSDirectory.open(indexDir);
IndexWriter indexWriter = new IndexWriter(directory, config);
List<String> fileList = new ArrayList<String>();
FileUtils.listFile(new File(FILE_TARGET), fileList);
for(String filePath : fileList){
System.out.println("文件:" + filePath + "正在被索引....");
String content = readFile(filePath);
Document doc = new Document();
doc.add(new TextField("content", content.toString(), Store.YES));
doc.add(new TextField("path", filePath, Store.YES));
indexWriter.addDocument(doc);
}
indexWriter.close();
Long endTime = System.currentTimeMillis();
System.out.println("花费了" + (endTime - startTime) + "毫秒来创建索引文件");
}
/**
* 读取文件内容
* @param filePath
* @return
* @throws Exception
*/
private String readFile(String filePath) throws Exception{
@SuppressWarnings("resource")
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath)));
StringBuffer content = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
content.append(str).append("\n");
}
return content.toString();
}
private void searchByKeyWords(String keyWords) throws Exception{
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(FILE_INDEX)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
QueryParser parser = new QueryParser(Version.LUCENE_44, "content",analyzer);
Query query = parser.parse(keyWords);
TopDocs results = searcher.search(query,1000);
ScoreDoc[] score = results.scoreDocs;
if (score.length == 0) {
System.out.println("对不起,没有找到您要的结果。");
} else {
System.out.println("查找["+QUERY_STR+"]有" + score.length + "个结果");
for (int i = 0; i < score.length; i++) {
try {
Document doc = searcher.doc(score[i].doc);
System.out.print("这是第" + i + "个检索到的结果,文件名为:");
System.out.println(doc.get("path"));
System.out.println("内容:\n" + doc.get("content"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
代码中的常量可以通过文档来查看,运行后就可以看到搜索的结果了。
当然这里只是对txt进行搜索,你还可以对word,excel,pdf等文档进行搜索,前提是要将其内容读取出来,创建索引即可。