Repository: blevesearch/bleve
Pull request #1930 (2023-12-05 23:05:44)

`explain` in SearchRequest to toggle including request as part of response

abhinavdangeti: Turns out the SDK team doesn't really care about the request bit in the response payload, we're in the clear on this now.

Patch:
diff --git a/index_alias_impl.go b/index_alias_impl.go
index 28f20775..b1e33ff3 100644
--- a/index_alias_impl.go
+++ b/index_alias_impl.go
@@ -707,7 +707,9 @@ func MultiSearch(ctx context.Context, req *SearchRequest, preSearchData []map[st
 	}
 
 	// fix up original request
-	sr.Request = req
+	if req.Explain {
+		sr.Request = req
+	}
 	searchDuration := time.Since(searchStart)
 	sr.Took = searchDuration
 
diff --git a/index_impl.go b/index_impl.go
index 7b9bfc90..ddcace82 100644
--- a/index_impl.go
+++ b/index_impl.go
@@ -696,13 +696,17 @@ func (i *indexImpl) SearchInContext(ctx context.Context, req *SearchRequest) (sr
 			Total:      1,
 			Successful: 1,
 		},
-		Request:  req,
 		Hits:     hits,
 		Total:    coll.Total(),
 		MaxScore: coll.MaxScore(),
 		Took:     searchDuration,
 		Facets:   coll.FacetResults(),
 	}
+
+	if req.Explain {
+		rv.Request = req
+	}
+
 	return rv, nil
 }
 
diff --git a/search.go b/search.go
index d4f9969e..5ca170aa 100644
--- a/search.go
+++ b/search.go
@@ -441,7 +441,7 @@ func (ss *SearchStatus) Merge(other *SearchStatus) {
 // Facets - The facet results for the search.
 type SearchResult struct {
 	Status   *SearchStatus                  `json:"status"`
-	Request  *SearchRequest                 `json:"request"`
+	Request  *SearchRequest                 `json:"request,omitempty"`
 	Hits     search.DocumentMatchCollection `json:"hits"`
 	Total    uint64                         `json:"total_hits"`
 	Cost     uint64                         `json:"cost"`
@@ -471,7 +471,7 @@ func (sr *SearchResult) Size() int {
 func (sr *SearchResult) String() string {
 	rv := ""
 	if sr.Total > 0 {
-		if sr.Request.Size > 0 {
+		if sr.Request != nil && sr.Request.Size > 0 {
 			rv = fmt.Sprintf("%d matches, showing %d through %d, took %s\n", sr.Total, sr.Request.From+1, sr.Request.From+len(sr.Hits), sr.Took)
 			for i, hit := range sr.Hits {
 				rv += fmt.Sprintf("%5d. %s (%f)\n", i+sr.Request.From+1, hit.ID, hit.Score)
diff --git a/search/search.go b/search/search.go
index e66b0012..479a2697 100644
--- a/search/search.go
+++ b/search/search.go
@@ -147,7 +147,7 @@ type DocumentMatch struct {
 	Index           string                `json:"index,omitempty"`
 	ID              string                `json:"id"`
 	IndexInternalID index.IndexInternalID `json:"-"`
-	Score           float64               `json:"score"`
+	Score           float64               `json:"score,omitempty"`
 	Expl            *Explanation          `json:"explanation,omitempty"`
 	Locations       FieldTermLocationMap  `json:"locations,omitempty"`
 	Fragments       FieldFragmentMap      `json:"fragments,omitempty"`


Test patch:
diff --git a/index_alias_impl_test.go b/index_alias_impl_test.go
index fc5686fb..623dbc62 100644
--- a/index_alias_impl_test.go
+++ b/index_alias_impl_test.go
@@ -494,8 +494,7 @@ func TestIndexAliasMulti(t *testing.T) {
 			Successful: 2,
 			Errors:     make(map[string]error),
 		},
-		Request: sr,
-		Total:   2,
+		Total: 2,
 		Hits: search.DocumentMatchCollection{
 			{
 				ID:    "b",
@@ -575,8 +574,7 @@ func TestMultiSearchNoError(t *testing.T) {
 			Successful: 2,
 			Errors:     make(map[string]error),
 		},
-		Request: sr,
-		Total:   2,
+		Total: 2,
 		Hits: search.DocumentMatchCollection{
 			{
 				Index: "2",
@@ -949,8 +947,7 @@ func TestMultiSearchTimeoutPartial(t *testing.T) {
 				"ei3": context.DeadlineExceeded,
 			},
 		},
-		Request: sr,
-		Total:   2,
+		Total: 2,
 		Hits: search.DocumentMatchCollection{
 			{
 				Index: "2",
@@ -1105,8 +1102,7 @@ func TestIndexAliasMultipleLayer(t *testing.T) {
 				"ei3": context.DeadlineExceeded,
 			},
 		},
-		Request: sr,
-		Total:   2,
+		Total: 2,
 		Hits: search.DocumentMatchCollection{
 			{
 				Index: "4",
@@ -1184,6 +1180,7 @@ func TestMultiSearchCustomSort(t *testing.T) {
 	}}
 
 	sr := NewSearchRequest(NewTermQuery("test"))
+	sr.Explain = true
 	sr.SortBy([]string{"name"})
 	expected := &SearchResult{
 		Status: &SearchStatus{