SELECT single field from embedded array in azure documentDB - azure

I have a documentDB collection that looks like this sample:
{
"data1": "hello",
"data2": [
{
"key": "key1",
"value": "value1"
},
{
"key": "key2",
"value": "value2"
}
}
In reality the data has a lot of other fields and the embedded array has some fields where the data is quite large. I need to query the data and I care about the small "key" field in the data2 array but I do not need the large "value". I am finding returning all the value data is causing performance problems, but if I exclude the array data from the SELECT all together it is fast (so the data size is the issue).
I cannot figure out a way to return only the "key" but exclude the "value" in the embedded array.
I basically want SELECT r.data1, r.data2.key and to have it return as:
{
"data1": "hello",
"data2": [
{
"key": "key1"
},
{
"key": "key2"
}
}
but it doesn't seem possible to SELECT r.data2.key because it is in an array
A JOIN will cause it to return a copy of each document for each "data2" array element, which does not work for me. My only other option would be to migrate the data and put the data I want into its own array so I can select the whole object.
Is this possible some how that I have not been able to figure out?

Mike,
As you have surmised, this is not possible without a custom UDF until DocumentDB supports sub-queries. If you would like to go down that route, see the following answer for an example of how the UDF may have to look:
DocumentDB Sub Query
Good luck!

Related

Mongo DB: Append a value to a nested array

I think there might be better ways to store data but the app I am working on has some data stored like this.
I have a document with nested arrays. In this particular example, would it be possible to append a new element "value3" to an array inside products whose "productId" matches the condition. So, basically look for productId and append "value3" to the array which has matching "productId".
I am just learning about MongoDB so if you can provide some suggestions for changing schema, even that would be very helpful.
P.S. Not sure if it helps but I am using node.js
{"_id":
{"$oid":"601baf0e5c307422c0fa958c"},
"sale":"Test Sale",
"products":[
[
["productId","value1","value2"],
["productId","value1","value2"]
],
[
["productId","value1","value2"],
["productId","value1","value2"]
],
[
["productId","value1","value2"],
["productId","value1","value2"]
]
],
"collectionProducts":["id","id","id"]
}
Maybe something like this:
db.store.update({},{$push:{"products.$[].$[k]":"value3"}},{ arrayFilters: [{ k:'productId' }]})

Create and query mango index on unknown object key in CouchDB

I have the following object:
{
"roleAttribution": {
"15497490976600-51042": {
"teams": [
"e5abb1e962e11a84ff0e41e99103cd90"
],
"persons": [
"15124323582330-17269"
]
}
},
"type": "link",
}
And need to index/query the teams array. The problem is the roleAttribution keys are unpredictable.
Is there a way to index and query all possible keys of the object up to the teams array?
At this point, CouchDB does not support a good way to just index arrays. (https://issues.apache.org/jira/browse/COUCHDB-2867). You would need to create a view for that. If you would like to query documents based on the values of team array, you would need to iterate over the array in view map function and emit all the values there. More info about the views here http://guide.couchdb.org/draft/views.html

Cosmos DB Date Index Not Efficient

I have a collection with a Date field that is populated by a C# application using a DateTime object. This field is serialized to the following format "2018-06-10T17:32:48.3285735Z".
I haven't touched the Index Policy in the collection, so strings are using the Range index type. From what I've read in the documentation, that's the most efficient way to index dates, however, when I use the Date field in an ORDER BY clause, the query consumes at least 10x more RUs than if I were to query using the timestamp (_ts) number field. That means paying 10x more for this single collection.
To illustrate the issue:
SELECT TOP 100 * FROM c ORDER BY c.Date DESC
//query consumes a minimum of 500 RUs
SELECT TOP 100 * FROM c ORDER BY c._ts DESC
//query consumes 50 RUs
Is this how it is supposed to work or am I missing something? I suspect that if this was the expected behavior, it would be emphasized in the index documentation, and storing dates as numbers would be highlighted as the best practice.
EDIT:
This is the index policy for the collection (I never changed it).
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
},
{
"kind": "Spatial",
"dataType": "Point"
}
]
}
],
"excludedPaths": []
}
This may have to do with index collisions (multiple values map to the same index term).
You may want to narrow the range of the filed Date and see if that helps. Basically, try this query:
SELECT TOP 100 * FROM c WHERE (c.Date BETWEEN '2000-01-01' AND '2100-01-01') ORDER BY c.Date DESC
Please note that the added filter should not charge the query result set.
Did you try specifically configuring for Range Queries?
I think by default strings are hashed and you have to specify indexing for range queries.
I found this in the documentation:
By default, Azure Cosmos DB indexes all string properties within
documents consistently with a Hash index.
Documentation link
For setting up a range query index on the collection:
DocumentCollection collection = new DocumentCollection { Id = "orders" };
collection.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String)
{ Precision = -1 });
await client.CreateDocumentCollectionAsync("/dbs/orderdb", collection);
The document they are querying against looks like this:
{
"id": "09152014101",
"OrderDate": "2014-09-15T23:14:25.7251173Z",
"ShipDate": "2014-09-30T23:14:25.7251173Z",
"Total": 113.39
}
Documentation link
I believe this is an optimisation deficiency when the query uses TOP and ORDER BY. I've found that whilst there is not much difference in RU for a range query using timestamp as number and timestamp as string, in scenarios such as yours the range index on string appears to be ignored.
User Voice issue here:
https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/32345410-optimise-top-with-order-by-clause-queries

How to fuzzy query against multiple fields in elasticsearch?

Here's my query as it stands:
"query":{
"fuzzy":{
"author":{
"value":query,
"fuzziness":2
},
"career_title":{
"value":query,
"fuzziness":2
}
}
}
This is part of a callback in Node.js. Query (which is being plugged in as a value to compare against) is set earlier in the function.
What I need it to be able to do is to check both the author and the career_title of a document, fuzzily, and return any documents that match in either field. The above statement never returns anything, and whenever I try to access the object it should create, it says it's undefined. I understand that I could write two queries, one to check each field, then sort the results by score, but I feel like searching every object for one field twice will be slower than searching every object for two fields once.
https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzzy-match-query.html
If you see here, in a multi match query you can specify the fuzziness...
{
"query": {
"multi_match": {
"fields": [ "text", "title" ],
"query": "SURPRIZE ME!",
"fuzziness": "AUTO"
}
}
}
Somewhat like this.. Hope this helps.

elasticsearch query dates by range

My elasticsearch has data, particularly something like this for dates:
{
"startTime": {
"type": "string",
"format": "yyyy/MM/dd",
"index": "analyzed",
"analyzer": "keyword"
}
}
I am adding a date range picker and want to use the dates picked to go query elasticsearch for data with startTime inside this range chosen. I'm not sure how to structure this query to elasticsearch, or if it will even work with this being a string field (I can potentially change it, though).
can anyone help me here?
Your field is a string, the format property is ignored. You should change your mapping and use the date type. Have a look here to see the core types available in elasticsearch.
I would use a filter instead of a query. It will be cached, thus faster. The following is an example for the last 7 days:
{
"filter" : {
"range" : {
"PublishTime" : {
"from" : "20130505T000000",
"to" : "20131105T235959"
}
}
}
}
Note that if you use the filter like this it's going to be the same filter the whole day, thus you would make good use of the cache.

Resources