Document 包分析

理解 Document 

Lucene 没有定义数据源 , 而是定义了一个通用的文档结构 , 这个文档结构就是 LuceneDocument 包下的 Document 类 . 

一个 Document 对应于你在进行网页抓取的时候一个 msword, 一个 pdf, 一个 html, 一个 text 等 .Lucene 的这种形式可以定义 

非常灵活的应用 , 只要前端有相应的转换器把数据源转成 Document 结构就可以了 . 

一个 Document 内部维护一个 Field 的 vector. 

好 , 我们一起来看一下 document 的核心源码 ( 只有定义 , 没有实现 ) 

public final class Document implements java.io.Serializable { 

  List fields = new Vector();// 成员变量 

  //boost 用来表示此 document 的重要程度 , 默认为 1.0, 会作用于 document 中的所有的 field 

  private float boost = 1.0f; 

  public Document() {} 

  public void setBoost(float boost) {this.boost = boost;} 

  public float getBoost() {return boost;} 

  public final void add(Field field) 

  public final void removeField(String name)    

  public final void removeFields(String name) 

  public final Field getField(String name) 

  public final String get(String name) 

  public final Enumeration fields() 

  public final Field[] getFields(String name) 

  public final String[] getValues(String name) 

  public final String toString() 

理解 Field 

刚才提到一个 Document 中有一个用来存储 Field 的 vector, 那么什么是 Field. 你可以简单的认为 Field 是一个 <name,value> 

name 为域( Field )的名字,例如 title , body , subject , data 等等。 value 就是文本。我们来看一下源码定义 , 不就 OK 了 . 

( 由于 Field 是 Lucene 中非常重要的概念 , 所以我们拿来源码看一下 ) 

public final class Field implements java.io.Serializable { 

  private String  name = "body"; 

  private String  stringValue = null; 

  private boolean storeTermVector = false; 

  private Reader  readerValue = null; 

  private boolean isStored = false; 

  private boolean isIndexed = true; 

  private boolean isTokenized = true; 

  /* 以前一直不了解 boost 为何?其实 boost 就是由于后来进行相关度排序时用的 , 由于在 query 时, 

  * 每个 term 都分属与一个 field 。同样的 term 当其属于不同的 field 时,其重要性不一样,譬如 

  *field:<title> 中的 term 就要比 field:<content> 中的 term 重要!而这个重要性如何体现就 

  * 可以通过 boost 进行设定。可以把 field:<title> 的 boost 至设大一些 

* 注意 boost 在 Document 中还有整个的设定 . 

  */ 

  private float   boost = 1.0f; 

  public void setBoost(float boost) {this.boost = boost;} 

  public float getBoost() { return boost;} 

  public static final Field Keyword(String name, String value) {return new Field(name, value, true, true, false);} 

  public static final Field UnIndexed(String name, String value) {return new Field(name, value, true, false, false);} 

  public static final Field Text(String name, String value) {return Text(name, value, false);} 

  public static final Field Keyword(String name, Date value) {return new Field(name, DateField.dateToString(value), true, true, false);} 

  public static final Field Text(String name, String value, boolean storeTermVector) { 

    return new Field(name, value, true, true, true, storeTermVector);} 

  public static final Field UnStored(String name, String value) { 

    return UnStored(name, value, false);} 



  




  

  public static final Field UnStored(String name, String value, boolean storeTermVector) { 

    return new Field(name, value, false, true, true, storeTermVector); } 



  




  

  public static final Field Text(String name, Reader value) { 

    return Text(name, value, false);} 

  public static final Field Text(String name, Reader value, boolean storeTermVector) { 

    Field f = new Field(name, value); 

    f.storeTermVector = storeTermVector; 

    return f; 

  } 

  public String name()         { return name; } 

  public String stringValue()         { return stringValue; } 

  public Reader readerValue()       { return readerValue; } 

  public Field(String name, String string, 

                                     boolean store, boolean index, boolean token) { 

    this(name, string, store, index, token, false); 

  } 

// 最低层的构造函数 

  public Field(String name, String string, 

                                     boolean store, boolean index, boolean token, boolean storeTermVector) 



  




  

  Field(String name, Reader reader) 

  public final boolean    isStored()      { return isStored; } 

  public final boolean    isIndexed()    { return isIndexed; } 

  public final boolean    isTokenized()        { return isTokenized; } 

  public final boolean isTermVectorStored() { return storeTermVector; } 

    public final String toString() 

  public final String toString2()// 我加的用来返回六元组 

} 

代码可能看起来有点长 , 不过看一下就知道了 Field 其实是一个六元组 , 咱们上文说其是 <name,value> 对是一种简化形式 . 

Field 的六元组形式为 <name,stringValue,isStored,isIndexed,isTokenized,isTermVectorStored>,Field 提供了不同的构造函数 

主要有一下几个 




方法 


切词 


索引 


存储 


用途 




Field.Text(String name, String value) 


Yes 


Yes 


Yes 


切分 , 索引 , 并存储,比如: title , subject 




Field Text(String name, Reader value) 


Yes 


Yes 


Yes 


与上面同 , Term Vector 并不存储此 Field 




Field Text(String name, String value, boolean storeTermVector) 


Yes 


Yes 


Yes 


切分 , 索引 , 存储,比如: title,subject. 于上面不同的加入了一个控制变量 




Field Text(String name, Reader value, boolean storeTermVector) 


Yes 


Yes 


Yes 


切分 , 索引 , 存储,比如: title,subject. 于上面不同的加入了一个控制变量 




Field.Keyword(String name, String value) 


No 


Yes 


Yes 


不切分 , 索引 , 存储,比如: date,url 




Field Keyword(String name, Date value) 


 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 

不切分 , 存储 , 索引 , 用来返回 hits 




Field.UnIndexed(String name, String value) 


No 


No 


Yes 


不切分 , 不索引,存储,比如:文件路径 




Field.UnStored(String name, String value) 


Yes 


Yes 


No 


只全文索引,不存储 




Field UnStored(String name, String value, boolean storeTermVector) 


Yes 


Yes 


No 


于上面相同 , 不同的是加入了一个控制变量 


总的来看 ,Field 的构造函数就只有四种形式 ,Text,KeyWord,UnIndexed,UnStored, 只不过每种函数往往有多种变形罢了 . 

编一段代码来测试一下 Document 类和 Field 类 

public class TestDocument 

{ 

  private Document makeDocumentWithFields() throws IOException 

  { 

    Document doc = new Document(); 

    doc.add(Field.Text("title","title")); 

    doc.add(Field.Text("subject","ubject")); 

    doc.add(Field.Keyword("date","2005.11.12")); 

    doc.add(Field.Keyword("url","www.tju.edu.cn")); 

    doc.add(Field.UnIndexed("filepath","D://Lucene")); 

    doc.add(Field.UnStored("unstored","This field is unstored")); 

    Field field; 

    for(int i=0;i<doc.fields.size();i++) 

    { 

      field =(Field)doc.fields.get(i); 

      System.out.println(field.toString()); 

      System.out.println(" 对应的六元组形式为 "); 

      System.out.println(field.toString2()); 

    } 

    return doc; 

  } 

  public void GetValuesForIndexedDocument() throws IOException 

  { 

    RAMDirectory dir = new RAMDirectory(); 

    IndexWriter writer = new IndexWriter(dir,new StandardAnalyzer(),true); 

    writer.addDocument(makeDocumentWithFields()); 

    writer.close(); 

    

    Searcher searcher = new IndexSearcher(dir); 

    Query query = new TermQuery(new Term("title","title")); 

    //Hits 由匹配的 Document 组成 . 

    Hits hits = searcher.search(query); 

    System.out.println("Document 的结构形式 "); 

    System.out.println(hits.doc(0)); 

 

转载于:https://www.cnblogs.com/lcuzhanglei/archive/2012/07/31/2616688.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值