// Will lower case all string values in and queries tothis index.
// May get better accurate results, but at a slight performance cost.
ToLower bool
-
- // Counts the number of documents in the index.
- numDocs int
-}
-
-func (cfg *IndexConfig) incrNumDocs() {
- cfg.numDocs++
}
// Document is the interface an indexable document in Hugo must fulfill.
type InvertedIndex struct {
cfg Config
index map[string]map[Keyword][]Document
+ // Counts the number of documents added to each index.
+ indexDocCount map[string]int
minWeight int
maxWeight int
// NewInvertedIndex creates a new InvertedIndex.
// Documents to index must be added in Add.
func NewInvertedIndex(cfg Config) *InvertedIndex {
- idx := &InvertedIndex{index: make(map[string]map[Keyword][]Document), cfg: cfg}
+ idx := &InvertedIndex{index: make(map[string]map[Keyword][]Document), indexDocCount: make(map[string]int), cfg: cfg}
for _, conf := range cfg.Indices {
idx.index[conf.Name] = make(map[Keyword][]Document)
if conf.Weight < idx.minWeight {
panic("index is finalized")
}
var err error
- for i, config := range idx.cfg.Indices {
+ for _, config := range idx.cfg.Indices {
if config.Weight == 0 {
// Disabled
continue
}
if added {
- c := &idx.cfg.Indices[i]
- (*c).incrNumDocs()
+ idx.indexDocCount[config.Name]++
}
}
}
continue
}
setm := idx.index[config.Name]
- numDocs := config.numDocs
- if numDocs == 0 {
+ if idx.indexDocCount[config.Name] == 0 {
continue
}
// Remove high cardinality terms.
+ numDocs := idx.indexDocCount[config.Name]
for k, v := range setm {
percentageWithKeyword := int(math.Ceil(float64(len(v)) / float64(numDocs) * 100))
if percentageWithKeyword > config.CardinalityThreshold {