发布于 2014-12-16 14:29:25 | 366 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的Lucene教程,程序狗速度看过来!

Apache Lucene全文检索引擎工具包

Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。


本文为大家介绍的是一篇 lucene 使用入门教材,Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎,感兴趣的同学参考下.

lucene简介

Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。

lucene入门

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)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务