github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/document/document.go (about)

     1  package document
     2  
     3  import (
     4  	. "github.com/balzaczyy/golucene/core/codec/spi"
     5  	. "github.com/balzaczyy/golucene/core/index/model"
     6  )
     7  
     8  // document/Document.java
     9  /** Documents are the unit of indexing and search.
    10   *
    11   * A Document is a set of fields.  Each field has a name and a textual value.
    12   * A field may be {@link org.apache.lucene.index.IndexableFieldType#stored() stored} with the document, in which
    13   * case it is returned with search hits on the document.  Thus each document
    14   * should typically contain one or more stored fields which uniquely identify
    15   * it.
    16   *
    17   * <p>Note that fields which are <i>not</i> {@link org.apache.lucene.index.IndexableFieldType#stored() stored} are
    18   * <i>not</i> available in documents retrieved from the index, e.g. with {@link
    19   * ScoreDoc#doc} or {@link IndexReader#document(int)}.
    20   */
    21  type Document struct {
    22  	fields []IndexableField
    23  }
    24  
    25  /** Constructs a new document with no fields. */
    26  func NewDocument() *Document {
    27  	return &Document{make([]IndexableField, 0)}
    28  }
    29  
    30  func (doc *Document) Fields() []IndexableField {
    31  	return doc.fields
    32  }
    33  
    34  /**
    35   * <p>Adds a field to a document.  Several fields may be added with
    36   * the same name.  In this case, if the fields are indexed, their text is
    37   * treated as though appended for the purposes of search.</p>
    38   * <p> Note that add like the removeField(s) methods only makes sense
    39   * prior to adding a document to an index. These methods cannot
    40   * be used to change the content of an existing index! In order to achieve this,
    41   * a document has to be deleted from an index and a new changed version of that
    42   * document has to be added.</p>
    43   */
    44  func (doc *Document) Add(field IndexableField) {
    45  	doc.fields = append(doc.fields, field)
    46  }
    47  
    48  /*
    49  Returns the string value of the field with the given name if any exist in
    50  this document, or null.  If multiple fields exist with this name, this
    51  method returns the first value added. If only binary fields with this name
    52  exist, returns null.
    53  
    54  For IntField, LongField, FloatField, and DoubleField, it returns the string
    55  value of the number. If you want the actual numeric field instance back, use
    56  getField().
    57  */
    58  func (doc *Document) Get(name string) string {
    59  	for _, field := range doc.fields {
    60  		if field.Name() == name && field.StringValue() != "" {
    61  			return field.StringValue()
    62  		}
    63  	}
    64  	return ""
    65  }
    66  
    67  // document/DocumentStoredFieldVisitor.java
    68  /*
    69  A StoredFieldVisitor that creates a Document containing all
    70  stored fields, or only specific requested fields provided
    71  to DocumentStoredFieldVisitor.
    72  
    73  This is used by IndexReader.Document() to load a document.
    74  */
    75  type DocumentStoredFieldVisitor struct {
    76  	*StoredFieldVisitorAdapter
    77  	doc         *Document
    78  	fieldsToAdd map[string]bool
    79  }
    80  
    81  /** Load all stored fields. */
    82  func NewDocumentStoredFieldVisitor() *DocumentStoredFieldVisitor {
    83  	return &DocumentStoredFieldVisitor{
    84  		doc: NewDocument(),
    85  	}
    86  }
    87  
    88  func (visitor *DocumentStoredFieldVisitor) BinaryField(fi *FieldInfo, value []byte) error {
    89  	panic("not implemented yet")
    90  	// visitor.doc.add(newStoredField(fieldInfo.name, value))
    91  	// return nil
    92  }
    93  
    94  func (visitor *DocumentStoredFieldVisitor) StringField(fi *FieldInfo, value string) error {
    95  	ft := NewFieldTypeFrom(TEXT_FIELD_TYPE_STORED)
    96  	ft.storeTermVectors = fi.HasVectors()
    97  	ft.indexed = fi.IsIndexed()
    98  	ft._omitNorms = fi.OmitsNorms()
    99  	ft._indexOptions = fi.IndexOptions()
   100  	visitor.doc.Add(NewFieldFromString(fi.Name, value, ft))
   101  	return nil
   102  }
   103  
   104  func (visitor *DocumentStoredFieldVisitor) IntField(fi *FieldInfo, value int) error {
   105  	panic("not implemented yet")
   106  }
   107  
   108  func (visitor *DocumentStoredFieldVisitor) LongField(fi *FieldInfo, value int64) error {
   109  	panic("not implemented yet")
   110  }
   111  
   112  func (visitor *DocumentStoredFieldVisitor) FloatField(fi *FieldInfo, value float32) error {
   113  	panic("not implemented yet")
   114  }
   115  
   116  func (visitor *DocumentStoredFieldVisitor) DoubleField(fi *FieldInfo, value float64) error {
   117  	panic("not implemented yet")
   118  }
   119  
   120  func (visitor *DocumentStoredFieldVisitor) NeedsField(fi *FieldInfo) (status StoredFieldVisitorStatus, err error) {
   121  	if visitor.fieldsToAdd == nil {
   122  		status = STORED_FIELD_VISITOR_STATUS_YES
   123  	} else if _, ok := visitor.fieldsToAdd[fi.Name]; ok {
   124  		status = STORED_FIELD_VISITOR_STATUS_YES
   125  	} else {
   126  		status = STORED_FIELD_VISITOR_STATUS_NO
   127  	}
   128  	return
   129  }
   130  
   131  func (visitor *DocumentStoredFieldVisitor) Document() *Document {
   132  	return visitor.doc
   133  }
   134  
   135  type StoredFieldVisitorAdapter struct{}
   136  
   137  func (va *StoredFieldVisitorAdapter) BinaryField(fi *FieldInfo, value []byte) error  { return nil }
   138  func (va *StoredFieldVisitorAdapter) StringField(fi *FieldInfo, value string) error  { return nil }
   139  func (va *StoredFieldVisitorAdapter) IntField(fi *FieldInfo, value int) error        { return nil }
   140  func (va *StoredFieldVisitorAdapter) LongField(fi *FieldInfo, value int64) error     { return nil }
   141  func (va *StoredFieldVisitorAdapter) FloatField(fi *FieldInfo, value float32) error  { return nil }
   142  func (va *StoredFieldVisitorAdapter) DoubleField(fi *FieldInfo, value float64) error { return nil }