-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_search_response.go
139 lines (123 loc) · 3.74 KB
/
http_search_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2020 The Bluge Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"math"
"github.com/blugelabs/bluge/search"
)
type DocumentMatch struct {
Document interface{} `json:"document"`
Score float64 `json:"score"`
Expl *search.Explanation `json:"explanation"`
ID string `json:"id"`
}
type AggregationValue struct {
DisplayName string `json:"display_name"`
FilterName string `json:"filter_name"`
Count uint64 `json:"count"`
Filtered bool `json:"filtered"`
}
type Aggregation struct {
DisplayName string `json:"display_name"`
FilterName string `json:"filter_name"`
Values []*AggregationValue `json:"values"`
}
type SearchResponse struct {
Query string `json:"query"`
Total uint64 `json:"total"`
TopScore float64 `json:"top_score"`
Hits []*DocumentMatch `json:"hits"`
Duration string `json:"duration"`
Aggregations map[string]*Aggregation `json:"aggregations"`
Message string `json:"message"`
PreviousPage int `json:"previousPage,omitempty"`
NextPage int `json:"nextPage,omitempty"`
}
func NewSearchResponse(query string) *SearchResponse {
return &SearchResponse{
Query: query,
}
}
func (s *SearchResponse) buildAggregation(aggs *search.Bucket, name string, filters []*Filter) {
agg := &Aggregation{
DisplayName: displayName(name),
FilterName: name,
}
for _, bucket := range aggs.Buckets(name) {
aggVal := &AggregationValue{
DisplayName: displayName(bucket.Name()),
FilterName: bucket.Name(),
Count: bucket.Count(),
}
for _, f := range filters {
if f.Name == name && f.Value == bucket.Name() {
aggVal.Filtered = true
}
}
agg.Values = append(agg.Values, aggVal)
}
s.Aggregations[name] = agg
}
func (s *SearchResponse) AddAggregations(aggs *search.Bucket, filters []*Filter) {
s.Total = aggs.Count()
s.TopScore = aggs.Metric("max_score")
s.Duration = aggs.Duration().String()
s.Aggregations = make(map[string]*Aggregation)
s.buildAggregation(aggs, typeAggregation, filters)
s.buildAggregation(aggs, styleAggregation, filters)
s.buildAggregation(aggs, updatedAggregation, filters)
s.buildAggregation(aggs, abvAggregation, filters)
}
func (s *SearchResponse) AddPaging(aggs *search.Bucket, page int) {
numPages := int(math.Ceil(float64(aggs.Count()) / float64(resultsPerPage)))
if numPages > page {
s.NextPage = page + 1
}
if page != 1 {
s.PreviousPage = page - 1
}
if page != 1 {
s.Message = fmt.Sprintf("Page %d of ", page)
}
s.Message += fmt.Sprintf("%d results (%s)", aggs.Count(),
aggs.Duration().Round(roundDurationTo))
}
func displayName(in string) string {
switch in {
case typeAggregation:
return "Type"
case "beer":
return "Beer"
case "brewery":
return "Brewery"
case updatedAggregation:
return "Updated"
case "old":
return "Before 2012"
case "new":
return "Since 2012"
case styleAggregation:
return "Style"
case abvAggregation:
return "ABV"
case "low":
return "Low (< 3%)"
case "med":
return "Medium (3% - 5%)"
case "high":
return "High (> 5%)"
}
return in
}