Filter a neo4j map projection - database

I'm attempting to build up a map projection that has a dynamic flag in it, so I initially set the flag to false for all nodes, then union the results with specific users and set their flag to true as below
MATCH (u:User)
WITH u, false as has_policyflag
RETURN u{.UPN, has_policyflag:has_policyflag}
UNION
MATCH (u:User)-[*1..2]-(:Policy{id:"4d3e7650-1ee0-4254-9ec1-30f2a271a7a5"})
WITH u, true AS has_policyflag
RETURN u{.UPN, has_policyflag:has_policyflag}
UNION
MATCH (u:User)-[*1..2]-(:Policy{id: "806134f2-7738-4da2-a725-3837f8c5d769"})
WITH u, true AS has_policyflag
RETURN u{.UPN, has_policyflag:has_policyflag}
What this gets me is a map projection of ALL the user UPNs and their policy flag, but what I'd really like to be able to do is filter this down so I only return the ones which are true. Is this possible or am I taking the wrong approach?

Related

Neo4j/Cypher Match only if predicate applies for all relationships

I need to match nodes only when every relationship the node has fullfills a whereclause:
MATCH (o:Otherthing)
WHERE id(o) = 1
MATCH (unknown:Thing)
WHERE (unknown)-[:DEPENDS_ON]->(:Thing)<-[:DEPENDS_ON*]-(:Thing)<-[:STARTED_WITH]-(o)
RETURN unknown
Every matched "Thing" should only have relationships labeled with "DEPENDS_ON" and all of them should fullfill the condition.
How can I achieve that?
This may work for you:
MATCH (u:Thing)-[:DEPENDS_ON]->(:Thing)<-[:DEPENDS_ON*]-(:Thing)<-[:STARTED_WITH]-(o)
WHERE ID(o) = 1
WITH u, COUNT(*) AS num
WHERE SIZE((u)--()) = num
RETURN u
The query uses an efficient degreeness check to get the total number of relationships for u, and compares that with the number of times u satisfied the MATCH. Also, since you are identifying o by its native ID (which I am assuming is always going to be the ID for an Otherthing), it is more efficient to not specify its label (to avoid a label verification operation).

How do I specify which collections are being queried in AQL?

lets imagine I have this graph
and I want to query for all the partners connected to an user
WITH partner
FOR u IN user
FILTER u._name == #user_name
FOR v IN OUTBOUND user GRAPH 'accounts'
RETURN v
this query works when #user_name == Client because it's only connected to partners but when #user_name == Admin the profiles are returned also, when I expected an empty list (because user Admin has no partners)
Am I using the keyword WITH in the correct manner?
The purpose of the WITH keyword is to specify the collections involved in a traversal, so that they can be read-locked at query start instead of lazily during traversal, which can lead to dead lock situations. It it required for traversals in a cluster.
It does not affect the query result. If you want to return paths that end at nodes from a certain collection only, use a filter with IS_SAME_COLLECTION():
WITH user, partner, profile
FOR u IN user
FILTER u._name == #user_name
FOR v IN OUTBOUND user GRAPH 'accounts'
FILTER IS_SAME_COLLECTION('partner', v)
RETURN v

Google Cloud Datastore get entities that have a boolean set to false or null?

I have a bunch of entities with a boolean field that can either be: true, false, or null (the actual null value). I need to retrieve all the entities that have this field set to null or false. I know that that GCD doesn't support a not equal query but I think I got it to work by querying for all the entities that have the field less than true.
It seems to work but I am not sure why it works. Are null and false always less than true?
Any of these work:
test_query = Test.query(Test.bool != True).fetch()
test_query = Test.query(Test.bool.IN([False, None])).fetch()
test_query = Test.query(Test.bool < True).fetch()
In Python:
>>> None < False < True
True
Datastore not supports OR and != (not equal) operators natively.
Python implementations, of != is fully programmatic and have number of limitation when used with another query operations.
Search with IN operator when [False, None] provided will cause multiple query to be launched and merged in a single result with this call.
The '< True' operation will not find properties without any value (empty val) as it is not indexed, but will find false and null values and this can be done in a single query.
So, the best option will be (on JavaScript):
let query = db.createQuery("SomeNamespace", "SomeKind")
.filter("property", "<", true);

Calculated measure to detect if multiple dimension members are selected in filter

I would like to detect if multiple members are selected in a dimension filter and if yes then nullify my measure in the olap cube.
Something like this
[measures].[total users] = IIF([measures].[is multi select], null, [measures].[total users]);
The reason being the total users measures gives a wrong value if multiple members are selected by user.
I tried this measure
[measures].[is multi select] = ISERROR([DIM 1].[DIM1 KEY].currentmember) OR
ISERROR([DIM 2].[DIM2 KEY].currentmember).
But this doesn't seem to work as expected. I thought this would give me true if multiple members of dim1 and or dim2 are selected in a filter it will return true..but it always returns false.
Any other trick?

SQL bit field filtering

just a quick question here.
Lets say I have a stored procedure for seleting records from a table.
select * from tbl_table
where deleted = #deleted
So for this above, I must pass in a parameter being either TRUE or FALSE.
Is it possible to return results where deleted is equal to TRUE and FALSE but retaining the option to pass in a parameter. So I guess, if no parameter is passed in, there is no filter.
The other way I thought was to do this..
select * from tbl_table
where deleted = #deleted1
and deleted = #deleted2
So you have 2 parameters for the same filter. This way you can do true or false or set both filters the same - giving more leeway.
If anyone has any ideas or thoughts on this that would be great!
set your parameter as nullable (= NULL) and then
select * from tbl_table
where (#deleted IS NULL) OR (deleted = #deleted)
Then do not supply the parameter (or explicitly supply as NULL), when you do not want the filter by that parameter to be applied.
(But be aware this can sometimes have parameter sniffing consequences, especially with a large number of parameters)

Resources