-1

Is it possible to use variables with regex search when using the Dgraph go client?

We could built the query with SprintF but that would be insecure.

q :=query Search($searchterm: string) {
        subdomains(func: regexp(name, /^.*$searchterm/)) {
        uid:uid
        name:name
    }
}

The following query does not return any results, even tho they exist.

q := `query Search($searchterm: string) {
        subdomains(func: regexp(name, /$searchterm/)) {
            uid:uid
            name:name
        }
    }`

Or this

q := `query Search($searchterm: string) {
        subdomains(func: regexp(name, /.*$searchterm.*/)) {
            uid:uid
            name:name
        }
    }`

Then send the query to dgraph using: txn.QueryWithVars(ctx, q, variableMap)

Edit: This is a bug.

mbudge
  • 557
  • 13
  • 28

1 Answers1

0

Not the way you're doing it. But you can build a dynamic regex pattern by joining strings the normal way:

re, err := regexp.Compile("ˆ.*" + regexp.QuoteMeta(searchTerm))
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189