Matching Query Help Needed

Greetings Community,

I am wanting to solve a problem for our dgraph setup. I wanted to retrieve deals that are matching with my member node’s profile.

So, for this problem, jp (member) has 2 matches:

  1. Free Pizza (deal) through profiles education_5 and kid_0
  2. Free Toast (deal) through profile gender_1

My Schema:

<age_range>: int .
<category_id>: [string] @index(term) .
<category_name>: string .
<deal_id>: string @index(exact) .
<dgraph.cors>: [string] @index(exact) @upsert .
<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string .
<dgraph.graphql.p_sha256hash>: string @index(exact) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.schema_created_at>: datetime .
<dgraph.graphql.schema_history>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
<display_name>: string .
<education>: [int] @index(int) .
<educations>: int .
<email>: string .
<gender>: int .
<has>: [uid] @reverse .
<instance_deal_id>: string @index(exact) .
<interested_in>: [uid] @reverse .
<kid>: int .
<kids>: [int] @index(int) .
<marriage>: [int] @index(int) .
<name>: string @index(exact) .
<offers>: [uid] @reverse .
<password>: string .
<photo>: string .
<targets>: [uid] @reverse .
<template>: int .
<title_line_1>: string .
<title_line_2>: string .
<trip_id>: string .
<type>: string .
<user_id>: string @index(exact) .
<vendor_id>: string .
type <black> {
	name
}
type <category> {
	name
	category_id
}
type <deal> {
	name
	instance_deal_id
	targets
}
type <dgraph.graphql> {
	dgraph.graphql.schema
	dgraph.graphql.xid
}
type <dgraph.graphql.history> {
	dgraph.graphql.schema_history
	dgraph.graphql.schema_created_at
}
type <dgraph.graphql.persisted_query> {
	dgraph.graphql.p_query
	dgraph.graphql.p_sha256hash
}
type <gold> {
	name
}
type <member> {
	name
	user_id
	interested_in
	has
}
type <profile> {
	name
}
type <silver> {
	name
}
type <vendor> {
	name
	offers
	has
}
type <wish> {
	name
	category_id
	category_name
	user_id
}

My data:

{
  "data": {
    "me": [
      {
        "name": "jp",
        "has": [
          {
            "name": "gender_1"
          },
          {
            "name": "age_range_3"
          },
          {
            "name": "birth_year_0"
          },
          {
            "name": "education_5"
          },
          {
            "name": "marriage_8"
          },
          {
            "name": "kid_0"
          }
        ],
        "interested_in": [
          {
            "name": "category_4cfa4b7c-daca-4cc4-ad87-50d4de3c0a03"
          }
        ]
      },
      {
        "name": "category_4cfa4b7c-daca-4cc4-ad87-50d4de3c0a03"
      },
      {
        "name": "gender_1"
      },
      {
        "name": "age_range_3"
      },
      {
        "name": "birth_year_0"
      },
      {
        "name": "education_5"
      },
      {
        "name": "marriage_8"
      },
      {
        "name": "kid_0"
      },
      {
        "name": "vendor_2d183135-fd8f-4979-a610-a077a9f9b2d8",
        "offers": [
          {
            "name": "Free Toast"
          },
          {
            "name": "Free Pizza"
          }
        ],
        "has": [
          {
            "name": "category_4cfa4b7c-daca-4cc4-ad87-50d4de3c0a03"
          }
        ]
      },
      {
        "name": "Free Toast",
        "targets": [
          {
            "name": "gender_1"
          },
          {
            "name": "age_range_5"
          },
          {
            "name": "education_4"
          },
          {
            "name": "marriage_1"
          },
          {
            "name": "kid_2"
          }
        ]
      },
      {
        "name": "age_range_5"
      },
      {
        "name": "education_4"
      },
      {
        "name": "marriage_1"
      },
      {
        "name": "kid_2"
      },
      {
        "name": "Free Pizza",
        "targets": [
          {
            "name": "gender_1"
          },
          {
            "name": "education_5"
          },
          {
            "name": "kid_0"
          },
          {
            "name": "age_range_5"
          },
          {
            "name": "marriage_1"
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 195528,
      "processing_ns": 1420962,
      "encoding_ns": 89413,
      "assign_timestamp_ns": 871531,
      "total_ns": 2660994
    },
    "txn": {
      "start_ts": 13428
    },
    "metrics": {
      "num_uids": {
        "_total": 92,
        "has": 15,
        "interested_in": 15,
        "name": 32,
        "offers": 15,
        "targets": 15
      }
    }
  }
}

What I have done previously:

{
            var(func: type(preference)){
                ~has @filter(eq(uuid,` + value + `))
                {
                    has {
                        mem_preference_uids as uid
                    }
                }
            }
            var(func: type(deal)){
                prefers 
                @filter(uid(mem_preference_uids)) {
                    ~prefers {
                        name        
                        uid
                    }
                }
                  totalMatches as count(prefers)@filter(uid(mem_preference_uids)) 
            }
          var(func: uid(totalMatches),first:1,orderdesc: val(totalMatches))
             @filter(eq(deal_type,"black"))
            {
                    black as uid
                }
          var(func: uid(totalMatches),first:1,orderdesc: val(totalMatches))
             @filter(eq(deal_type,"silver"))
            {
              silver as uid
            }
          var(func: uid(totalMatches),first:1,orderdesc: val(totalMatches))
             @filter(eq(deal_type,"gold"))
            {
                gold as uid
                }
          q(func: uid(black,silver,gold), orderasc: deal_type_int) {
            uid
            name
            uuid
            deal_type
            deal_type_int
            totalMatches: val(totalMatches)
          }
          q2(func: type(member))
          @filter(eq(uuid,` + value + `))
            {
                name
                totalPreferences: count(has)
            }
        }

I edited your post for easier readability. Let me read through it and help you with your query

Some initial thoughts: make everything less dynamically or stringly typed:

type <member> {
     name
     user_id
     interested_in
     age_range
     birth_year
     education_level
     marriage_status
     children
     interested_in
}

EDIT: sent you a PM

Thank you chewxy for your response.

Thank you chew for the discussion today! I will keep you posted! Thank you and will adjust my graph accordingly

https://www.ogogo.com/

1 Like