<commit_before>//  Copyright (c) 2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.
package search

import (
	// "math"
	// "reflect"
	"testing"

	// "github.com/couchbaselabs/bleve/index"
	"github.com/couchbaselabs/bleve/document"
	"github.com/couchbaselabs/bleve/index/mock"
)

func TestTermSearcher(t *testing.T) {

	query := TermQuery{
		Term:     "beer",
		Field:    "desc",
		BoostVal: 1.0,
		Explain:  true,
	}

	i := mock.NewMockIndex()
	i.Update(&document.Document{
		ID: "a",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "b",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "c",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})

	searcher, err := NewTermSearcher(i, &query)
	if err != nil {
		t.Fatal(err)
	}

	if searcher.Count() != 3 {
		t.Errorf("expected count of 3, got %d", searcher.Count())
	}

	docMatch, err := searcher.Next()
	if err != nil {
		t.Errorf("expected result, got %v", err)
	}
	if docMatch.ID != "a" {
		t.Errorf("expected result ID to be 'a', got '%s", docMatch.ID)
	}
	docMatch, err = searcher.Advance("c")
	if err != nil {
		t.Errorf("expected result, got %v", err)
	}
	if docMatch.ID != "c" {
		t.Errorf("expected result ID to be 'c' got '%s'", docMatch.ID)
	}

	// try advancing past end
	docMatch, err = searcher.Advance("f")
	if err != nil {
		t.Fatal(err)
	}
	if docMatch != nil {
		t.Errorf("expected nil, got %v", docMatch)
	}

	// try pushing next past end<commit_msg>improved test<commit_after>//  Copyright (c) 2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.
package search

import (
	"math"
	"testing"

	"github.com/couchbaselabs/bleve/document"
	"github.com/couchbaselabs/bleve/index/mock"
)

func TestTermSearcher(t *testing.T) {

	query := TermQuery{
		Term:     "beer",
		Field:    "desc",
		BoostVal: 3.0,
		Explain:  true,
	}

	i := mock.NewMockIndex()
	i.Update(&document.Document{
		ID: "a",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "b",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "c",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "d",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "e",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "f",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "g",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "h",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "i",
		Fields: []*document.Field{
			document.NewTextField("desc", []byte("beer")),
		},
	})
	i.Update(&document.Document{
		ID: "j",
		Fields: []*document.Field{
			document.NewTextField("title", []byte("cat")),
		},
	})

	searcher, err := NewTermSearcher(i, &query)
	if err != nil {
		t.Fatal(err)
	}
	defer searcher.Close()

	searcher.SetQueryNorm(2.0)
	idf := 1.0 + math.Log(float64(i.DocCount())/float64(searcher.Count()+1.0))
	expectedQueryWeight := 3 * idf * 3 * idf
	if expectedQueryWeight != searcher.Weight() {
		t.Errorf("expected weight %v got %v", expectedQueryWeight, searcher.Weight())
	}

	if searcher.Count() != 9 {
		t.Errorf("expected count of 9, got %d", searcher.Count())
	}

	docMatch, err := searcher.Next()
	if err != nil {
		t.Errorf("expected result, got %v", err)
	}
	if docMatch.ID != "a" {
		t.Errorf("expected result ID to be 'a', got '%s", docMatch.ID)
	}
	docMatch, err = searcher.Advance("c")
	if err != nil {
		t.Errorf("expected result, got %v", err)
	}
	if docMatch.ID != "c" {
		t.Errorf("expected result ID to be 'c' got '%s'", docMatch.ID)
	}

	// try advancing past end
	docMatch, err = searcher.Advance("z")
	if err != nil {
		t.Fatal(err)
	}
	if docMatch != nil {
		t.Errorf("expected nil, got %v", docMatch)
	}

	// try pushing next past end
{'fraction_non_alphanumeric': 0.17458256029684602, 'fraction_numerical': 0.006679035250463822, 'mean_word_length': 3.7289473684210526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7360851', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1943, 'n_words': 502}