Simple SQL query to Couchbase - couchbase

I have the following document:
{
"postId": "Message_2D73B43390041E868694A85A65E47A09D50F019C180E93BAACC454488F67A411_1375457942227",
"userId": "User_2D73B43390041E868694A85A65E47A09D50F019C180E93BAACC454488F67A411",
"post_message": "test",
"attachments": {
"images": [
],
"audio": [
],
"videos": [
]
},
"timestamp": 1375457942227,
"followers": [
],
"follow": 0,
"reporters": [
],
"report": 0,
"rerayz": 0,
"mtype": "post"
}
I would like do the following query:
SELECT * FROM posts WHERE users in ("User_1", "User_2", "User_3") ORDER_BY timestamp LIMIT 20
I did the following and I pass multiple ?keys=["User_1", "User_2", etc] . But how can I get the results sorted by timestamp in order to get only the 20 first?
function (doc, meta) {
if(doc.mtype == "post") {
emit(doc.userId, doc.post_message);
}
}
Any suggestions?
Thanks.

I see two choices here (there are likely more):
Emit the time stamp instead of the user id. loop through the results and filter out the users you want by using code instead of asking couchbase to do it for you. Loop until you have 20 matching records.
You could emit the user id followed by the timestamp. Then search for each user in turn and grab their last 20 posts (each). Put these in some sort of array or structure and sort them (using code) by the timestap. Grab the first 20 entries in your array (which contains 60 items if you have 3 users) and you have what you need.

Related

Using Power Query to extract data from nested arrays in JSON

I'm relatively new to Power Query, but I'm pulling in this basic structure of JSON from a web api
{
"report": "Cost History",
"dimensions": [
{
"time": [
{
"name": "2019-11",
"label": "2019-11",
…
},
{
"name": "2019-12",
"label": "2019-12",
…
},
{
"name": "2020-01",
"label": "2020-01",
…
},
…
]
},
{
"Category": [
{
"name": "category1",
"label": "Category 1",
…
},
{
"name": "category2",
"label": "Category 2",
…
},
…
]
}
],
"data": [
[
[
40419.6393798211
],
[
191.44
],
…
],
[
[
2299.652439184997
],
[
0.0
],
…
]
]
}
I actually have 112 categories and 13 "times". I figured out how to do multiple queries to turn the times into column headers and the categories into row labels (I think). But the data section is alluding me. Because each item is a list within a list I'm not sure how to expand it all out. Each object in the date array will have 112 numbers and there will be 13 objects. If that all makes sense.
So ultimately I want to make it look like
2019-11 2019-20 2020-01 ...
Category 1 40419 2299
Category 2 191 0
...
First time asking a question on here, so hopefully this all makes sense and is clear. Thanks in advance for any help!
i am also researching this exact thing and looking for a solution. In PQ, it displays nested arrays as a list and there is a function to extract values choosing a separating characterenter image description here
So this becomes, this
enter image description here
= Table.TransformColumns(#"Filtered Rows", {"aligned_to_ids", each Text.Combine(List.Transform(_, Text.From), ","), type text})
However the problem i'm trying to solve is when the nested json has multiple values like this: enter image description here
And when these LIST are extracted then an error message is caused, = Table.TransformColumns(#"Extracted Values1", {"collaborators", each Text.Combine(List.Transform(_, Text.From), ","), type text})
Expression.Error: We cannot convert a value of type Record to type Text.
Details:
Value=
id=15890
goal_id=323
role_id=15
Type=[Type]
It seems the multiple values are not handled and PQ does not recognise the underlying structure to enable the columns to be expanded.

How to build a Snowflake query to get these results

The below table (TMP_RN_TC) in query is a temp table which would be used to load the data into the final table. This table has to get the data from stage-table and the output of temp-table data needs to be stored in final table. Stage table will get data for 15-days in every run.
But the fact/final table should store all the data for the first run and then after only one day of data which would be changing (rest 14-days data would remain same). Since stage-table will hold even the duplicate data, temp-table should be able to remove those duplicates and load only data for the day from second run. Distinct didn't help. Below is the data and query:
For example, in the first run for 15-days, we get 30 records but in the second run, stage is getting 30-more records that is 60-records in stage now after 2nd run, but temp table should pick only 2-records since that would only have changed in the second run and rest 14-days(28-rows) data would be same.
This is the query I want to build:
1-row Data looks like:
{
"location": "xyz",
"metrics": [
{
"name": "traffic_in",
"data": [
{
"group": {
"start": "2020-07-05",
"type": "date"
},
"index": 0,
"next_level": [
{"index": 0,
"validity": "complete",
"value": 1,
"group": {
"finish": "00:15",
"start": "00:00",
"type": "time"
}
}
]
}
],
}
],
}
Below is the snowflake-query:
create or replace TABLE TMP_RN_TC as
(select * from(
select distinct
replace(D_NEXT : location , '"' , '')as rn_loc_id,
mtr.value:name::VARCHAR as metrics_name,
dta.value:group.start::DATE as metrics_event_date,
dta.value:index::numeric as metrics_date_index,
nxt.value:validity::VARCHAR as metrics_data_validity,
nxt.value:value::numeric as metrics_data_value,
nxt.value:group.start::time as metrics_data_start_tms,
nxt.value:index::numeric as metrics_time_index
from STG_RN_TC stg,
lateral flatten(input => stg.D_NEXT:metrics) mtr,
lateral flatten(input => mtr.value:data) dta,
lateral flatten(input => dta.value:next_level)nxt)
) ;
Note: from second run, I want only one day data to be coming in tmp-table which would be loaded in final table eventually.

how to overwrite a one to many records in odoo through default API

how to overwrite a one to many records in odoo through odoo API ?
This is my create json, what change I want to make in this json to overwrite(Replace) the existing? lead_product_ids., now it is appending the records. Now i am getting multiple when update the records in this code instead of 0,0 what is the value,
Please help.
{
"jsonrpc": "2.0",
"params": {
"model": "crm.lead",
"method": "create",
"args": [
{
"type": "opportunity",
"name": "Fgtrdhjkkmmmmmmmm1290",
"pro_info": "Fggggggg hhhhhh jkkkkkkknjj hjkll",
"tag_ids": [
6,
0,
[
43,42
]
],
"purposes_id": 3,
"lead_product_ids": [
***0,
0,***
{
"product_uom": 21,
"product_id": 148,
"description": "",
"qty": 1,
"price_unit": 2448,
"expected_price": 2448,
"discount": 0,
"tax_id": [
6,
0,
[
22
]
],
"price_subtotal": 2741.760009765625
}
],
"partner_id": 1592,
"religion": 2,
"age_bucket": "40_45",
"phone": "5695324877",
"mobile": "5695324878",
"locations_id": 157,
"district_id": 157,
"state_id": 593
}
]
}
}
The answer is found in the docstring of the Model.write():
"""
...
This format is a list of triplets executed sequentially, where each
triplet is a command to execute on the set of records. Not all
commands apply in all situations. Possible commands are:
``(0, _, values)``
adds a new record created from the provided ``value`` dict.
``(1, id, values)``
updates an existing record of id ``id`` with the values in
``values``. Can not be used in :meth:`~.create`.
``(2, id, _)``
removes the record of id ``id`` from the set, then deletes it
(from the database). Can not be used in :meth:`~.create`.
``(3, id, _)``
removes the record of id ``id`` from the set, but does not
delete it. Can not be used on
:class:`~odoo.fields.One2many`. Can not be used in
:meth:`~.create`.
``(4, id, _)``
adds an existing record of id ``id`` to the set. Can not be
used on :class:`~odoo.fields.One2many`.
``(5, _, _)``
removes all records from the set, equivalent to using the
command ``3`` on every record explicitly. Can not be used on
:class:`~odoo.fields.One2many`. Can not be used in
:meth:`~.create`.
``(6, _, ids)``
replaces all existing records in the set by the ``ids`` list,
equivalent to using the command ``5`` followed by a command
``4`` for each ``id`` in ``ids``.
.. note:: Values marked as ``_`` in the list above are ignored and
can be anything, generally ``0`` or ``False``.
"""
It's (1, id, {'field_1': value_1,'field_2': value_2,}). But you should use write instead of create because in create it doesn't make any sense to change non-existing records of a x2many field.

Extract multiple values with JSON extractor

I'm getting following data in response of a request:
{
"items": [
{
"id": 54925,
"currCode": "USD",
"lastUpdated": 1531233169000
},
{
"id": 54926,
"currCode": "USD",
"lastUpdated": 1531233169000
},
{
"id": 54927,
"currCode": "USD",
"lastUpdated": 1531233169000
}
],
"totalCount": 3
}
As we can see there are three different ids in the data(54925,54926,54927)
I want to perform iterate over all these ids and perform some operation( basically I want to use like foreach(String id: ids) { request(id);}
I added a JSON extractor as follows:
As per my research(research link) it's supposed to store all the ids in the id_list
After this added a foreach loop to iterate over these values:
But somehow the it's not going inside this for loop. What I'm doing wrong here?
is there any other way to fetch all these ids and loop through them?
You forgot to mention in JSON Extractor you expect it to return all values by setting Match No. as -1
-1 means extract all results, they will be named as _N

Grouping nested document

I'm new to elasticsearch querying and probably this question is not so smart, but I would appreciate any help. Is there any idea how I can query only events in the following sample json user session (field "l"):
{
"dvs_t": 103492673,
"l": [
{
"e": "SessionInfo",
"p": {
"Device": "samsung GT-P6800",
"SessionNumber": "36"
},
"ts": 103279627
},
{
"e": "InAppPurchaseCompleted",
"p": {
"ItemID": "sdbundle_stars_10",
"TimePlayed_Total": "3 - 3.25 Hours"
},
"ts": 103318595
}
],
"osv": "4.1.2",
"request": "ANME",
"srv_ver": "0.2"
}
For instance, can I somehow
count the number of InAppPurchaseCompleted events in the session?
count the number of InAppPurchaseCompleted events in the sessions grouped by session parameter request or any other parameter?
Filter in Elasticsearch allow you to search (really quickly) on a specific set of data. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html.
If you only want to return count you can use search type count http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-search-type.html#count