Practice Every Day_6 (Lucene 小练习)

来源:互联网 发布:并查集 c语言 编辑:程序博客网 时间:2024/06/08 01:15

//创建含有三个Document的索引
//引入所需的java类
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.analysis.standard.StandardAnalyzer;

public class ThreeIndex

 public static void main(String[] args)
 {
   String indexPath="three";
   //创建IndexWriter
   IndexWriter writer=new IndexWriter(indexPath,newStandarAnalyzer());
   //创建Document
   Document doc=new Document();
   //创建Field-title
   String title="Family";
   Field field=new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   //创建Field-content
   String content="Family is short for father and mother I love you";
   field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   //创建Field-time
   String time="2012-4-24";
   field=new Field("time",time,Field.Store.YES,Field.Index.NO);
   doc.add(field);
   writer.addDocument(doc);
 

   Document doc=new Document();
   String title="Family";
   Field field=new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   String content="Family is short for father and mother I love you";
   field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   String time="2012-4-24";
   field=new Field("time",time,Field.Store.YES,Field.Index.NO);
   doc.add(field);
   writer.addDocument(doc);
 
 
   Document doc=new Document();
   String title="Family";
   Field field=new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   String content="Family is short for father and mother I love you";
   field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
   doc.add(field);
   String time="2012-4-24";
   field=new Field("time",time,Field.Store.YES,Field.Index.NO);
   doc.add(field);
   writer.addDocument(doc);
 
  
   writer.close();
   System.out.println("Index Three Created!");
 
 
 }

}

 

 

//为某一目录下的所有文件创建索引

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.analysis.standard.StandardAnalyzer;
import java.io.*;
import tool.FileText;
import tool.FielList;

public class LoopIndexer
{
 public static void main(String[] args)throws java.io.IOException
 {
    String indexPath="loop";
    IndexWriter writer=new IndexWriter(indexPath,new StandardAnalyzer());
    
    String[] files=FileList.getFiles("doc");
    int num=files.length;
    for(int i=0;i<num;i++)
    {
     javax.swing.text.Document doc=new Document();
     File f=new File(files[i]);
     String name=f.getName();
     Field field=new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED);
     doc.add(field);
     String content=FileText.getText(f);
     field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);
     doc.add(field);
     System.out.println("File:"+name+"Indexed!");
     writer.addDocument(doc);
     
       
    }
 writer.close();
 System.out.printer("Loop Index Created");
 }
 
 

}


 

原创粉丝点击