This AppEngine task seems like it should be trivial to do but I haven't quite worked it out. I have some data in a GrandParent/Parent/Child relationship thus:
EntityName Key
----------- -------
GrandParent W
GrandParent X
Parent W.A
Parent X.A
Parent X.B
Child W.A.i
Child X.A.i
Child X.A.ii <=== matches X.*.ii
Child X.A.iii
Child X.B.i
Child X.B.ii <=== matches X.*.ii
I'm trying to construct a query that matches all children with keys X.*.ii. In other words, it would return the keys X.A.ii and X.B.ii from the above, and nothing else. I'm using the low-level query mechanism, and what I have so far is this:
Entity gpX = new Entity("GrandParent","X");
Query q = new Query("Child");
q.setAncestor(gpX.getKey());
Which returns five children. If the i/ii/iii attribute was a property and not a key this would be trivial, but it is a key, and it is not clear to me how to specify that 'ii' is a search criterion.
There's no way to do this without a separate property. All filters in the App Engine datastore have to be either equality or range tests (which are equivalent to string prefix tests); it's not possible to filter in the way you're describing using a single filter.
Related
Let's say I have two types of vertices, parents and children. I want to be able to query for all parent vertices and have all the children grouped by their associated parent along with properties for each child. Assume parents have multiple children and children can have only 1 parent. Parent will always have the parent label, but the children can have various different labels.
So right now if I do
g.V().hasLabel('parent').group().by(__.inE().outV()).toList()
I get back:
[{v[Child_A]: [v[Parent_A]], v[Child_B]: [v[Parent_B]]}]
What I want is the opposite hierachy along with the value map/projected values of the child, so for example:
[Parent_A: Child_A1: {properties}, Child_A2: {properties}], [Parent_B: Child_B1: {properties]
Using python gremlin with Neptune if that matters.
If I understood your desired output, I think this what you looking for:
g.V().hasLabel('parent').group()
.by(__.values('name'))
.by(__.in_().group().by(__.values('name')).by(__.valueMap(true)))
I want to get the key to an entity (I don't need the actual entity. I need the key just to get a child entity).
So I know there are two ways of doing it:
// 1.
Key<Thing> tKey = com.googlecode.objectify.Key.create(Thing.class, id);
// 2.
Key<Thing> tKey = ofy().load().type(Thing.class).id(id);
What's the difference between them? what's faster? Which one should I use?
Would the answer change if I had to do this as well:
Thing t = tKey.get();
You want to use Key.create(Thing, id).
ofy().load().type(Thing.class).id(id) returns a Ref<Thing>, not a Key<Thing>. It actually loads the thing out of the datastore, which is not what you want.
I pass an NDB Key() with a parent to a deferred function. In this function I retrieve the entity again. But I cannot use the passed key to get the entity directly. I have to change the key order pairing in the ndb.Key().
deferred.defer(my_deferred.a_function, entity.key)
The entity.key() looks like :
Key('Parents', 'my_parent', 'Childs', 'my_child') # the first pair is the parent?
my_deferred.py :
def a_function(key) :
entity = ndb.Key(key) # the pass entity.key does not work !!!!!
Giving exception : ValueError: Key() must have an even number of positional arguments.
entity = ndb.Key('Childs', key.id(), parent = key.parent()).get() # this one works fine
I do not understand why the entity.key() method does not give me a key, which I can use directly? Or is there another way to get the entity, without "changing" the key. And I do not understand the ValueError excpetion.
Update : Thanks to Gregory
entity = key.get() # works fine
first, answering your code specific question, passing the key properly, it is not a callable:
deferred.defer(my_deferred.a_function, entity.key)
next, on the actual design of the code itself, there are some things that need tweaking.
the deferred api serializes your code, so there really is no need to re-query entity from the datastore. if you insist on this though, passing the entity.key to the deferred method, it's already an instance of ndb.Key, so there's no need to construct a new Key object.
I can't test this right now, but what about:
entity = ndb.Key(*key.flat())
The Key constructor accepts a few different kinds of input, and since flat() Returns a tuple of flattened kind and id values (kind1, id1, kind2, id2, ...)., unpacking the tuple should pass in the necessary inputs . Per the same link, this should also work:
entity = ndb.Key(pairs=key.pairs())
We can get all children of y (including indirect), by X.all().ancestor(y), but I want to receive only those which are direct children of y.
Is there any way to do it?
try:
X.all().filter("parent = ", y)
Using the Datastore Query() you can set its ancestor by using the method setAncestor() but it does not guarantee that the ancestor is the direct parent.
What you can do to ensure fetching only the direct children is by doing a comparison operation.
if( directChildEntity.getKey().getParent().equals( directParentEntity.getKey() ) )
{
// directChildEntity is a direct child of directParentEntity
}
The trick is to use the Datastore Key's getParent() method since it can mediate a one-step hierarchy between keys.
I'm trying to check whether a certain node exists beneath a branch of an ExtJS tree. Knowing the ID of the parent node, is there a library function to check whether a node exists beneath the parent (by its ID)?
I've checked the API numerous times over, and can only seem to accomplish this by iterating through the entire branch of the tree.
Is there a library function which allows me to check if a child exists (by its ID) if the parent node ID is known?
Thanks!
PS, to find the parent ID, I'm using the following:
tree.getNodeById('myID');
Ext.tree.TreeNode "contains" function does exactly what you want:
var parent = tree.getNodeById('myID');
parent.contains(tree.getNodeById('childId'));
Have you looked at DomQuery? The API defines the method jsSelect: selects a group of elements.
jsSelect( String selector, [Node/String root] ) : Array
Parameters:
selector : String
The selector/xpath query (can be a comma separated list of selectors)
root : Node/String
(optional) The start of the query (defaults to document).
Returns an Array of DOM elements which match the selector. If there are no matches, and empty Array is returned.