Skip to content
Snippets Groups Projects
Commit d86183b9 authored by Anton Sattarov's avatar Anton Sattarov
Browse files

Добавлен поиск по идентификатору записи для случая когда не указано выражение для фильтрации

parent 30b5e866
No related branches found
No related tags found
No related merge requests found
...@@ -62,7 +62,12 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter ...@@ -62,7 +62,12 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter
case bson.M: case bson.M:
b = e b = e
case string: case string:
b = bson.M{"$text": bson.M{"$search": e}} b = bson.M{
"$or": bson.A{
bson.M{"_id": e},
bson.M{"$text": bson.M{"$search": e}},
},
}
default: default:
err = fmt.Errorf("invalid expression") err = fmt.Errorf("invalid expression")
} }
......
...@@ -50,6 +50,7 @@ func TestConvertToMongo(t *testing.T) { ...@@ -50,6 +50,7 @@ func TestConvertToMongo(t *testing.T) {
{"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), nil, bson.M{"d": bson.M{"$gt": tm}}, false}, {"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), nil, bson.M{"d": bson.M{"$gt": tm}}, false},
{"in", "In(s, [1,2,3])", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false}, {"in", "In(s, [1,2,3])", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false},
{"in", "In(s, 1)", nil, bson.M{"s": bson.M{"$in": []interface{}{1}}}, false}, {"in", "In(s, 1)", nil, bson.M{"s": bson.M{"$in": []interface{}{1}}}, false},
{"text search or id", "id", nil, bson.M{"$or": bson.A{bson.M{"_id": "id"}, bson.M{"$text": bson.M{"$search": "id"}}}}, false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
...@@ -72,7 +73,7 @@ func BenchmarkConvertToMongo(b *testing.B) { ...@@ -72,7 +73,7 @@ func BenchmarkConvertToMongo(b *testing.B) {
//fmt.Println(len(exp)) //fmt.Println(len(exp))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{})) _, _ = ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{}))
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment