lucene4.10.2入门

1、首先到官网下载lucene的jar包是必须的

2、下载完的jar中其中有一个demo 有一个是lucene-xml-query-demo.war可以放到tomcat 安装目录的webapps中

3、将tomcat服务器开启输入localhost:8080/lucene-xml-query-demo将会出现界面但是点击查询会报java.lang.ClassNotFoundException: org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo这个错误。这个原因是新版本中FormBasedXmlQueryDemo的路径

变了,这时你就需要到该项目的web.xml中将<servlet-class>org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo</servlet-class>

更改为<servlet-class>org.apache.lucene.demo.xmlparser.FormBasedXmlQueryDemo</servlet-class>

然后把lucene-4.1.0解压包下analysis\common\lucene-analyzers-common-4.10.2.jar 和sandbox\lucene-sandbox-4.10.2.jar

这两个文件拷贝到WEB-INF\lib文件夹下面,这时在点击查询就不会出现问题了 输入java查询结果如下


3、上面的lucene中的附带的例子下面开始进入lucene简单的学习

lucene基本工作原理简单可以理解为创建索引,而根据索引查询

下面是简单的例子

TxtFileInderxer作用是将D:/luceneData中所有的.txt文件建立索引并将所有的索引存放在D:/luceneIndex中

public class TxtFileIndexer {
public static void main(String[] args) throws Exception {
// indexDir is the directory that hosts Lucene's index files
File indexDir = new File("D:\\luceneIndex");
// dataDir is the directory that hosts the text files that to be indexed
File dataDir = new File("D:\\luceneData");
// Analyzer luceneAnalyzer = new
// StandardAnalyzer(Version.LUCENE_4_10_2);


// 对文档进行分词
Analyzer luceneAnalyzer = new StandardAnalyzer();


File[] dataFiles = dataDir.listFiles();


IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_4_10_2, luceneAnalyzer);


// 创建索引
IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),
indexWriterConfig);


// IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
// true);


long startTime = new Date().getTime();


for (int i = 0; i < dataFiles.length; i++) {
if (dataFiles[i].isFile()
&& dataFiles[i].getName().endsWith(".txt")) {
System.out.println("Indexing file "
+ dataFiles[i].getCanonicalPath());
// 封装document对象
Document document = new Document();
Reader txtReader = new FileReader(dataFiles[i]);
document.add(new TextField("path", dataFiles[i]
.getCanonicalPath(), Store.YES));
// document.add(Field.Text("contents", txtReader));
document.add(new TextField("contents", txtReader));
indexWriter.addDocument(document);
}
}


indexWriter.commit();
// indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();


System.out.println("It takes " + (endTime - startTime)
+ " milliseconds to create index for the files in directory "
+ dataDir.getPath());
}
}

TxtFileSearcher作用是从D:/luceneIndex中读取索引并查询.txt文件中含有lucene的文件

public class TxtFileSearcher {
public static void main(String[] args) throws Exception {
String queryStr = "lucene";
// This is the directory that hosts the Lucene index
File indexDir = new File("D:\\luceneIndex");


Directory directory = FSDirectory.open(indexDir);
// FSDirectory fsDirectory = FSDirectory.open(indexDir);
IndexReader indexReader = IndexReader.open(directory);


// FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
// IndexReader indexReader = IndexReader.open(fsDirectory);


IndexSearcher indexSearcher = new IndexSearcher(indexReader);


// IndexSearcher searcher = new IndexSearcher(indexReader);
if (!indexDir.exists()) {
System.out.println("The Lucene index is not exist");
return;
}
Term term = new Term("contents", queryStr.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
// Hits hits = searcher.search(luceneQuery);
TopDocs topDocs = indexSearcher.search(luceneQuery, 1000);


ScoreDoc[] scoreDocs = topDocs.scoreDocs;
if (scoreDocs == null || scoreDocs.length == 0) {
System.out.println("The  Lucene index is not exist");


}


for (int i = 0; i < scoreDocs.length; i++) {
Document document = indexSearcher.doc(scoreDocs[i].doc);
System.out.println("File: " + document.get("path"));
}
indexReader.close();
}
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值