lucene 例子,lucene demo, lucene 教程

來源:互聯網
上載者:User

標籤:java   索引   搜尋   lucene   

查了很多lucene資料,就納悶為啥不能分享個簡單的例子,我在這裡自己寫了一個

lucene實現其實很簡單,先建立索引,在進行搜尋,easy!

下載jar包,連結在這裡:http://download.csdn.net/detail/dannor2010/8183641   項目中匯入lib即可,不多說了。前期:建立兩個txt檔案,
C:\\source
C:\\index
source 中建立txt檔案,輸入你想要測試搜尋的String類型內容。
1、建立索引,代碼如下
 

package com.ch.lucene;import java.io.File;import java.io.FileReader;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.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import com.ch.util.FileUtil;public class TextFileIndexer {@SuppressWarnings("deprecation")public static void main(String[] args) throws Exception {File fileDir = new File("C:\\source");File fileIndex = new File("C:\\index");Directory dir = FSDirectory.open(fileIndex);Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_45);IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45,luceneAnalyzer);iwc.setOpenMode(OpenMode.CREATE);IndexWriter iw = new IndexWriter(dir, iwc);File[] textFiles = fileDir.listFiles();long startTime = new Date().getTime();// 增加document到索引去for (File file : textFiles) {if (file.isFile() && file.getName().endsWith(".txt")) {System.out.println("檔案:" + file.getCanonicalPath() + "正在被索引操作。");String temp = FileUtil.readTxtFile(file.getPath());System.out.println(temp);Document document = new Document();@SuppressWarnings("unused")Field filepath = new Field("path", file.getPath(),Field.Store.YES, Field.Index.NO);Field FieldBody = new Field("body", temp, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);document.add(filepath);document.add(FieldBody);iw.addDocument(document);}}iw.close();// 測試一下索引的時間long endTime = new Date().getTime();System.out.println("這花費了" + (endTime - startTime) + " 毫秒來把文檔增加到索引裡面去"+ fileDir.getPath());}}


2、進行搜尋,代碼如下

package com.ch.lucene;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.index.IndexReader;import org.apache.lucene.queryparser.classic.ParseException;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class Query {public static void main(String[] args) throws IOException, ParseException {String index = "C:\\index";@SuppressWarnings("deprecation")IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));IndexSearcher searcher = new IndexSearcher(reader);ScoreDoc[] hits = null;String queryString = "關鍵詞"; // 搜尋的關鍵詞Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);QueryParser qp = new QueryParser(Version.LUCENE_45, "body", analyzer);org.apache.lucene.search.Query query = qp.parse(queryString);if (searcher != null) {TopDocs results = searcher.search(query, 10);hits = results.scoreDocs;if (hits.length > 0) {System.out.println("找到:" + hits.length);}}}}





lucene 例子,lucene demo, lucene 教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.