label.id == "my-id1" && label.value == 100
( label.id == "id1" && label.value == 100 ) || ( label.id == "id2" && label.value == 900 )
--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/17ce4922-1086-4a68-9c34-a2ef9cd0626c%40googlegroups.com.
env, err := cel.NewEnv(
cel.Types(
types.NewObjectTypeValue("label"),
),
)
Hi Patryk,Taking a look at the Elastic Search Query DSL, it looks like the structure of the DSL is an abstract syntax tree, but that there is also a human readable query form which can be parsed to the JSON-like representation.The CEL parse step generates a cel.Ast result whose .Expr() function will return the underlying protobuf representation of the abstract-syntax tree. You can either walk the AST and build a Elastic Search query string format or a JSON-like DSL directly.It might be useful to look at the cel.AstToString implementation which lives in parser/unparser.go. This is an example of how the Ast is walked in order to produce a human-readable output. I could imagine the same or similar Ast walk being used to generate query strings or JSON-like query DSLs for Elastic Search.CEL generates type information during the env.Check call. It is possible to make limited inferences post-parse purely based on the function call operator name, though this doesn't ensure type-agreement of the variables.It sounds like you mostly need a way to translate CEL to Elastic Search, but if you need the type information I'd recommend checking the expression as well.-Tristan
On Tue, Nov 5, 2019 at 6:35 AM Patryk Małek <patry...@nauto.com> wrote:
I've created an issue on Github (https://github.com/google/cel-go/issues/279) but I believe this place is more appropriate for this type of questions.--I'm working on an internal Go package that would translate user (other services') expressions into Elasticsearch queries.My goal is to declare parsing logic which would let's say allow something like:label.id == "my-id1" && label.value == 100or( label.id == "id1" && label.value == 100 ) || ( label.id == "id2" && label.value == 900 )So the parsing logic has to be aware that it looks for (in the second example):* a label with id `id1` *and* `value` of `100`or* a label with id `id2` *and* `value` of `900`What I'm looking for is to generate a query that would do the filtering etc. on the DB side (Elasticsearch) instead of getting all the relevant results (for instance from a particular time range) and then evaluating the filter on those results.I believe that aligns with usage of cel in other projects like those mentioned on [1]> Google uses CEL as the expression component of IAM and Firebase security policies, as well as within Istio Mixer configs.>> Cloud IAM Conditions> Firebase Rules> Istio MixerCan I achieve this with cel? If so then can I do that?
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/1dd07df6-1b70-4360-ab45-f3517eb3af66%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/CAEddPXwogHwzq2MDxsZFtu%3D76cVOP_%3DHv52gvo24UVUJau%3DKRg%40mail.gmail.com.
package main
import ( "encoding/json" "flag" "log"
)
var queryFlag = flag.Int("query", -1, "Specify which query to parse and print")
func main() { flag.Parse()
var q elastic.Query switch *queryFlag { case 1: q = query1() case 2: q = query2() default: log.Fatalf("Unknown ID") }
src, err := q.Source() if err != nil { log.Fatalf("Source error %v", err) return } b, err := json.Marshal(src) if err != nil { log.Fatalf("Source marshal error %v", err) return }
log.Printf("Source %s", pretty.Pretty(b))
// ast, _ = env.Check(ast) // Here I'd hope to get an elastic.Query instead of a decls.Bool
// log.Printf("ResultType %v", ast.ResultType())}
func query1() elastic.Query { // ast, _ := env.Parse(`label.id == "my-id" && label.value == "my-value"`)
return elastic.NewBoolQuery().Filter( elastic.NewTermQuery("label.id", "my-id"), elastic.NewTermQuery("label.value", "my-value"), )}
func query2() elastic.Query { // ast, _ := env.Parse(
return elastic.NewBoolQuery().Should( elastic.NewBoolQuery().Filter( elastic.NewTermQuery("label.id", "my-id"), elastic.NewTermQuery("label.value", "my-value"), ), elastic.NewBoolQuery().Filter( elastic.NewTermQuery("label.id", "my-other-id"), ), )}
Hi Patryk,
My understanding of the Elastic Search API is limited, so we're trying to map the capabilities of two systems in real-time.Maybe you could give the example of the input CEL and the output Elastic Search and we can work from there?As general information for how to use CEL, the following would map the environment you'd want to configure for the original example:env, _ := cel.NewEnv(cel.Declarations(decls.Ident("label.id", decls.String, nil),decls.Ident("label.value", decls.Int, nil),),)parsed, iss := env.Parse(`label.id == 'my-id' && label.value == 100`)// check for parse issueschecked, iss := env.Check(parsed)// result type ...// checked.ResultType() == decls.Bool// reference / type map ..// ce := cel.AstToCheckedExpr(checked)// types of each expression in the AST.// tm := ce.GetTypeMap()-Tristan
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/1dd07df6-1b70-4360-ab45-f3517eb3af66%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/CAEddPXwogHwzq2MDxsZFtu%3D76cVOP_%3DHv52gvo24UVUJau%3DKRg%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/d14d1c13-f11b-4a24-a4d4-b43e61ef4d51%40googlegroups.com.
Unfortunately, we don't have the expertise in Elastic Search to spell out the exact algorithm
It would probably be too ambitious to translate the full CEL language, but you could handle this specialized subset that your patterns match against.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/CAEddPXxXVZ1aoPWmmc9p%3DTYnMNefabHaAUfH0e0H9C7QosqBmQ%40mail.gmail.com.