0
0
Fork 0

added test and fixed behavior to ensure correct value is stored

optimization introduced last week inadvertently meant we were
not preserving the original byte values of text fields that
were stored
This commit is contained in:
Marty Schoch 2015-01-19 15:40:18 -05:00
parent 3ea1894dea
commit 7e3ba85b9d
2 changed files with 46 additions and 1 deletions

View File

@ -40,7 +40,14 @@ func (t *TextField) Options() IndexingOptions {
func (t *TextField) Analyze() (int, analysis.TokenFrequencies) {
var tokens analysis.TokenStream
if t.analyzer != nil {
tokens = t.analyzer.Analyze(t.Value())
bytesToAnalyze := t.Value()
if t.options.IsStored() {
// need to copy
bytesCopied := make([]byte, len(bytesToAnalyze))
copy(bytesCopied, bytesToAnalyze)
bytesToAnalyze = bytesCopied
}
tokens = t.analyzer.Analyze(bytesToAnalyze)
} else {
tokens = analysis.TokenStream{
&analysis.Token{

View File

@ -325,3 +325,41 @@ func (s *sawDataWriter) Write(p []byte) (n int, err error) {
s.sawData = true
return len(p), nil
}
func TestStoredFieldPreserved(t *testing.T) {
defer os.RemoveAll("testidx")
index, err := New("testidx", NewIndexMapping())
if err != nil {
t.Fatal(err)
}
doca := map[string]interface{}{
"name": "Marty",
"desc": "GopherCON India",
}
err = index.Index("a", doca)
if err != nil {
t.Error(err)
}
q := NewTermQuery("marty")
req := NewSearchRequest(q)
req.Fields = []string{"name", "desc"}
res, err := index.Search(req)
if err != nil {
t.Error(err)
}
if len(res.Hits) != 1 {
t.Error("expected 1 hit, got %d", len(res.Hits))
}
if res.Hits[0].Fields["name"] != "Marty" {
t.Errorf("expected 'Marty' got '%s'", res.Hits[0].Fields["name"])
}
if res.Hits[0].Fields["desc"] != "GopherCON India" {
t.Errorf("expected 'GopherCON India' got '%s'", res.Hits[0].Fields["desc"])
}
}