您的位置:首页 > 其它

lucene4.10.2入门

2014-12-16 21:00 232 查看
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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: