Get output of uid(a,b,...) as intersection not union

Hello,

My query is to find

get users uid having skill java

get users uid status is freshers

get users uid living in Delhi

Now get intersection of all these uids output.

How can I do this, any idea
I am currently using uid(a,b,…) but its giving me union output, but I need intersection.

Thanks

You can use filters, they perform AND operation with the function at root by default.

{
  me(func: eq(skill, "java")) @filter(eq(status, "freshers") AND eq(living, "Delhi")) {
    name
    ...
  }
}

I already have applied it.

But according to my requirements I need users having either all these condition or may be two or one.

But this query will give me exact match.

You already have uids from conditions A , B and C, its just a matter of using filters to get what you want.

I didn’t get your point.
As I have already explained my requirements, if I use filter it will give me only exact match. And if I use different method having filter like for eg:-

get A
get B
get C
get A&B
get B&C
get C&A
get A&B&C

then uid(uid of all)

But I think its quite lengthy.

Isn’t what you are describing just A U B U C?

No,

I want A intersection B intersection C

Sorry I get confused between 2 queries.

Maybe this Venn diagram for A U B U C would help.

Sorry for confusion,

This Requirements is for another query.

I want A intersection B intersection C.
But I cann’t use filter because either user can provide A, B, C, AB, BC, CA or ABC.
So for filter one variable should be permanent which will used in function then we can apply others in filter.

So I decided different approach by getting
A, B, C, AB, BC, CA, ABC as per user, then find intersection of all these.

And if I make a variable for eg. A compulsory, then A is a array type, and if I matched it, then user is repeating.

Please clarify your exact requirement and the data given with some examples and then I can suggest something.

@pawan, Example: Get allCars and get all carsWithOil, then subtract carsWithOil from allCars without using filters. Because car may not have relates_to predicate always. Therefore, we need to make two query blocks. Then subtract carsWithOil from another allCars. Then print all cars that has not relates_to-> oil.

  carsWithOil as var(func: has(car) )  @cascade 
  {
    name
    relates_to @filter(has(oil)) {
         name
    }
  }

 allCars as var(func: has(car) ) {
    name
  }

@pawan, another example will be, get trucks and cars, then merge uids of both queries, then print all.

One of solution will be to use OR.

 query(func: has(car) OR has(truck) )  
   {
        uid
   }

It works fine for basic cases, What if we need to filter cars and trucks with different criteria? ex: get trucks that name starts with some_word and get cars that has driver predicate true.

All of these queries are possible with multiple query blocks.

Get them separately in two query blocks and then get their union in a third block.

@pawan,
I looked dgraph documentation, specially query language. Nothing mentioned about unions, intersections?
How to get union of two uid list?
How to subtract uid list from another uid list?
Simple examples will be helpful

Union is OR

{
  cars as var(func: has(is_car)) {
  }

  trucks as var(func:has(is_truck)) {
  }

  trucksAndCars(func: uid(is_car, is_truck)) {
  }
}


Intersection would be AND (func at root and filter are in AND by default)

{
  carsAndTrucks(func: has(is_car)) @filter(has(is_truck)) {
  }
}

Subtraction

{
  carNotTruck(func: has(is_car)) @filter(NOT has(is_truck)) {
  }
}
5 Likes

Sorry for late reply.

My requirement is to get users having
Skills:- Java, Angularjs --required
Status:- Experienced --not compulsory
Experience: 0-2 yrs --not compulsory

So I have write my query something like this

{
 searchUser(func: eq(current_status, "Experienced")) @filter(anyofterms(preferred_location1, "Delhi, Gurgaon") OR anyofterms(preferred_location2, "Delhi, Gurgaon")){
          skills: skills @filter(anyofterms(skill_name, "Java, Angularjs")){
            skill_name
          }
          id: uid 
        }
      }

But the issue is current_status, preferred_location1 and preferred_location2 is not compulsory and skill is must required, So I want to do something like this

{
searchSkill(func: anyofterms(skill_name, "Java, Angularjs")){
uid
}

method to get user uid having current_status
method to get user uid for preferred_location1 and preferred_location2

then method to get intersection of uid(of above output) 

So, I f there is not intersection, then can provide some other way to complete my task.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.