Find nodes of 2 types joined by a third

I have a database of search results.
I am trying to query the database to see which searches hit on the same sentence.

this query produces the results just for a document

{
	r(func: eq(doc.id, "2349732")){
    doc.id
    ~document{
    	~result{
    querystring
  }
      result.index
      result.context{
      context.id
    }
    }
	}
}

I want to be able to find all the pink nodes(search results) that are connected to more than one geen node (result). Any ideas?

Hi @deanroker123
Do you have some sample data available. It will help to answer your question accurately.

Whats the best way to get you the data?

Do you want the schema too?

You could take a sample just copy paste it in the post itself (I dont think we need a big dump for this query). Schema will be great!

Schema

<company.id>: string .
<company_name>: string .
<context.id>: string @index(exact) .
<context>: default .
<dgraph.graphql.schema>: string .
<doc.id>: int @index(int) .
<document>: [uid] @reverse .
<name>: string @index(exact, term) .
<querystring>: string @index(hash) .
<result.context>: [uid] @reverse .
<result.documentid>: int @index(int) .
<result.id>: string @index(exact) .
<result.index>: string @index(exact) .
<result>: [uid] @reverse .
<result_contexts>: string .
<search>: [uid] @reverse .
type <Context> {
	context.id
}
type <Document> {
	doc.id
	name
	company.id
}
type <Result> {
	document
	result.documentid
	result.index
	result.context
}
type <Search> {
	querystring
	result
}
type <Theme> {
	theme.name
	search
}
type <dgraph.graphql> {
	dgraph.graphql.schema
}

data (i had to do 2 queried to get the matching nodes) is this ok?

{
  "data": {
    "r": [
      {
        "uid": "0x578930",
        "querystring": "(1efficiency~x)",
        "result": [
          {
            "result.context": [
              {
                "context.id": "2349732-355614"
              },
              {
                "context.id": "2349732-355570"
              },
              {
                "context.id": "2349732-355507"
              },
              {
                "context.id": "2349732-355566"
              },
              {
                "context.id": "2349732-355569"
              },
              {
                "context.id": "2349732-355758"
              }
            ]
          }
        ]
      }
    ],
    "q": [
      {
        "uid": "0x4ac954",
        "querystring": "(1costs~x)",
        "result": [
          {
            "result.context": [
              {
                "context.id": "2349732-355551"
              },
              {
                "context.id": "2349732-355742"
              },
              {
                "context.id": "2349732-355747"
              },
              {
                "context.id": "2349732-355750"
              },
              {
                "context.id": "2349732-355550"
              },
              {
                "context.id": "2349732-355735"
              },
              {
                "context.id": "2349732-355744"
              },
              {
                "context.id": "2349732-355745"
              },
              {
                "context.id": "2349732-355802"
              },
              {
                "context.id": "2349732-355552"
              },
              {
                "context.id": "2349732-355614"
              },
              {
                "context.id": "2349732-355529"
              },
              {
                "context.id": "2349732-355554"
              },
              {
                "context.id": "2349732-355570"
              },
              {
                "context.id": "2349732-355743"
              },
              {
                "context.id": "2349732-355771"
              }
            ]
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 136193,
      "processing_ns": 7499490,
      "encoding_ns": 52151,
      "assign_timestamp_ns": 549228,
      "total_ns": 8321465
    },
    "txn": {
      "start_ts": 3536122
    },
    "metrics": {
      "num_uids": {
        "context.id": 22,
        "querystring": 2,
        "result": 2,
        "result.context": 2,
        "result.documentid": 41634,
        "uid": 2
      }
    }
  }
}

here is the diagram, with the uid as the label for on the contexts

1 Like

Hi @deanroker123
I added the count index to the schema as below.

<result>: [uid] @count @reverse .

Here is the mutation I passed into Ratel. I have enforced the nodes with context 2349732-355570 to have the same uid, so that two pink nodes (result.context) point to the same green (result) node. Hope this part makes sense.

 {
"set": [
  {
        "uid": "_:u1",
        "querystring": "(1efficiency~x)",
        "result": [
          {
            "result.context": [
              {
                "context.id": "2349732-355614"
              },
              {
                "uid": "_:sameContext",
                "context.id": "2349732-355570"
              },
              {
                "context.id": "2349732-355507"
              },
              {
                "context.id": "2349732-355566"
              },
              {
                "context.id": "2349732-355569"
              },
              {
                "context.id": "2349732-355758"
              }
            ]
          }
          ]
          },
          
          {
        "uid": "_:u2",
        "querystring": "(1costs~x)",
        "result": [
          {
            "result.context": [
              {
                "context.id": "2349732-355551"
              },
              {
                "context.id": "2349732-355742"
              },
              {
                "context.id": "2349732-355747"
              },
              {
                "context.id": "2349732-355750"
              },
              {
                "context.id": "2349732-355550"
              },
              {
                "context.id": "2349732-355735"
              },
              {
                "context.id": "2349732-355744"
              },
              {
                "context.id": "2349732-355745"
              },
              {
                "context.id": "2349732-355802"
              },
              {
                "context.id": "2349732-355552"
              },
              {
                "context.id": "2349732-355614"
              },
              {
                "context.id": "2349732-355529"
              },
              {
                "context.id": "2349732-355554"
              },
              {
                 "uid": "_:sameContext",
                "context.id": "2349732-355570"
              },
              {
                "context.id": "2349732-355743"
              },
              {
                "context.id": "2349732-355771"
              }
            ]
          }
        ]
      }
        
]
}

Now to the query:

{
  q(func: has(context.id)) @normalize{
   context.id
   resultCount as count(~result.context)
  }
  qout(func: has(context.id)) @filter(gt(val(resultCount),1)){
    context.id
  }
}

The result is as below. qout should contain the desired context id.

{
  "data": {
    "q": [],
    "qout": [
      {
        "context.id": "2349732-355570"
      }
    ]
  }
}

Could you please try this and let us know?

I got it to work once, and it seems perfect, but now every time I run it i get this.

I expanded the Query a bit

{
  q(func: has(context.id)) @normalize{
   context.id
   resultCount as count(~result.context)
  }
  qout(func: has(context.id)) @filter(gt(val(resultCount),1)){
    context.id
    ~result.context{
    ~result{
      querystring
    }
  }
  }
}
{
  "name": "t",
  "url": "http://127.0.0.1:8083/query?timeout=120s&ro=true",
  "errors": [
    {
      "message": ": cannot find value without language tag from list with key 00000b7175657279737472696e67000000000000bc485a: cannot retrieve posting for UID 18446744073709551615 from list with key 00000b7175657279737472696e67000000000000bc485a: readTs: 3589457 less than minTs: 3589497 for key: \"\\x00\\x00\\vquerystring\\x00\\x00\\x00\\x00\\x00\\x00\\xbcHZ\"",
      "extensions": {
        "code": "ErrorInvalidRequest"
      }
    }
  ]
}

Any ideas?

I think your querystring field needs a language tag, something like querystring@en

Hi @deanroker123
Please let us know the version of Dgraph you are using. The readTs less than minTs is usually a temporary error, and should disappear with sufficient number of retries.
Refer: X less than minTs: y during query

I followed the docker instructions, Im running standalone 20.03.0

I will try and upgrade to 20.07.2

Thanks

Dean