forked from go-ego/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_response.go
executable file
·189 lines (153 loc) · 4.35 KB
/
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2013 Hui Chen
// Copyright 2016 ego 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 types
import (
"github.com/hhjpin/riot/utils"
)
// BaseResp search response options
type BaseResp struct {
// 搜索用到的关键词
Tokens []string
// 类别
// Class string
// 搜索到的文档,已排序
// Docs []ScoredDoc
// Docs interface{}
// 搜索是否超时。超时的情况下也可能会返回部分结果
Timeout bool
// 搜索到的文档个数。注意这是全部文档中满足条件的个数,可能比返回的文档数要大
NumDocs int
}
type AttrPair struct {
Key string
Values Attrs
}
type Attrs []*Attr
func (a Attrs) Len() int {
return len(a)
}
func (a Attrs) Less(i, j int) bool {
return a[i].RepeatTimes < a[j].RepeatTimes
}
func (a Attrs) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
type Attr struct {
Val interface{}
RepeatTimes uint64
}
type FacetResult map[string]*AttrPair
// SearchResp search response options
type SearchResp struct {
BaseResp
// 搜索到的文档,已排序
Docs interface{}
Facet FacetResult
}
// SearchDoc search response options
type SearchDoc struct {
BaseResp
// 搜索到的文档,已排序
Docs []ScoredDoc
Facet FacetResult
}
// SearchID search response options
type SearchID struct {
BaseResp
// 搜索到的文档,已排序
Docs []ScoredID
}
// Content search content
type Content struct {
// new Content
Content string
// new 属性 Attri
Attri map[string]Attribute
// new 返回评分字段
Fields interface{}
}
// ScoredDoc scored the document
type ScoredDoc struct {
ScoredID
// new 返回文档 Content
Content string
// new 返回文档属性 Attri
Attri map[string]Attribute
// new 返回评分字段
Fields interface{}
}
// ScoredDocs 为了方便排序
type ScoredDocs []ScoredDoc
func (docs ScoredDocs) Len() int {
return len(docs)
}
func (docs ScoredDocs) Swap(i, j int) {
docs[i], docs[j] = docs[j], docs[i]
}
func (docs ScoredDocs) Less(i, j int) bool {
// 为了从大到小排序,这实际上实现的是 More 的功能
min := utils.MinInt(len(docs[i].Scores), len(docs[j].Scores))
for iScore := 0; iScore < min; iScore++ {
if docs[i].Scores[iScore] > docs[j].Scores[iScore] {
return true
} else if docs[i].Scores[iScore] < docs[j].Scores[iScore] {
return false
}
}
return len(docs[i].Scores) > len(docs[j].Scores)
}
/*
______ .__ __. __ ____ ____ __ _______
/ __ \ | \ | | | | \ \ / / | | | \
| | | | | \| | | | \ \/ / | | | .--. |
| | | | | . ` | | | \_ _/ | | | | | |
| `--' | | |\ | | `----. | | | | | '--' |
\______/ |__| \__| |_______| |__| |__| |_______/
*/
// ScoredID scored doc only id
type ScoredID struct {
DocId string
// 文档的打分值
// 搜索结果按照 Scores 的值排序,先按照第一个数排,
// 如果相同则按照第二个数排序,依次类推。
Scores []float32
// 用于生成摘要的关键词在文本中的字节位置,
// 该切片长度和 SearchResp.Tokens 的长度一样
// 只有当 IndexType == LocsIndex 时不为空
TokenSnippetLocs []int
// 关键词出现的位置
// 只有当 IndexType == LocsIndex 时不为空
TokenLocs [][]int
}
// ScoredIDs 为了方便排序
type ScoredIDs []ScoredID
func (docs ScoredIDs) Len() int {
return len(docs)
}
func (docs ScoredIDs) Swap(i, j int) {
docs[i], docs[j] = docs[j], docs[i]
}
func (docs ScoredIDs) Less(i, j int) bool {
// 为了从大到小排序,这实际上实现的是 More 的功能
min := utils.MinInt(len(docs[i].Scores), len(docs[j].Scores))
for iScore := 0; iScore < min; iScore++ {
if docs[i].Scores[iScore] > docs[j].Scores[iScore] {
return true
} else if docs[i].Scores[iScore] < docs[j].Scores[iScore] {
return false
}
}
return len(docs[i].Scores) > len(docs[j].Scores)
}