lucene4.10.2入門

來源:互聯網
上載者:User

標籤: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();
}
}





lucene4.10.2入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.