diff --git a/pkg/expr/mongo.go b/pkg/expr/mongo.go index 70bd6f05e6f3e5c422c88e193a7e1621eaa2f9e6..95f4b6beb7a99a279db932cb62b8fcf2186e6a23 100644 --- a/pkg/expr/mongo.go +++ b/pkg/expr/mongo.go @@ -62,7 +62,12 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter case bson.M: b = e 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: err = fmt.Errorf("invalid expression") } diff --git a/pkg/expr/mongo_test.go b/pkg/expr/mongo_test.go index 46f92b551086ae7ad1d5336f35c98d3154b4253d..9fe237e393f0280a7f2f12534c1b214bbbc21c43 100644 --- a/pkg/expr/mongo_test.go +++ b/pkg/expr/mongo_test.go @@ -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}, {"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}, + {"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 { t.Run(tt.name, func(t *testing.T) { @@ -72,7 +73,7 @@ func BenchmarkConvertToMongo(b *testing.B) { //fmt.Println(len(exp)) for i := 0; i < b.N; i++ { - ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{})) + _, _ = ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{})) } }