java lucene2.0

來源:互聯網
上載者:User

最近因工作需要,需要學習java lucene2.0,剛開始學習,長話短說,記錄下來!!

 

1、下載lucene2.0
http://lucene.apache.org/

http://archive.apache.org/dist/lucene/java/

lucene-2.0.0.zip
lucene-core-2.0.0.jar

 

2、設定環境變數CLASSPATH

/home/tomcat/lucene-core-2.0.0.jar

3、寫個建立索引小程式練習,寫程式,編譯器,運行程式,一氣呵成。

#vi IndexTest.java

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.IOException;
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;

public class IndexTest {
        public static void main(String[] args) throws Exception{
                Date start = new Date();

                try {
                        IndexWriter indexWriter = new IndexWriter("/lucene_test/data/", new StandardAnalyzer(), true);

                        System.out.println("Indexing file /lucene_test/doc/1.txt");

                        Document document = new Document();
                        Reader reader = new FileReader("/lucene_test/doc/1.txt");

                        String path = "/lucene_test/doc/1.txt";

                        //document.add(new Field("path", path));
                        document.add(new Field("path", path, Field.Store.YES, Field.Index.NO));
                        document.add(new Field("contents", reader));

                        indexWriter.addDocument(document);
                        indexWriter.optimize();
                        indexWriter.close();
                } catch(IOException e) {
                        e.printStackTrace();
                }

                Date end = new Date();

                System.out.print(end.getTime() - start.getTime());
                System.out.println(" total milliseconds");
        }
}

#javac IndexTest.java

#java IndexTest

 

4、寫一個搜尋小程式

#vi SearchTest.java

import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;

public class SearchTest {
        public static void main(String[] args) throws Exception {
                File indexDir = new File("/root/coffee/lucene_test/data/");
                FSDirectory directory = FSDirectory.getDirectory(indexDir, false);
                IndexSearcher searcher = new IndexSearcher(directory);
                if(!indexDir.exists()) {
                        System.out.println("index is not exist");
                        return;
                }

                Term term = new Term("contents", "twinkle");
                TermQuery query = new TermQuery(term);
                Hits hits = searcher.search(query);
                for(int i=0; i<hits.length(); i++) {
                        Document document = hits.doc(i);
                        System.out.println("File: " + document.get("path") + " " + String.valueOf(document.getBoost()));
                }
        }
}

 

5、本周沒時間進一步深入學習了,計划下周好好學習下lucene文檔分數setBoost的控制

 

參考網站:

http://www.chedong.com/tech/lucene.html Lucene:基於Java的全文檢索索引引擎簡介

http://www.javaeye.com/topic/33241 lucene 入門

http://kb.cnblogs.com/b/243888/ Lucene源碼分析筆記之[org.apache.lucene.document](四)

征服Ajax+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.