How to filter by count

{
	var(func: type(Attribute)){
		last_analysis_results @filter(eq(result,"")) {
			u as count(uid)
			v as ~last_analysis_results 
     }
  }
  find(func: uid(v)) @filter(eq(val(u), 19)){
      uid 
      md5
  }
}

or,

{
		var(func: type(Attribute)){
			last_analysis_results @filter(eq(result,"")) {
					u as count(uid)
    }
  }
  find(func: eq(val(u), 19)){
	~last_analysis_results{
		uid 
        md5 
     }
  }
}

I want to get the MD5 of count(uid)=19。 I can’t query the data in my way, what should I do

1 Like

Hi @wpeng
If we have a type named User and each user is able to like a Content then I can easily filter users based on number of likes they have:

{
  getUsersWithMoreThan10Like(func: type(User)) @filter(ge(count(User.likes), 10))  {
    uid
    User.likes {
       uid
    }
  }
}
1 Like

Thank you for your reply, @pshaddel

I need to filter the Content first,
then count the number of Content,
and then find out the data where the number of Content is greater than 10.

1 Like

Did you check something like this:

{
  getUsersWithMoreThan10Like(func: type(User)) @filter(ge(count(User.likes), 10))  {
    uid
    User.likes @filter(has(Content.title)) {
       uid
       Content.title
    }
  }
}

Here I filtered contents and I got contents that have title field. I did not test this query but it makes sense.

1 Like

User.likes @filter(eq(Content.title, "xxxxx"))
The first filter will only get all User.likes data.
I need data whose title is a certain category and the number is greater than 10. Such as:
query:

{
  getUsersWithMoreThan10Like(func: type(User))   {
    uid
    User.likes @filter(eq(Content.title, "xxxxxx")) {
       count(uid)
    }
  }
}

result:

[
    {
        uid: "1",
        User.likes {
            count: 11
        }
    },{
        uid: "2",
        User.likes {
            count: 21
        }
    },{
        uid: "3",
        User.likes {
            count: 31
        }
    },{
        uid: "4",
        User.likes {
            count: 5
        }
    },
]

In the above query result, I want the data with count>10.

1 Like

Just tested this and it’s getting you somewhere you want by using cascade:

{
  getUsersWithMoreThanOneLike(func: type(User)) @filter(ge(count(User.likes), 2)) @cascade {
    uid
    User.name
    User.likes @filter(eq(Content.title, "xxxxx")) {
      uid
    }
  }
}

It uses cascade so if there is no uid inside User.likes result then it does not return that node as one of results.

Let me know if this solves your problem.

1 Like

Still not working.
In my example, I want to filter out the data with count=5, cascade doesn’t work here.

1 Like

Try this one:

{
  getUsersWithMoreThanOneLike(func: type(User)) @cascade {
    uid
    User.name
    User.likes @filter(eq(Content.title, "xxxxxx")) (offset: 10) {
      uid
    }
  }
}

It should only return results that has more than 10 content with title xxxx

1 Like

In the above example is valid。
Sorry,
count>10 is an example I gave, it’s not fixed. (greater than, less than, equal to) all possible.

1 Like

No problem. Check out this link:
In second part it uses value variable
Then edit the query

  var(func: allofterms(name@en, "eat drink man woman")) {
    starring {
      actors as performance.actor {
        totalRoles as count(actor.film)
      }
    }
  }

  edmw(func: uid(actors), orderdesc: val(totalRoles)) @filter(ge(val(totalRoles), 20)) {
    name@en
    name@zh
    totalRoles : val(totalRoles)
  }
}
1 Like

The top two queries refer to this link, and I have also tried to write aliases in various places, but have not gotten results.

my simple struct:

type Attr struct {
	md5 string
	last_analysis_results []struct {
		Result string
		Name   string
	}
}

I didn’t use filters, I didn’t get any data, as follows:

{
    top as var(func: type(Attribute)) {
		res as last_analysis_results {
			cnt as count(uid)
        }
    }
    find(func: uid(top),  first: 10) {
        total: val(cnt)
        last_analysis_results {
            tot: val(cnt)
        }
    }
  
    find2(func: uid(res), first:10) {
        total: val(cnt)
    }
}
1 Like

If you did not have that filter you could use this:

{
    top as var(func: type(Attribute)) {
		res as count(last_analysis_results)
    }
    find(func: uid(top)) {
        total: val(res)
    }
}

And easily you could filter on total.
But since you need to filter before calculating counts this method is not going to work.

I let you know if I found another way.

I found it, put the filter conditions into count.

{
  r as var(func: type(Attribute))  {
    v as count(last_analysis_results @filter(NOT eq(result, "")))
  }
  find(func: uid(r)) @filter(ge(val(v), 58)) {
	uid
  }
}
1 Like

Great. We also need to add this to the documentation.