lucene的建立检索学习实例源码

/**
 * 测试索引
 * @author Administrator
 *
 */
import java.io.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;

public class indexTest
{
 //声明索引路劲常量
 private static final String INDEX_STORE_PATH = "D:\\test";

 public static void main(String[] args)throws IOException
 {
  indexwriter();
  indexrriter();
  System.out.println("1.&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
  indexwriterONE();
  indexrriter();
  //System.out.println("2.&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
  //indexwriterONE();
  //indexrriter();
  while(true)
  {
  System.out.println("请输入查找的关键字:");
  seacher();
  }
 }

 /*
  *创建索引
  */
 public static void indexwriter() throws IOException
 {
  IndexWriter writer=new IndexWriter( INDEX_STORE_PATH ,new StandardAnalyzer(),true);
  writer.setUseCompoundFile(false);
  //创建三个文档
  Document doc1=new Document();
  Document doc2=new Document();
  Document doc3=new Document();
  
  //创建域
  Field f1=new Field("bookname","钢铁是怎么样练成的",Field.Store.YES,Field.Index.TOKENIZED);
  Field f2=new Field("bookname","英雄的女儿",Field.Store.YES,Field.Index.TOKENIZED);
  Field f3=new Field("bookname","篱笆女人和狗",Field.Store.YES,Field.Index.TOKENIZED);
  
  //将域分别添加到文档中
  doc1.add(f1);
  doc2.add(f2);
  doc3.add(f3);
  
  //将文件写入到索引写作器,将三个文档写入磁盘
  writer.addDocument(doc1);
  writer.addDocument(doc2);
  writer.addDocument(doc3);
  //关闭写作器
  writer.close();
  
  System.out.println("索引建立完毕-------");
 }
 
 /*
  *使用IndexReader读取索引
  */
 public static void indexrriter()
 {
  try{
  //创建阅读器
  IndexReader reader =IndexReader.open(INDEX_STORE_PATH);
  //显示所有的document文档
  System.out.println("索引文档列表为:");
  for(int i=0;i<reader.numDocs();i++)
   System.out.println(reader.document(i));
  //输出当前索引的版本号
  System.out.println("版本号:" + reader.getVersion());
  //当前索引的文档数
  System.out.println("索引内的文档数有:" + reader.numDocs());
  
  //关闭reader
  reader.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
 
 /*
  *再添加一个文档到索引
  */
 static void indexwriterONE()
 {
  try
  {
   //由于只是添加,所以在IndexWriter()中的indexCreate参数设置为false
   IndexWriter writer=new IndexWriter( INDEX_STORE_PATH ,new StandardAnalyzer(),false);
   writer.setUseCompoundFile(false);
   
   Document doc4=new Document();
   Field f4=new Field("bookename","钢铁战士",Field.Store.YES,Field.Index.TOKENIZED);
   
   doc4.add(f4);
   writer.addDocument(doc4);
   
   writer.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 
 /*
  *构造条词并在索引中查找(测试索引结果)
  *
  **/
 static void seacher() throws IOException
 {
  //创建阅读器
  IndexReader reader =IndexReader.open(INDEX_STORE_PATH);
  //输入关键字
  BufferedReader KBreader= new BufferedReader(new InputStreamReader(System.in));
  String keyword=KBreader.readLine();
  System.out.println("查找条词"+ "'"+ keyword +"'");
  //构造条词
  Term term= new Term("bookname",keyword);
  TermDocs docs=reader.termDocs(term);
  
  int i=0;
  while(docs.next())
  {
      i+=1;
     System.out.println("--------------");
     System.out.println("含有所查找的<"+ term +">的document的编号是:"+docs.doc());
     System.out.println(term+"在文档中出现的次数为"+docs.freq());
     System.out.println("--------------");
     
  
  }
  
  System.out.println("-------12133-------"+i);
  //关闭KBreader和reader
  //KBreader.close();
  reader.close();
 } 
}

 

你可能感兴趣的:(Lucene)