Skip to content

Instantly share code, notes, and snippets.

@voutilad
Created January 26, 2021 00:16
Show Gist options
  • Save voutilad/553c20a4aa7447baeff843bffd69e38f to your computer and use it in GitHub Desktop.
Save voutilad/553c20a4aa7447baeff843bffd69e38f to your computer and use it in GitHub Desktop.
Debugging a Neo4j query issue
package main
import (
"fmt"
"os"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)
// Run the given cypher and print the results
func run(driver *neo4j.Driver, cypher string) error {
session := (*driver).NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead})
defer session.Close()
fmt.Printf("Running Cypher: %s\n", cypher)
result, err := session.Run(cypher, nil)
keys, err := result.Keys()
if err != nil {
return err
}
row := 0
var record *neo4j.Record
for result.NextRecord(&record) {
row = row + 1
fmt.Printf("[ Row %d ]\n", row)
for i, value := range record.Values {
fmt.Printf(" %s: %v\n", keys[i], value)
}
}
return err
}
func main() {
useConsoleLogger := func(level neo4j.LogLevel) func(config *neo4j.Config) {
return func(config *neo4j.Config) {
config.Log = neo4j.ConsoleLogger(level)
}
}
url, found := os.LookupEnv("NEO4J_URL")
if !found {
panic("missing NEO4J_URL environment variable")
}
user, found := os.LookupEnv("NEO4J_USER")
if !found {
panic("missing NEO4J_USER environment variable")
}
password, found := os.LookupEnv("NEO4J_PASSWORD")
if !found {
panic("missing NEO4J_PASSWORD environment variable")
}
driver, err := neo4j.NewDriver(
url,
neo4j.BasicAuth(user, password, ""),
useConsoleLogger(neo4j.DEBUG))
if err != nil {
panic(err)
}
defer driver.Close()
query := `MATCH (loc:Vendor {id: "57d6654e-dbea-49f0-9a4e-a194dec99fee" })
MATCH (loc)<-[:HAS_VENDOR]-(c:Company)
MATCH (ven:Vendor)-[:FOR_STATE]->(s:State {abbreviation: "CO"})
WHERE ven.type <> "retail"
AND ((ven)<-[:HAS_EXCLUSIVE]-(c) OR NOT (ven)<-[:HAS_EXCLUSIVE]-(:Company))
AND (loc.medical = true OR loc.medical = ven.medical)
OPTIONAL MATCH (ven)-[:HAS_LICENSE]->(lic:License)
OPTIONAL MATCH (ven)<-[:HAS_PRODUCER]-(prod:Product)
OPTIONAL MATCH (prod)-[:HAS_SUBCATEGORY]->(subcat:Subcategory)-[:FOR_CATEGORY]->(cat:Category)
OPTIONAL MATCH (prod)-[:HAS_VARIANT]->(v:Variant)
OPTIONAL MATCH (v)-[:HAS_PRICE]->(msrp:MSRP)
OPTIONAL MATCH (loc)-[:HAS_PREFERRED]->(:Preferred)-[:FOR_VARIANT]->(v)<-[:HAS_VARIANT]-(pre:Product)
RETURN
ven.dba AS brand,
ven.id AS brand_id,
lic.id AS brand_license,
ven.medical AS medical,
ven.type AS type,
collect(distinct prod.name) AS products,
collect(distinct prod.product_type) AS product_types,
collect(distinct cat.name) AS categories,
collect(distinct subcat.name) AS subcategories,
collect(distinct v.cpc) AS cpcs,
collect(distinct v.name) AS variants,
any(f IN collect(pre) WHERE f IS NOT NULL) AS preferred,
any(f IN collect(prod.product_type) WHERE f <> "cannabis" ) AS has_non_cannabis,
s.abbreviation AS brand_state`
err = run(&driver, query)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment