最简单的支持中文的示例

需要先设置classpath包含lucene-*.*.jar 和 CJKAnalyzer.class及相关类的包

建立索引:

import java.io.*;
import java.text.*;
import java.util.*;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;

public class Mp3Indexer
{
 public final static String mp3Path="E://悦耳_music//new songs";//mp3所在目录
 public final static String indexPath="d://mp3Indexer";//索引存放目录
 public static void main(String[] args) throws ClassNotFoundException, IOException{
  try {
   IndexWriter writer = new IndexWriter(indexPath, new CJKAnalyzer(), true);
   indexMp3s(writer, new File(mp3Path));

   System.out.println("优化中....");
   writer.optimize();
   writer.close();

  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
}

public static void indexMp3s(IndexWriter writer, File file) throws Exception {
 if (file.isDirectory()) {
  String[] files = file.list();
  for (int i = 0; i < files.length; i++) {
   indexMp3s(writer, new File(file, files[i]));
  }
 }
 else if (file.getPath().endsWith(".mp3")) { //只对 MP3 文件做索引
 System.out.print("正在处理文件:" + file + " ....");
 // Add mp3 file ....
 Document doc = new Document();
 doc.add(Field.Text("name", file.getName())); //索引文件名
 doc.add(Field.UnIndexed("modified", DateFormat.getDateTimeInstance().format(new Date(file.lastModified())))); //索引最后修改时间
  System.out.println("[处理完成]");

 writer.addDocument(doc);
 } //end else if
}

} //end class

查询:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;

class SearchFiles {
  public static void main(String[] args) {
    try {
      Searcher searcher = new IndexSearcher("d://mp3Indexer");
      Analyzer analyzer = new CJKAnalyzer();

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      while (true) {
 System.out.print("Query: ");
 String line = in.readLine();

 if (line.length() == -1)
   break;

 Query query = QueryParser.parse(line, "name", analyzer);
 System.out.println("Searching for: " + query.toString("name"));

 Hits hits = searcher.search(query);
 System.out.println(hits.length() + " total matching documents");

 final int HITS_PER_PAGE = 10;
 for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
   int end = Math.min(hits.length(), start + HITS_PER_PAGE);
   for (int i = start; i < end; i++) {
     Document doc = hits.doc(i);
     String path = doc.get("name");
     if (path != null) {
              System.out.println(i + ". " + path);
     }
       }
    
  

   if (hits.length() > end) {
     System.out.print("more (y/n) ? ");
     line = in.readLine();
     if (line.length() == 0 || line.charAt(0) == 'n')
       break;
   }
 }
      }
      searcher.close();

    } catch (Exception e) {
      System.out.println(" caught a " + e.getClass() +
    "/n with message: " + e.getMessage());
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值