Remove the field IDs from outside of the index
This commit is contained in:
parent
ce64c17be1
commit
fc990bc2d1
|
@ -77,10 +77,9 @@ type IndexReader interface {
|
|||
|
||||
Document(id string) (*document.Document, error)
|
||||
DocumentFieldTerms(id string) (FieldTerms, error)
|
||||
DocumentFieldTermsForFields(id string, fields map[string]uint16) (FieldTerms, error)
|
||||
DocumentFieldTermsForFields(id string, fields []string) (FieldTerms, error)
|
||||
|
||||
Fields() ([]string, error)
|
||||
FieldIDs(fields []string) ([]uint16, error)
|
||||
|
||||
GetInternal(key []byte) ([]byte, error)
|
||||
|
||||
|
|
|
@ -112,22 +112,28 @@ func (i *IndexReader) DocumentFieldTerms(id string) (index.FieldTerms, error) {
|
|||
return rv, nil
|
||||
}
|
||||
|
||||
func (i *IndexReader) DocumentFieldTermsForFields(id string, fields map[string]uint16) (index.FieldTerms, error) {
|
||||
func (i *IndexReader) DocumentFieldTermsForFields(id string, fields []string) (index.FieldTerms, error) {
|
||||
back, err := i.index.backIndexRowForDoc(i.kvreader, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rv := make(index.FieldTerms, len(fields))
|
||||
fieldsMap := make(map[uint16]string, len(fields))
|
||||
for _, f := range fields {
|
||||
id, ok := i.index.fieldCache.FieldNamed(f, false)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Field %s was not found in cache", f)
|
||||
}
|
||||
fieldsMap[id] = f
|
||||
}
|
||||
for _, entry := range back.termEntries {
|
||||
for field, id := range fields {
|
||||
if id == uint16(*entry.Field) {
|
||||
terms, ok := rv[field]
|
||||
if !ok {
|
||||
terms = make([]string, 0)
|
||||
}
|
||||
terms = append(terms, *entry.Term)
|
||||
rv[field] = terms
|
||||
if field, ok := fieldsMap[uint16(*entry.Field)]; ok {
|
||||
terms, ok := rv[field]
|
||||
if !ok {
|
||||
terms = make([]string, 0)
|
||||
}
|
||||
terms = append(terms, *entry.Term)
|
||||
rv[field] = terms
|
||||
}
|
||||
}
|
||||
return rv, nil
|
||||
|
@ -162,17 +168,6 @@ func (i *IndexReader) Fields() (fields []string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (i *IndexReader) FieldIDs(fields []string) (ids []uint16, err error) {
|
||||
for _, f := range fields {
|
||||
id, found := i.index.fieldCache.FieldNamed(f, false)
|
||||
if !found {
|
||||
return nil, fmt.Errorf("Field %s was not found in cache", f)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (i *IndexReader) GetInternal(key []byte) ([]byte, error) {
|
||||
internalRow := NewInternalRow(key, nil)
|
||||
return i.kvreader.Get(internalRow.Key())
|
||||
|
|
|
@ -24,38 +24,25 @@ type FacetBuilder interface {
|
|||
type FacetsBuilder struct {
|
||||
indexReader index.IndexReader
|
||||
facets map[string]FacetBuilder
|
||||
fieldIDs map[string]uint16 // Not thread safe
|
||||
}
|
||||
|
||||
func NewFacetsBuilder(indexReader index.IndexReader) *FacetsBuilder {
|
||||
return &FacetsBuilder{
|
||||
indexReader: indexReader,
|
||||
facets: make(map[string]FacetBuilder, 0),
|
||||
fieldIDs: make(map[string]uint16, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *FacetsBuilder) Add(name string, facetBuilder FacetBuilder) {
|
||||
fb.facets[name] = facetBuilder
|
||||
fieldIDs, err := fb.indexReader.FieldIDs([]string{facetBuilder.Field()})
|
||||
if err == nil {
|
||||
fb.fieldIDs[facetBuilder.Field()] = fieldIDs[0]
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *FacetsBuilder) Update(docMatch *DocumentMatch) error {
|
||||
var fields []string
|
||||
for _, facetBuilder := range fb.facets {
|
||||
field := facetBuilder.Field()
|
||||
// Just in-case we added a field since creating the facets builder
|
||||
if _, ok := fb.fieldIDs[field]; !ok {
|
||||
fieldIDs, err := fb.indexReader.FieldIDs([]string{field})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fb.fieldIDs[field] = fieldIDs[0]
|
||||
}
|
||||
fields = append(fields, facetBuilder.Field())
|
||||
}
|
||||
fieldTerms, err := fb.indexReader.DocumentFieldTermsForFields(docMatch.ID, fb.fieldIDs)
|
||||
fieldTerms, err := fb.indexReader.DocumentFieldTermsForFields(docMatch.ID, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue