Skip to content

Commit 12aa9e3

Browse files
code review
1 parent 08b4a11 commit 12aa9e3

File tree

7 files changed

+24
-40
lines changed

7 files changed

+24
-40
lines changed

document/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131
}
3232

3333
type Document struct {
34-
id string `json:"id"`
34+
id string
3535
Fields []Field `json:"fields"`
3636
NestedDocuments []*Document `json:"nested_documents"`
3737
CompositeFields []*CompositeField

index/scorch/snapshot_index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,8 @@ func (i *IndexSnapshot) Ancestors(ID index.IndexInternalID) ([]index.IndexIntern
12381238
rv[0] = index.NewIndexInternalID(nil, ldoc+globalOffset)
12391239

12401240
// then all ancestors shifted by +1
1241-
for j := 1; j < len(ancestors)+1; j++ {
1242-
rv[j] = index.NewIndexInternalID(nil, ancestors[j-1]+globalOffset)
1241+
for j := 0; j < len(ancestors); j++ {
1242+
rv[j+1] = index.NewIndexInternalID(nil, ancestors[j]+globalOffset)
12431243
}
12441244
return rv, nil
12451245
}

mapping/document.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,9 @@ func (dm *DocumentMapping) walkDocument(data interface{}, path []string, indexes
455455
nestedContext := context.im.newWalkContext(nestedDocument, dm)
456456
dm.processProperty(fieldVal, path, append(indexes, uint64(i)), nestedContext)
457457
context.doc.AddNestedDocument(nestedDocument)
458-
continue
458+
} else {
459+
dm.processProperty(fieldVal, path, append(indexes, uint64(i)), context)
459460
}
460-
dm.processProperty(fieldVal, path, append(indexes, uint64(i)), context)
461461
}
462462
}
463463
case reflect.Ptr:

search/collector/eligible.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (ec *EligibleCollector) Collect(ctx context.Context, searcher search.Search
144144
}
145145

146146
if ec.nestedStore != nil {
147-
var count int
147+
var count uint64
148148
err := ec.nestedStore.VisitRoots(func(doc *search.DocumentMatch) error {
149149
// process the root document
150150
if err := dmHandler(doc); err != nil {
@@ -156,7 +156,7 @@ func (ec *EligibleCollector) Collect(ctx context.Context, searcher search.Search
156156
if err != nil {
157157
return err
158158
}
159-
ec.total = uint64(count)
159+
ec.total = count
160160
}
161161

162162
// help finalize/flush the results in case

search/collector/knn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (hc *KNNCollector) Collect(ctx context.Context, searcher search.Searcher, r
223223
}
224224

225225
if hc.nestedStore != nil {
226-
var count int
226+
var count uint64
227227
err := hc.nestedStore.VisitRoots(func(doc *search.DocumentMatch) error {
228228
// process the root document
229229
if err := dmHandler(doc); err != nil {
@@ -235,7 +235,7 @@ func (hc *KNNCollector) Collect(ctx context.Context, searcher search.Searcher, r
235235
if err != nil {
236236
return err
237237
}
238-
hc.total = uint64(count)
238+
hc.total = count
239239
}
240240

241241
// help finalize/flush the results in case

search/collector/topn.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ const CheckDoneEvery = uint64(1024)
9090
// skipping over the first 'skip' hits
9191
// ordering hits by the provided sort order
9292
func NewTopNCollector(size int, skip int, sort search.SortOrder) *TopNCollector {
93-
return newTopNCollector(size, skip, sort)
93+
return newTopNCollector(size, skip, sort, nil)
9494
}
9595

9696
// NewTopNCollectorAfter builds a collector to find the top 'size' hits
9797
// skipping over the first 'skip' hits
9898
// ordering hits by the provided sort order
9999
// starting after the provided 'after' sort values
100100
func NewTopNCollectorAfter(size int, sort search.SortOrder, after []string) *TopNCollector {
101-
rv := newTopNCollector(size, 0, sort)
101+
rv := newTopNCollector(size, 0, sort, nil)
102102
rv.searchAfter = createSearchAfterDocument(sort, after)
103103
return rv
104104
}
@@ -109,7 +109,7 @@ func NewTopNCollectorAfter(size int, sort search.SortOrder, after []string) *Top
109109
// while ensuring the nested documents are handled correctly
110110
// (i.e. parent document is returned instead of nested document)
111111
func NewNestedTopNCollector(size int, skip int, sort search.SortOrder, nr index.NestedReader) *TopNCollector {
112-
return newNestedTopNCollector(size, skip, sort, nr)
112+
return newTopNCollector(size, skip, sort, nr)
113113
}
114114

115115
// NewNestedTopNCollectorAfter builds a collector to find the top 'size' hits
@@ -119,37 +119,21 @@ func NewNestedTopNCollector(size int, skip int, sort search.SortOrder, nr index.
119119
// while ensuring the nested documents are handled correctly
120120
// (i.e. parent document is returned instead of nested document)
121121
func NewNestedTopNCollectorAfter(size int, sort search.SortOrder, after []string, nr index.NestedReader) *TopNCollector {
122-
rv := newNestedTopNCollector(size, 0, sort, nr)
122+
rv := newTopNCollector(size, 0, sort, nr)
123123
rv.searchAfter = createSearchAfterDocument(sort, after)
124124
return rv
125125
}
126126

127-
func newTopNCollector(size int, skip int, sort search.SortOrder) *TopNCollector {
127+
func newTopNCollector(size int, skip int, sort search.SortOrder, nr index.NestedReader) *TopNCollector {
128128
hc := &TopNCollector{size: size, skip: skip, sort: sort}
129129

130130
hc.store = getOptimalCollectorStore(size, skip, func(i, j *search.DocumentMatch) int {
131131
return hc.sort.Compare(hc.cachedScoring, hc.cachedDesc, i, j)
132132
})
133133

134-
// these lookups traverse an interface, so do once up-front
135-
if sort.RequiresDocID() {
136-
hc.needDocIds = true
134+
if nr != nil {
135+
hc.nestedStore = newStoreNested(nr)
137136
}
138-
hc.neededFields = sort.RequiredFields()
139-
hc.cachedScoring = sort.CacheIsScore()
140-
hc.cachedDesc = sort.CacheDescending()
141-
142-
return hc
143-
}
144-
145-
func newNestedTopNCollector(size int, skip int, sort search.SortOrder, nr index.NestedReader) *TopNCollector {
146-
hc := &TopNCollector{size: size, skip: skip, sort: sort}
147-
148-
hc.store = getOptimalCollectorStore(size, skip, func(i, j *search.DocumentMatch) int {
149-
return hc.sort.Compare(hc.cachedScoring, hc.cachedDesc, i, j)
150-
})
151-
152-
hc.nestedStore = newStoreNested(nr)
153137

154138
// these lookups traverse an interface, so do once up-front
155139
if sort.RequiresDocID() {
@@ -369,7 +353,7 @@ func (hc *TopNCollector) Collect(ctx context.Context, searcher search.Searcher,
369353
}
370354

371355
if hc.nestedStore != nil {
372-
var count int
356+
var count uint64
373357
err := hc.nestedStore.VisitRoots(func(doc *search.DocumentMatch) error {
374358
if err := hc.adjustDocumentMatch(searchContext, reader, doc); err != nil {
375359
return err
@@ -386,7 +370,7 @@ func (hc *TopNCollector) Collect(ctx context.Context, searcher search.Searcher,
386370
if err != nil {
387371
return err
388372
}
389-
hc.total = uint64(count)
373+
hc.total = count
390374
}
391375

392376
if hc.knnHits != nil {

search/explanation.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func (expl *Explanation) Size() int {
5757

5858
const MergedExplMessage = "sum of merged explanations:"
5959

60+
// MergeExpl merges two explanations into one.
61+
// If either explanation is nil, the other is returned.
62+
// If the first explanation is already a merged explanation,
63+
// the second explanation is appended to its children.
64+
// Otherwise, a new merged explanation is created
65+
// with the two explanations as its children.
6066
func MergeExpl(first, second *Explanation) *Explanation {
6167
if first == nil {
6268
return second
@@ -70,12 +76,6 @@ func MergeExpl(first, second *Explanation) *Explanation {
7076
first.Children = append(first.Children, second)
7177
return first
7278
}
73-
if second.Message == MergedExplMessage {
74-
// reuse second explanation as the merged one
75-
second.Value += first.Value
76-
second.Children = append(second.Children, first)
77-
return second
78-
}
7979
// create a new explanation to hold the merged one
8080
rv := &Explanation{
8181
Value: first.Value + second.Value,

0 commit comments

Comments
 (0)