Apache Lucence 使用的一个简单例子
//在C盘下创建一个s的文件夹,然后在s文件夹中新建三个文件:1.txt,3.txt,2.txt,,1.txt中输入"想 飞的我",2.txt中输入"想飞",3.txt中输入"我".接下来我们来看下面的测试:
测试结果:
找到:2 个结果!
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.Date;
- 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.index.CorruptIndexException;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.store.LockObtainFailedException;
- //需要的apache下的lucene包
- /**
- * 创建索引文件
- */
- public class CreateIndex {
- public static boolean createDocumentIndex()
- {
- boolean bool = false;
- try{
- //需要索引的目录
- File dirpath = new File("c:\\s");
- //将索引文件存放在index目录下
- File indexpath = new File("c:\\index");
- //创建分词器
- Analyzer analyzer = new StandardAnalyzer();
- IndexWriter index = new IndexWriter(indexpath,analyzer,true);
- File[] txtfiles = dirpath.listFiles();
- long starttime = new Date().getTime();
- for(int i=0;i<txtfiles.length;i++)
- {
- if(txtfiles[i].isFile())
- {
- System.out.println("文件"+txtfiles[i].getCanonicalPath()+"正在索引中...");
- Reader read = new FileReader(txtfiles[i]);
- Document doc = new Document();
- doc.add(new Field("content",read));
- doc.add(new Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.NO));
- index.addDocument(doc);
- }
- }
- index.optimize();
- index.close();
- long endTime = new Date().getTime();
- System.out
- .println("这花费了"
- + (endTime - starttime)
- + " 毫秒来把文档增加到索引里面去!"
- + dirpath.getPath());
- bool=true;
- }
- catch(Exception e)
- {
- bool = false;
- }
- return bool;
- }
- /**
- * 测试
- */
- public static void main(String[] a)
- {
- CreateIndex.createDocumentIndex();
- }
- }
- 测试结果:
- 文件C:\s\1.txt正在索引中...
- 文件C:\s\2.txt正在索引中...
- 文件C:\s\3.txt正在索引中...
- 这花费了141 毫秒来把文档增加到索引里面去!c:\index
- package test;
- import java.io.IOException;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.queryParser.QueryParser;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- /**
- * 从索引文件查询内容
- * @author Jan
- *
- */
- public class QueryString
- {
- public static void main(String[] args) throws IOException, ParseException {
- Hits hits = null;
- String queryString = "我";
- Query query = null;
- IndexSearcher searcher = new IndexSearcher("c:\\index");
- Analyzer analyzer = new StandardAnalyzer();
- try {
- QueryParser qp = new QueryParser("content", analyzer);
- query = qp.parse(queryString);
- } catch (ParseException e) {
- }
- if (searcher != null) {
- hits = searcher.search(query);
- if (hits.length() > 0) {
- System.out.println("找到:" + hits.length() + " 个结果!");
- }
- }
- }
- }
package test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
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.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
//需要的apache下的lucene包
/**
* 创建索引文件
*/
public class CreateIndex {
public static boolean createDocumentIndex()
{
boolean bool = false;
try{
//需要索引的目录
File dirpath = new File("c:\\s");
//将索引文件存放在index目录下
File indexpath = new File("c:\\index");
//创建分词器
Analyzer analyzer = new StandardAnalyzer();
IndexWriter index = new IndexWriter(indexpath,analyzer,true);
File[] txtfiles = dirpath.listFiles();
long starttime = new Date().getTime();
for(int i=0;i<txtfiles.length;i++)
{
if(txtfiles[i].isFile())
{
System.out.println("文件"+txtfiles[i].getCanonicalPath()+"正在索引中...");
Reader read = new FileReader(txtfiles[i]);
Document doc = new Document();
doc.add(new Field("content",read));
doc.add(new Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.NO));
index.addDocument(doc);
}
}
index.optimize();
index.close();
long endTime = new Date().getTime();
System.out
.println("这花费了"
+ (endTime - starttime)
+ " 毫秒来把文档增加到索引里面去!"
+ dirpath.getPath());
bool=true;
}
catch(Exception e)
{
bool = false;
}
return bool;
}
/**
* 测试
*/
public static void main(String[] a)
{
CreateIndex.createDocumentIndex();
}
}
测试结果:
文件C:\s\1.txt正在索引中...
文件C:\s\2.txt正在索引中...
文件C:\s\3.txt正在索引中...
这花费了141 毫秒来把文档增加到索引里面去!c:\index
package test;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
/**
* 从索引文件查询内容
* @author Jan
*
*/
public class QueryString
{
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
String queryString = "我";
Query query = null;
IndexSearcher searcher = new IndexSearcher("c:\\index");
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("content", analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher != null) {
hits = searcher.search(query);
if (hits.length() > 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
}
}
}
}
测试结果:
找到:2 个结果!