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

     1  package tokenattributes
     2  
     3  import (
     4  	"github.com/balzaczyy/golucene/core/util"
     5  )
     6  
     7  /*
     8  This attribute is requested by TermsHashPerField to index the
     9  contents. This attribute can be used to customize the final []byte
    10  encoding of terms.
    11  
    12  Consumers of this attribute call BytesRef() up-front, and then invoke
    13  FillBytesRef() for each term. Examle:
    14  
    15  	termAtt := tokenStream.Get("TermToBytesRefAttribute")
    16  	bytes := termAtt.BytesRef();
    17  
    18  	var err error
    19  	var ok bool
    20  	for ok, err = tokenStream.IncrementToken(); ok && err == nil; ok, err = tokenStream.IncrementToken() {
    21  
    22  		// you must call termAtt.FillBytesRef() before doing something with the bytes.
    23  		// this encodes the term value (internally it might be a []rune, etc) into the bytes.
    24  		hashCode := termAtt.FillBytesRef()
    25  
    26  		if isIntersting(bytes) {
    27  
    28  			// becaues the bytes are reused by the attribute (like CharTermAttribute's []rune buffer),
    29  			// you should make a copy if you need persistent access to the bytes, otherwise they will
    30  			// be rewritten across calls to IncrementToken()
    31  
    32  			clone := make([]byte, len(bytes))
    33  			copy(clone, bytes)
    34  			doSomethingWith(cone)
    35  		}
    36  	}
    37  	...
    38  */
    39  type TermToBytesRefAttribute interface {
    40  	// Updates the bytes BytesRef() to contain this term's final
    41  	// encoding.
    42  	FillBytesRef()
    43  	// Retrieve this attribute's BytesRef. The bytes are updated from
    44  	// the current term when the consumer calls FillBytesRef().
    45  	BytesRef() *util.BytesRef
    46  }