Skip to content

Commit 0103e41

Browse files
committed
style: format code annotations
Signed-off-by: adam <[email protected]>
1 parent d3d6925 commit 0103e41

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

pkg/infra/search/storage/meilisearch/convert.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
/**
2-
* @Author: Adam wu
3-
* @Description:
4-
* @File: convert.go
5-
* @Version: 1.0.0
6-
* @Date: 2025/3/21
7-
*/
1+
// Copyright The Karpor Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
814

915
package meilisearch
1016

@@ -46,26 +52,26 @@ func ConvertWithDefaultFilter(sql string, defaultFilter []string) (*meilisearch.
4652
return nil, "", fmt.Errorf("only single table queries are supported")
4753
}
4854

49-
// 应用默认过滤器
55+
// apply the default filter
5056
sel = applyDefaultFilter(sel, defaultFilter)
5157

52-
// 获取表名
58+
// obtain the table name
5359
tableName := strings.ReplaceAll(sqlparser.String(sel.From), "`", "")
5460

55-
// 构建搜索请求
61+
// build a search request
5662
req := &meilisearch.SearchRequest{
5763
Limit: 1000, //by default
5864
Offset: 0,
5965
}
6066

61-
// 处理WHERE条件
67+
// handle the where condition
6268
filter, err := buildFilter(sel.Where)
6369
if err != nil {
6470
return nil, "", err
6571
}
6672
req.Filter = filter
6773

68-
// 处理LIMIT/OFFSET
74+
// deal with LIMIT/OFFSET
6975
if sel.Limit != nil {
7076
if offset, ok := sel.Limit.Offset.(*sqlparser.SQLVal); ok {
7177
req.Offset = parseInt(string(offset.Val))
@@ -75,7 +81,7 @@ func ConvertWithDefaultFilter(sql string, defaultFilter []string) (*meilisearch.
7581
}
7682
}
7783

78-
// 处理ORDER BY
84+
// deal with ORDER BY
7985
if len(sel.OrderBy) > 0 {
8086
sort := make([]string, 0, len(sel.OrderBy))
8187
for _, order := range sel.OrderBy {

pkg/infra/search/storage/meilisearch/index.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ const (
2222
)
2323

2424
var defaultResourceIndexSetting = &meilisearch.Settings{
25-
// 对应 ES 的 max_result_window
2625
Pagination: &meilisearch.Pagination{
27-
MaxTotalHits: 1_000_000, // 控制最大返回结果数
26+
MaxTotalHits: 1_000_000,
2827
},
2928

30-
// 对应 ES analysis.normalizer
3129
SortableAttributes: []string{
3230
"creationTimestamp",
3331
"deletionTimestamp",
@@ -42,15 +40,14 @@ var defaultResourceIndexSetting = &meilisearch.Settings{
4240
},
4341
},
4442

45-
// 对应 ES mappings.properties 字段配置
4643
FilterableAttributes: []string{
4744
"id",
4845
"cluster",
4946
"apiVersion",
50-
"kind", // 启用过滤和精确匹配
47+
"kind",
5148
"namespace",
5249
"name",
53-
"labels", // 扁平化字段过滤
50+
"labels",
5451
"annotations",
5552
"ownerReferences",
5653
"content",
@@ -60,10 +57,9 @@ var defaultResourceIndexSetting = &meilisearch.Settings{
6057
},
6158

6259
SearchableAttributes: []string{
63-
"content", // 全文搜索字段
60+
"content",
6461
},
6562

66-
// 对应 _source.excludes 排除字段
6763
DisplayedAttributes: []string{
6864
"cluster",
6965
"apiVersion",
@@ -77,16 +73,13 @@ var defaultResourceIndexSetting = &meilisearch.Settings{
7773
"ownerReferences",
7874
"resourceVersion",
7975
"content",
80-
// 排除 "custom" 字段
8176
},
8277
}
8378
var defaultResourceGroupRuleIndexSetting = &meilisearch.Settings{
84-
// 对应 Elasticsearch 的 max_result_window
8579
Pagination: &meilisearch.Pagination{
86-
MaxTotalHits: 1000000, // 控制最大返回结果数
87-
}, // 控制最大返回结果数
80+
MaxTotalHits: 1000000,
81+
},
8882

89-
// 对应 mappings.properties 字段属性
9083
FilterableAttributes: []string{
9184
"id",
9285
"name",
@@ -100,8 +93,6 @@ var defaultResourceGroupRuleIndexSetting = &meilisearch.Settings{
10093
"deletedAt",
10194
},
10295

103-
// 对应 mappings._source.excludes
104-
// Meilisearch 无完全等效功能,但可通过以下方式近似实现
10596
DisplayedAttributes: []string{
10697
"id",
10798
"name",
@@ -110,6 +101,5 @@ var defaultResourceGroupRuleIndexSetting = &meilisearch.Settings{
110101
"createdAt",
111102
"updatedAt",
112103
"deletedAt",
113-
}, // 显式指定返回字段
114-
104+
},
115105
}

pkg/infra/search/storage/meilisearch/parse.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ func ParseQueries(queries []storage.Query) ([]interface{}, error) {
120120
return filter, nil
121121
}
122122

123-
// ConvertToFilter map 转换为 Meilisearch 等值过滤器
124-
// 输入示例:
123+
// ConvertToFilter convert the map to a meilisearch equivalent filter
124+
// Input:
125125
//
126126
// {
127127
// "category": "electronics",
128128
// "tags": ["a", "b"],
129129
// "author": {"name": "Alice"}
130130
// }
131131
//
132-
// 输出: ["category = 'electronics'", "tags IN ['a','b']", "author.name = 'Alice'"]
132+
// Output: ["category = 'electronics'", "tags IN ['a','b']", "author.name = 'Alice'"]
133133
func ConvertToFilter(query map[string]any) ([]any, error) {
134134
return processMap(query, "")
135135
}
136136

137-
// 递归处理嵌套 map
137+
// recursively handles nesting map
138138
func processMap(data map[string]any, parentKey string) ([]any, error) {
139139
var filters []any
140140

@@ -146,23 +146,20 @@ func processMap(data map[string]any, parentKey string) ([]any, error) {
146146

147147
switch v := value.(type) {
148148
case map[string]any:
149-
// 处理嵌套对象
150149
nestedFilters, err := processMap(v, fullKey)
151150
if err != nil {
152151
return nil, err
153152
}
154153
filters = append(filters, nestedFilters)
155154

156155
case []any:
157-
// 处理数组 (IN 查询)
158156
inClause, err := buildInClause(fullKey, v)
159157
if err != nil {
160158
return nil, err
161159
}
162160
filters = append(filters, inClause)
163161

164162
default:
165-
// 处理基本类型
166163
eqClause, err := buildEqualClause(fullKey, v)
167164
if err != nil {
168165
return nil, err
@@ -174,7 +171,7 @@ func processMap(data map[string]any, parentKey string) ([]any, error) {
174171
return filters, nil
175172
}
176173

177-
// 构建等值条件
174+
// build an equivalence condition
178175
func buildEqualClause(key string, value any) (string, error) {
179176
valueStr, err := convertValue(value)
180177
if err != nil {
@@ -183,7 +180,7 @@ func buildEqualClause(key string, value any) (string, error) {
183180
return fmt.Sprintf("%s = %s", key, valueStr), nil
184181
}
185182

186-
// 构建 IN 条件
183+
// build an in condition
187184
func buildInClause(key string, values []any) (string, error) {
188185
var elements []string
189186
for _, v := range values {
@@ -196,7 +193,7 @@ func buildInClause(key string, values []any) (string, error) {
196193
return fmt.Sprintf("%s IN [%s]", key, strings.Join(elements, ",")), nil
197194
}
198195

199-
// 值类型转换
196+
// value type conversion
200197
func convertValue(value any) (string, error) {
201198
switch v := value.(type) {
202199
case string:

0 commit comments

Comments
 (0)