lucene簡單一實例

來源:互聯網
上載者:User

標籤:Lucene   style   blog   http   io   ar   color   os   sp   

建立索引的代碼如下:

package com.search.lucene;import java.io.File;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.Store;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import org.junit.Before;import org.junit.Test;public class IndexFile {    protected String[] ids={"1", "2"};    protected String[] content={"Amsterdam has lost of add  cancals", "i love  add this girl"};    protected String[] city={"Amsterdam", "Venice"};    private Directory dir;    /**     * 初始添加文檔     * @throws Exception     */    @Test    public void init() throws Exception {        String pathFile="D://lucene/index";        dir=FSDirectory.open(new File(pathFile));        IndexWriter writer=getWriter();        for(int i=0; i < ids.length; i++) {            Document doc=new Document();            doc.add(new StringField("id", ids[i], Store.YES));            doc.add(new TextField("content", content[i], Store.YES));            doc.add(new StringField("city", city[i], Store.YES));            writer.addDocument(doc);        }        System.out.println("init ok?");        writer.close();    }    /**     * 獲得IndexWriter對象     * @return     * @throws Exception     */    public IndexWriter getWriter() throws Exception {        Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_40);        IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);        return new IndexWriter(dir, iwc);    }}

 說明下:lucene4.0中有核心包和其他包:我匯入

package com.search.lucene;import java.io.File;import org.apache.lucene.document.Document;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.junit.Test;public class IndexSearch {    /**     * 查詢     * @throws Exception     */    @Test    public void search() throws Exception {        String filePath="D://lucene/index";        Directory dir=FSDirectory.open(new File(filePath));        IndexReader reader=DirectoryReader.open(dir);        IndexSearcher searcher=new IndexSearcher(reader);        Term term=new Term("content", "add");        TermQuery query=new TermQuery(term);        TopDocs topdocs=searcher.search(query, 5);        ScoreDoc[] scoreDocs=topdocs.scoreDocs;        System.out.println("查詢結果總數---" + topdocs.totalHits+"最大的評分--"+topdocs.getMaxScore());        for(int i=0; i < scoreDocs.length; i++) {            int doc = scoreDocs[i].doc;            Document document = searcher.doc(doc);            System.out.println("content===="+document.get("content"));            System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--"+scoreDocs[i].shardIndex);        }        reader.close();    }}

 

result:

  查詢結果總數---2最大的評分--0.2972674

content====i love  add this girlid--1---scors--0.2972674---index---1content====Amsterdam has lost of add  cancalsid--0---scors--0.26010898---index---1

 

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.