Do field cache only once per search
This commit is contained in:
parent
9a9b630a6d
commit
ce64c17be1
|
@ -77,7 +77,7 @@ type IndexReader interface {
|
|||
|
||||
Document(id string) (*document.Document, error)
|
||||
DocumentFieldTerms(id string) (FieldTerms, error)
|
||||
DocumentFieldTermsForFields(id string, fieldIDs []uint16, fields []string) (FieldTerms, error)
|
||||
DocumentFieldTermsForFields(id string, fields map[string]uint16) (FieldTerms, error)
|
||||
|
||||
Fields() ([]string, error)
|
||||
FieldIDs(fields []string) ([]uint16, error)
|
||||
|
|
|
@ -10,10 +10,11 @@
|
|||
package upside_down
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/blevesearch/bleve/document"
|
||||
"github.com/blevesearch/bleve/index"
|
||||
"github.com/blevesearch/bleve/index/store"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type IndexReader struct {
|
||||
|
@ -111,21 +112,21 @@ func (i *IndexReader) DocumentFieldTerms(id string) (index.FieldTerms, error) {
|
|||
return rv, nil
|
||||
}
|
||||
|
||||
func (i *IndexReader) DocumentFieldTermsForFields(id string, fieldIDs []uint16, fields []string) (index.FieldTerms, error) {
|
||||
func (i *IndexReader) DocumentFieldTermsForFields(id string, fields map[string]uint16) (index.FieldTerms, error) {
|
||||
back, err := i.index.backIndexRowForDoc(i.kvreader, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rv := make(index.FieldTerms, len(fieldIDs))
|
||||
rv := make(index.FieldTerms, len(fields))
|
||||
for _, entry := range back.termEntries {
|
||||
for id, field := range fieldIDs {
|
||||
if field == uint16(*entry.Field) {
|
||||
terms, ok := rv[fields[id]]
|
||||
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[fields[id]] = terms
|
||||
rv[field] = terms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
search.go
16
search.go
|
@ -191,8 +191,6 @@ func (h *HighlightRequest) AddField(field string) {
|
|||
// Facets describe the set of facets to be computed.
|
||||
// Explain triggers inclusion of additional search
|
||||
// result score explanations.
|
||||
// Sort specifies the sorting for the returned results
|
||||
// results will be sorted by score if this is empty
|
||||
//
|
||||
// A special field named "*" can be used to return all fields.
|
||||
type SearchRequest struct {
|
||||
|
@ -203,7 +201,6 @@ type SearchRequest struct {
|
|||
Fields []string `json:"fields"`
|
||||
Facets FacetsRequest `json:"facets"`
|
||||
Explain bool `json:"explain"`
|
||||
Sort map[string]bool `json:"sort"`
|
||||
}
|
||||
|
||||
func (sr *SearchRequest) Validate() error {
|
||||
|
@ -223,14 +220,6 @@ func (r *SearchRequest) AddFacet(facetName string, f *FacetRequest) {
|
|||
r.Facets[facetName] = f
|
||||
}
|
||||
|
||||
// AddSort field in ascending or descending direction
|
||||
func (r *SearchRequest) AddSort(field string, ascending bool) {
|
||||
if r.Sort == nil {
|
||||
r.Sort = make(map[string]bool)
|
||||
}
|
||||
r.Sort[field] = ascending
|
||||
}
|
||||
|
||||
// UnmarshalJSON deserializes a JSON representation of
|
||||
// a SearchRequest
|
||||
func (r *SearchRequest) UnmarshalJSON(input []byte) error {
|
||||
|
@ -242,7 +231,6 @@ func (r *SearchRequest) UnmarshalJSON(input []byte) error {
|
|||
Fields []string `json:"fields"`
|
||||
Facets FacetsRequest `json:"facets"`
|
||||
Explain bool `json:"explain"`
|
||||
Sort map[string]bool `json:"sort"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(input, &temp)
|
||||
|
@ -260,7 +248,6 @@ func (r *SearchRequest) UnmarshalJSON(input []byte) error {
|
|||
r.Highlight = temp.Highlight
|
||||
r.Fields = temp.Fields
|
||||
r.Facets = temp.Facets
|
||||
r.Sort = temp.Sort
|
||||
r.Query, err = ParseQuery(temp.Q)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -293,7 +280,6 @@ func NewSearchRequestOptions(q Query, size, from int, explain bool) *SearchReque
|
|||
Size: size,
|
||||
From: from,
|
||||
Explain: explain,
|
||||
Sort: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -321,7 +307,7 @@ func (iem IndexErrMap) UnmarshalJSON(data []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SearchStatus is a section in the SearchResult reporting how many
|
||||
// SearchStatus is a secion in the SearchResult reporting how many
|
||||
// underlying indexes were queried, how many were successful/failed
|
||||
// and a map of any errors that were encountered
|
||||
type SearchStatus struct {
|
||||
|
|
|
@ -24,29 +24,38 @@ 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 {
|
||||
fields = append(fields, facetBuilder.Field())
|
||||
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]
|
||||
}
|
||||
}
|
||||
fieldIds, err := fb.indexReader.FieldIDs(fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fieldTerms, err := fb.indexReader.DocumentFieldTermsForFields(docMatch.ID, fieldIds, fields)
|
||||
fieldTerms, err := fb.indexReader.DocumentFieldTermsForFields(docMatch.ID, fb.fieldIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue