以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  信息检索原理课件  (http://njcie.com/bbs/list.asp?boardid=16)
----  [推荐]Lucene入门练习  (http://njcie.com/bbs/dispbbs.asp?boardid=16&id=667)

--  作者:admin
--  发布时间:2009/3/18 20:27:41
--  [推荐]Lucene入门练习

1、只能索引一个文件的最简单的Lucene程序
Exec.java
import java.io.File;
import java.io.FileReader;

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.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Exec {
public static void main(String[] args) throws Exception {
  File indexDir = new File("C:\\\\temp");
  File dataDir = new File("C:\\\\docs\\\\lucene.txt");

  // indexDir表示索引的存放地点
  // new StandardAnalyzer()表示分析器
  // true表示如已有索引则覆盖之
  IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
    true);

  // 表示生成多文件索引文件(默认为复合索引)
  writer.setUseCompoundFile(false);

  Document doc = new Document();
  doc.add(Field.Text("contents", new FileReader(dataDir)));
  doc.add(Field.Keyword("filename", dataDir.getCanonicalPath()));
  writer.addDocument(doc);
  writer.optimize();
  writer.close();

  // if true, create, or erase any existing contents
  Directory fsDir = FSDirectory.getDirectory(indexDir, false);

  // 打开索引
  IndexSearcher is = new IndexSearcher(fsDir);

  // 解析查询词语
  // Query query = new TermQuery(new Term("contents", "String"));
  // Query query = new TermQuery(new Term("contents", "Character"));
  Query query = QueryParser.parse("java AND NOT lucene", "contents",
    new StandardAnalyzer());
  // in*

  // 搜索索引
  Hits hits = is.search(query);

  for (int i = 0; i < hits.length(); i++) {
   Document document = hits.doc(i);
   System.out.println(document.get("filename"));
  }
}
}

2、可以索引指定目录下的多个文件(增加了递归访问目录的方法):
Indexer.java(索引程序):
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

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;

/**
* This code was originally written for Erik\'s Lucene intro java.net article
*/
public class Indexer {
public static void main(String[] args) throws Exception {
  // Create Lucene index in this directory
  File indexDir = new File("C:\\\\temp");

  // Index files in this directory
  File dataDir = new File("C:\\\\docs");
  index(indexDir, dataDir);
}

public static int index(File indexDir, File dataDir) throws IOException {
  IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
    true);
  writer.setUseCompoundFile(false);
  indexDirectory(writer, dataDir);
  int numIndexed = writer.docCount();
  writer.optimize();
  writer.close();
  return numIndexed;
}

private static void indexDirectory(IndexWriter writer, File dir)
   throws IOException {
  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   File f = files[i];
   if (f.isDirectory()) {
    indexDirectory(writer, f); // recurse
   } else if (f.getName().endsWith(".txt")) {
    indexFile(writer, f);
   }
  }
}

private static void indexFile(IndexWriter writer, File f)
   throws IOException {
  if (f.isHidden() || !f.exists() || !f.canRead()) {
   return;
  }
  System.out.println("Indexing " + f.getCanonicalPath());

  // 建立索引关键代码
  Document doc = new Document();
  doc.add(Field.Text("contents", new FileReader(f)));
  doc.add(Field.Keyword("filename", f.getCanonicalPath()));
  writer.addDocument(doc);
}
}

SearchExec.java(搜索程序):
import java.io.File;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class SearchExec {
public static void main(String[] args) throws Exception {
  File indexDir = new File("c:\\\\temp");
  Directory fsDir = FSDirectory.getDirectory(indexDir, false);

  IndexSearcher is = new IndexSearcher(fsDir);

  Query query = QueryParser.parse("java AND NOT lucene", "contents",
    new StandardAnalyzer());

  Hits hits = is.search(query);
  for (int i = 0; i < hits.length(); i++) {
   Document doc = hits.doc(i);
   System.out.println(doc.get("filename"));
  }
}
}

3、增加了图形界面的完整应用程序:
http://www.njmars.net/dispbbs.asp?boardID=12&ID=505&page=1

[此贴子已经被作者于2010-12-14 09:33:35编辑过]