Java如何使用lucene開發搜尋引擎

來源:互聯網
上載者:User

最近忽然心血來潮想用java寫個搜尋引擎,經過一天的研究也有小成,把自己寫的一個簡單的Hello word發給大家看看,希望對大家有所協助.關於lucene的架包可以在apache的官方網站下載到,如果需要問我要.如果對lucene不瞭解的可以先看一下我的利用lucene搜尋這篇文章,或則到網上去查一下資料.
/*
 * author:shijiajia
 * Created on 2006-8-30
 */
package com.client;

import java.util.Date;

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.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

public class BooleanQueryTest1
{ 
  /**

   *
   * @param args
   * @throws Exception
   */ 
  public static void main (String [] args) throws Exception {
    Document doc1 = new Document();
    doc1.add(Field.Text("name", "word1 word2 word3"));
    doc1.add(Field.Keyword("title", "doc1"));
   
    Document doc2 = new Document();
    doc2.add(Field.Text("name", "word1 word4 word5"));
    doc2.add(Field.Keyword("title", "doc2"));
   
    Document doc3 = new Document();
    doc3.add(Field.Text("name", "word1 word2 word6"));
    doc3.add(Field.Keyword("title", "doc3"));
   
    Document doc4 = new Document();
    doc4.add(Field.Text("name", "中人民共和國"));
    doc4.add(Field.Keyword("title", "doc4"));
    IndexWriter writer = new IndexWriter("c:\\index", new StandardAnalyzer(), true);
    writer.addDocument(doc1);
    writer.addDocument(doc2);
    writer.addDocument(doc3);
    writer.addDocument(doc4);
    writer.close();
   
    Query query1 = null;
    Query query2 = null;
    BooleanQuery query = null;
    Hits hits = null;
   
    IndexSearcher searcher = new IndexSearcher("c:\\index");
   
    query1 = new TermQuery(new Term("name","人"));
    query2 = new TermQuery(new Term("name","word2"));
   
    // 構造一個布爾查詢
    query = new BooleanQuery();
   
    // 添加兩個子查詢
    query.add(query1, false, false);
    query.add(query2, false, false);
    /////////
    Date start = new Date();
    hits = searcher.search(query);
    Date end = new Date();
    System.out.println("總共耗時" + (end.getTime()-start.getTime()) + "毫秒" );
    ///////////////////
    printResult(hits, "word1和word2");
   
  }
 
  public static void printResult(Hits hits, String key) throws Exception
  {
    System.out.println("尋找 \"" + key + "\" :");
    if (hits != null)
    {
      if (hits.length() == 0)
      {
        System.out.println("沒有找到任何結果");
      }
      else
      {
        System.out.println("找到" + hits.length() + "個結果");
        for (int i = 0; i < hits.length(); i++)
        {
          Document d = hits.doc(i);
          String dname = d.get("title");
          System.out.print(dname + "   ");
        }
        System.out.println();
        System.out.println();
      }
    }
  }
}
推薦學lunece的人看一下lucene in action這本書,很不錯~~~~~~

聯繫我們

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