I am using RethinkDB, and my rows have nested objects. If a field is a value, I want it overwritten, however, if it is a an array, I want to the old and new values to be concatenated. Thus, for a nested, object, I want the same behavior.
I was hoping this line of code would do the trick
r.branch(row(field).typeOf().eq('ARRAY'), row(field).add(data[field]),
r.branch(row(field).typeOf().eq('OBJECT'), deepcopy(data[field], row(field)),data[field]));
However, in the nested branch, if an object is a string, it comes out as true. What is the appropriate check?
Related
Is there a way in ElasticSearch wherein I can remove some the objects in the nested field array.
So I have a nested field and it returns array of objects. I need to remove some objects in the nested field.
Is it possible to do so in the query or I need to do that in my code
These extra nested documents are hidden; we can’t access them directly. To update, add, or remove a nested object, we have to reindex the whole document. It’s important to note that, the result returned by a search request is not the nested object alone; it is the whole document.
Nested Objects Elastic search
As far as I know, In Elasticsearch you can't just remove part of a existing document. You should change the document (remove objects you don't need) and renew(rewrite) the document.
I have a mapping similar to this: JsonExample
This represents a changelog structure. So each document represents an object with certain properties. These properties can be changed at certain dates. To limit the space, it keeps track of the changes instead of updating the whole object with only one updated field for example.
The goal is to query a certain object based on a given date. The result should be the object with the properties on that given date. So later changes should be discarded and only the changes matching or most recent to the given date should be returned.
So with a nested query I can retrieve the whole object. Now I only want those nested properties to be returned and also sorted closest to the given date so I can easily find the properties at that given date.
Is there any way to do this with only ElasticSearch queries/filters and without parsing the returned json and sort afterwards with for example Java.
Im trying filter records with that has a specific value key and delete them. I tried "withFields" and "hasFields" but seems that i can't apply delete to them. My question is how can i do that?
r.db('databaseFoo').table('checkpoints').filter(function (user) {
return user('type').default(false);
}).delete();
If you want all documents that have a type key, you can use hasFields for that.
r.db('databaseFoo').table('checkpoints')
.hasFields('type')
In your current query, what you are doing is getting all documents that don't have a type key or where the value for type is equal to false. This might be what you want, but it's a little confusing if you only want documents that have a type property.
Keeping a reference to the original document
The problem with using hasFields is that it converts a selection (a sequence with a reference to the specific rows in the database) that you can update, and delete into a sequence, with which you can't do that. This is a known issue in RethinkDB. You can read this blog post to understand the different types in ReQL a bit better.
In order to get around this, you can use the hasFields method with the filter method.
r.db('databaseFoo').table('checkpoints')
.filter(r.row.hasFields('type'))
.delete()
This query will work since it returns a selection which can then be passed into delete.
If you want to get all records with with a specific value at a specific key, you can do so a couple of different ways. To get all documents where the property type is equal to false, you can do as follows:
r.db('databaseFoo').table('checkpoints')
.filter({ type: false })
or, you can do:
r.db('databaseFoo').table('checkpoints')
.filter(r.row('type').eq(false))
In Elasticsearch, is it possible to reference a top-level (non-nested) property in a nested filter?
I have a situation where I need a condition to be true either at a global level or in one of any number of associated nested objects. Inside of the nested filter I have an or-filter to check one or the other, but the outer property appears to be ignored. An example is here.
I have a feeling that what I'm needing is not supported and everything inside of the nested-filter must apply at or below the specified path (from the docs, "The query is executed against the nested objects / docs as if they were indexed as separate docs (they are, internally)". I'm about to just duplicate the top-level data in each nested object (it really is just a boolean field), but I'd like to know if this is possible or if there's another obvious solution I'm missing.
You are correct in that the feature you are looking for is not supported. Elasticsearch uses the various Lucene join queries such as ToParentBlockJoinQuery underneath and it does not reference both layers of properties.
You can use the include_in_parent/include_in_root properties to push the property to a higher level, but you lose the ability to filter on multiple properties belong to the same nested document.
When I call mydictionary.Add(key, object), can I guarantee that the object is added to the end of the dictionary in the sense that mydictionary.ElementAt(mydictionary.Count - 1) returns that object? I'm asking because I'm used to Java where HashMap doesn't have any order at all.
I'm hoping to use the ordering given by ElementAt as a way of knowing the order in which objects were added to the dictionary without using a separate data structure.
Update: Looks like ElementAt isn't going to be of any use. Is the best way to do this to use a separate data structure to store the ordering that I need?
Thanks
There is no order to a dictionary. The ElementAt method is a linq extension method that iterates over the dictionary using IEnumerable and counts the number of things, there is no relation to the order things were added.
There is a SortedDictionary, which will sort things by key, but will not keep them in the order they were added in.
If the order is really important you could always have two data structures, a list that you add the object to and a dictionary that stores the key to list index mapping. Or put a field inside your object that set from a counter as you add it to the dictionary.