$multiply function not working in R studio - r

this is my code chunk
db.collection.aggregate([
{
"$unwind": "$items"
},
{
"$group": {
"_id": "$_id"
}
},
{
"$addFields": {
"total": {
"$multiply": [
"$items.quantity",
"$items.price"
]
}
}
},
{
"$limit": 10
}
])
This is what it outputs
This is a sample document

The problem you're facing is due to your $group stage. it works similar to SQL's groupby if you're more familiar.
What this means is that the quantity and price field are lost during that stage. you'll have to modify it a little to retain them.
I'm not sure exactly what you're trying to achieve as you did not specify but here is a toy example of what I imagine you're looking for:
db.collection.aggregate([
{
"$unwind": "$items"
},
{
"$group": {
"_id": "$items.name",
quantity: {
$sum: "$items.quantity"
},
price: {
$first: "$items.price"
}
}
},
{
"$addFields": {
"total": {
"$multiply": [
"$quantity",
"$price"
]
}
}
},
{
$sort: {
total: -1
}
},
{
"$limit": 10
}
])
Mongo Playground

Related

2 filter dsl query looks the same and how to combine

In Kibana I created 2 filters:
raw.browserJs.isWebDriver is true and raw.browserJs.isWebDriver isnot true. why the edit query DSL is the same for both:
{
"query": {
"match": {
"raw.browserJs.isWebDriver": {
"query": true,
"type": "phrase"
}
}
}
}
Also, how can i add condition in order to have one large DSL query with:
{
"query": {
"match": {
"appName": {
"query": "temp",
"type": "phrase"
}
}
}
}
The query DSL showing in Kibana is not the actual query which is send to elasticsearch. A range filter for the selected period is added and filters are inverted. You can see the actual query in the underlying request that is send in your browser.
You filter where raw.browserJs.isWebDriver is not true will end up in something like:
{
"query": {
"bool": {
"must_not": [
{
"match_phrase": {
"raw.browserJs.isWebDriver": true
}
}
]
}
}
}
You can combine multiple conditions in one DSL query with the bool query.(https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)
The following query will work in your example:
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"raw.browserJs.isWebDriver": true
}
},
{
"match_phrase": {
"appName": "temp"
}
}
]
}
}
}

How to change the include section of an AQL query in a file spec

I want to change the output of a AQL string formatted as a file spec for Artifactory.
The query looks like this:
{
"files": [
{
"aql": {
"items.find":{
"repo":"gradle-dev-local",
"$or":[
{
"$and": [
{ "stat.downloads": { "$eq":null } },
{ "updated": { "$before": "7d" } }
]
},
{
"$and": [
{ "stat.downloads": { "$gt": 0 } },
{ "stat.downloaded": { "$before": "30d" } }
]
}
]
}
}
}
]
}
In a pure AQL REST API call, I would include the following:
"include":["repo", "name", "path", "updated", "sha256", "stat.downloads", "stat.downloaded"]
But when used, it does not get passed in to the right part of the query, resulting in the following error message:
Failed to parse query: items.find({
"repo":"mfm-gradle-dev-local",
"$or":[
{
"$and": [
{ "stat.downloads": { "$eq":null } },
{ "updated": { "$before": "7d" } }
]
},
{
"$and": [
{ "stat.downloads": { "$gt": 0 } },
{ "stat.downloaded": { "$before": "30d" } }
]
}
]
},
"include":["repo", "name", "path", "updated", "sha256", "stat.downloads", "stat.downloaded"]
).include("name","repo","path","actual_md5","actual_sha1","size","type","property"), it looks like there is syntax error near the following sub-query: "include":["repo", "name", "path", "updated", "sha256", "stat.downloads", "stat.downloaded"]
How do I format the AQL so that the include statement gets passed as well?
If you're using the JFrog CLI, there is an open issue (github.com/jfrog/jfrog-cli-go/issues/320) for being able to add includes in the search queries (both using the -s parameter and file specs). Please feel free to add additional information to that issue, if we've missed anything so far.

Storing arrays in DynamoDB with AppSync with Resolver Mapping Template (edited)

I'm having some trouble storing arrays in DynamoDB on PutItem with AppSync, and it's turning me crazy :P
The problem is that the array I pass from GraphQl disappears when I run $util.dynamodb.toMapValuesJson. What am I doing wrong?
I'm really stuck on this, and would very much appreciate any help I can get!
This is the template:
#set($failing_list = [ "foo", 123, { "bar" : "baz" } ])
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key": {
"userId": { "S": "$ctx.args.input.email" },
"createdAt": { "N": "$util.time.nowEpochMilliSeconds()" }
},
"data" : {
"test": $util.dynamodb.toMapValuesJson({
"working_list": [ "foo", 123, { "bar" : "baz" } ],
"failing_list": $failing_list
})
}
}
This is the result:
{
version: '2017-02-28',
operation: 'PutItem',
key: {
userId: { S: 'xxxxxxxxxxxx' },
createdAt: { N: '1531521284789' }
},
data: {
test: {
working_list: {
L: [ { S: 'foo' }, { N: '123' }, { M: { bar: { S: 'baz' } } } ]
},
failing_list: {
M: { L: { L: [ { M: { map: { M: {} } } }, { M: { map: { M: {} } } }, { M: { map: { M: {} } } } ] } }
}
}
}
}

What is the best way to query the document closest to a date-time on elasticsearch?

I need to retrieve the document that has the closest geo location and date-time to the request, so I'm not looking for a match of the date-time, but the closest one. I solved it using a custom script, however I'm guessing there might be a better way to do it, similar to the way I'm filtering the geo location based on a location and a distance.
Here's my code (in python):
query = {
"query": {
"function_score": {
"boost_mode": "replace",
"query": {
"filtered": {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "10km",
"location" : json.loads(self.request.body)["location"]
}
}
}
},
"script_score": {
"lang": "groovy",
"script_file": "calculate-score",
"params": {
"stamp": json.loads(self.request.body)["stamp"]
}
}
}
},
"sort": [
{"_score": "asc"}
],
"size": 1
}
response = requests.get('http://localhost:9200/meteo/meteo/_search', data=json.dumps(query))
The custom calculate-score.groovy script contains the following:
abs(new java.text.SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm").parse(stamp).getTime() - doc["stamp"].date.getMillis()) / 60000
The script returns the score as the absolute difference in minutes between the document date-time and the requested date-time.
Is there any other way to achieve this?
You should be able to use function_score to do this.
You could use the decay functions mentioned in the doucmentation to give a larger score to documents closer to the origin timestamp. Below is the example
where the scale=28800 mins i.e 20d.
Example:
put test
put test/test/_mapping
{
"properties": {
"stamp": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
put test/test/1
{
"stamp":"2015-10-15T00:00"
}
put test/test/2
{
"stamp":"2015-10-15T12:00"
}
post test/_search
{
"query": {
"function_score": {
"functions": [
{
"linear": {
"stamp" : {
"origin": "now",
"scale": "28800m"
}
}
}
],
"score_mode" : "multiply",
"boost_mode": "multiply",
"query": {
"match_all": {}
}
}
}
}

Elasticsearch Date Range Aggregation with Sum

I've followed Elastic's docs and successfully queried my index to return data for two date ranges.
What I can't get right, is adding a sum of units to each range. I've managed to add a unit sum (see query below) but this only returns a sum for the entire range.
What I'm trying to achieve is a period A vs B comparison. i.e. This week you've sold X units vs Y units last week and therefore the % variance is Z.
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"*",
"analyze_wildcard":true
}
}
}
},
"size":0,
"aggs":{
"vendor_type":{
"terms":{
"field":"vendor_type",
"size":5
},
"aggs":{
"product_type":{
"terms":{
"field":"product_type",
"size":5,
"order":{
"unit_sum":"desc"
}
},
"aggs":{
"range":{
"date_range":{
"field":"date",
"format":"MM-yyy",
"ranges":[
{
"to":"now-1M/M"
},
{
"from":"now-1M/M"
}
]
}
},
"unit_sum":{
"sum":{
"field":"units"
}
}
}
}
}
}
}
}
Can anyone help? Thanks in advance.
You're almost there, you just need to move (or copy) your unit_sum inside the range aggregation, like this:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*",
"analyze_wildcard": true
}
}
}
},
"size": 0,
"aggs": {
"vendor_type": {
"terms": {
"field": "vendor_type",
"size": 5
},
"aggs": {
"product_type": {
"terms": {
"field": "product_type",
"size": 5
},
"aggs": {
"range": {
"date_range": {
"field": "date",
"format": "MM-yyy",
"ranges": [
{
"to": "now-1M/M"
},
{
"from": "now-1M/M"
}
]
},
"aggs": {
"unit_sum": {
"sum": {
"field": "units"
}
}
}
}
}
}
}
}
}
}

Resources