How to handle Dimensions with multiple keys in Crossfilter.js - dc.js

I have data which is similar to the json below.
data = [
{"student_id": 1, "clubs": ["Photography", "Drama" ], "sports": ["Soccer", "Baseball", "Biking"], "gender": "Male", "age" : 12},
{"student_id": 2, "clubs": ["Debating", "Drama", "Art" ], "sports": [ "Basketball", ], "gender": "Male", "age" : 15},
{"student_id": 3, "clubs": ["Math Club", "Drama" ], "sports": ["Swimming", "Running", "Biking"], "gender": "Male", "age" : 12},
{"student_id": 4, "clubs": [ "Drama", "Art" ], "sports": [ "Baseball", ], "gender": "Male", "age" : 11},
{"student_id": 5, "clubs": ["Math Club", "Photography" ], "sports": ["Basketball", "Biking"], "gender": "Female", "age" : 12},
{"student_id": 6, "clubs": ["Drama", "Art" ], "sports": [ "Volleyball", ], "gender": "Female", "age" : 16},
]
Can I use crossfilter.js to answer questions ( essentially group count or sum ) such as how many students are members of the Photography Club and play Basketball.
Essentially use a have chart for Clubs and another one for Sports.
Display the counts for each of the attributes ( dimensions ) and filter across them.
Yes the students may get double counted because they could be members of many clubs or playing many sports.

Related

Elasticsearch price range filter with currency convert (by selected currency)

The flow of my project :
One user register and enter new entity with the price: 100 | currency: EUR
Another user registered and creates a new entity with price: 252 | currency: USD
Third user registered and creates a new entity with price: 321 | currency: INR
What if I am trying to filter the records by price range (minimum price and maximum price) and selected currency can be any currency (EUR or USD or INR or any forth currency).
{
"_index": "tutors",
"_type": "tutors",
"_id": "1",
"_score": 2,
"_ignored": [
"biography.keyword"
],
"_source": {
"first_name": "Stewart"
"last_name": "Brado"
"currency": "INR"
"local_rates": [
{
"subject_id": 374,
"hourly_rate": 1200,
"subject_en_name": "ACT",
"country_id": 2
},
{
"subject_id": 350,
"hourly_rate": 700,
"subject_en_name": "Accounting",
"country_id": 1
}
]
}
},
{
"_index": "tutors",
"_type": "tutors",
"_id": "2",
"_score": 2,
"_ignored": [
"biography.keyword"
],
"_source": {
"first_name": "Nicole"
"last_name": "Cross"
"currency": "USD"
"local_rates": [
{
"subject_id": 371,
"hourly_rate": 654,
"subject_en_name": "Art",
"country_id": 1
},
{
"subject_id": 350,
"hourly_rate": 890,
"subject_en_name": "Accounting",
"country_id": 2
},
{
"subject_id": 150,
"hourly_rate": 1700,
"subject_en_name": "Autocad",
"country_id": 3
}
]
}
}
I have tried below query :
"function_score" => [
"functions" => [[
"script_score" => [
"script" => [
"params" => [
"USD" => 1,
"SGD" => 0.72,
"MYR" => 0.24,
"INR" => 0.014,
"EUR" => 1.12
],
"source" => "doc['local_rates.hourly_rate'].value * params.EUR"
]
]
]]
]

D3.js hierarchy calculated summary by field

Please I have flat json like this :
var data = [{ "Id": 1, "Name": "a1", "Parent": null},
{ "Id": 2, "Name": "a2", "Parent": 1},
{ "Id": 3, "Name": "a3", "Parent": 2, "nb1": 30, "nb2": 40 },
{ "Id": 4, "Name": "a4", "Parent": 2, "nb1": 25, "nb2": 64 },
{ "Id": 5, "Name": "a5", "Parent": 1},
{ "Id": 6, "Name": "a6", "Parent": 5, "nb1": 2, "nb2": 6 },
{ "Id": 7, "Name": "a7", "Parent": 5, "nb1": 5, "nb2": 4 }];
using d3js, I want to builde hierarchcal json with sum of each fields (nb1 and nb2) for all parents
I am using :
var treeData = d3.stratify().
id(function (d) { return d.Id; })
.parentId(function (d) { returnd.Parent; })(data);
treeData.sum(d=> d.nb1)
But it return new field (value) with sum.
What I need is sum of nb1 and keep the same field name nb1 and sum of nb2 and keep the same name field nb2 for parents.
In other way, customize the sum function of d3js hierarchy.
Thanks lot
Use recursive buildNode function:
const data = [
{ "Id": 1, "Name": "a1", "Parent": null},
{ "Id": 2, "Name": "a2", "Parent": 1},
{ "Id": 3, "Name": "a3", "Parent": 2, "nb1": 30, "nb2": 40 },
{ "Id": 4, "Name": "a4", "Parent": 2, "nb1": 25, "nb2": 64 },
{ "Id": 5, "Name": "a5", "Parent": 1},
{ "Id": 6, "Name": "a6", "Parent": 5, "nb1": 2, "nb2": 6 },
{ "Id": 7, "Name": "a7", "Parent": 5, "nb1": 5, "nb2": 4 }];
const getValue = (node, children, attr) =>
children.length > 0 ?
children.reduce((s, n) => s + n[attr], 0) :
node[attr];
const buildNode = (node) => {
const children = data
.filter(n => n.Parent === node.Id)
.map(n => buildNode(n));
const nb1 = getValue(node, children, 'nb1');
const nb2 = getValue(node, children, 'nb2');
return {...node, children, nb1, nb2};
}
const root = buildNode(data.find(n => !n.Parent));
console.log('ROOT: ', root)

Collapse/Group search query result by a field with supporting sort options

I have some documents like the following examples:
{"id": 1, "digest": "A", "date": "2020-05-07"}
{"id": 2, "digest": "A", "date": "2020-01-07"}
{"id": 3, "digest": "A", "date": "2020-03-07"}
{"id": 4, "digest": "B", "date": "2020-01-02"}
{"id": 5, "digest": "B", "date": "2020-01-01"}
{"id": 6, "digest": "C", "date": "2020-01-05"}
and I want to group the result of the search query by a field (like digest in this example) in the way that:
the result items sort by date descending
the group leader must be the record which has the minimum date (for example id=2 between 1,2,3 which have the same digest)
in summary, the result that I expected is something like:
[
{
"id": 2
"inner": [{"id": 1}, {"id": 3}],
"date": "2020-01-07"
},
{
"id": 6
"inner": [{"id": 6}],
"date": "2020-01-05"
},
{
"id": 5
"inner": [{"id": 4}],
"date": "2020-01-01"
}
]
I have tried collapsing fields but by this way, I can't select group leader item and because digest is a hash value, not a static tags, I can't find aggregation method for that.

How to retrieve Json data type data in SQL using a where condition in LARAVEL

Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?
I want to display all the order that contains order->Product->user->id === 1
{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
{
"id": 26,
"Product": [
{
"id": 4,
"name": "Araliya Rice",
"desription": "Araliya Rice",
"salePrice": 500,
"category": "Rice",
"user": {
"id": 1,
"name": "Mohamed Naalir",
}
}
],
},
{
"id": 27,
"Product": [
{
"id": 2,
"name": "white sugar",
"desription": "aaa",
"salePrice": 100,
"category": "Sugar",
"user": {
"id": 5,
"name": "Mohamed Sharaf",
}
}
],
}
]
}
json where clauses
$orders = DB::table('orders')
->whereJsonContains('Product', [['user' => ['id' => 1]]])
->get();

elasticksearch nested query

I have a index with some nested information and right now struggle with how make to make a query.
The index below represents a car piece and this piece belongs to 2 cars.
{
"_index": "items_production_20180411115923024",
"_type": "item",
"_id": "1232",
"_score": 21.715849,
"_source": {
"description": "CUBO RODA TRASEIRA VW:GOL, VOYAGE G5/G6 09> C/ABS",
"observation": null,
"brand_id": "1 ",
"product_line_id": "13",
"line_id": "1",
"line": "Rolamentos",
"segment_id": "21",
"segment": "cubo de roda",
"application_features": [
"4 furos"
],
"original_codes": [
" 5u0 501 611 "
],
"original_brands": [
"volkswagen"
],
"vehicles": [
{
"id": "285",
"brand": "volkswagen",
"name": "golf",
"years": [
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018"
]
},
{
"id": "345",
"brand": "volkswagen",
"name": "jetta",
"years": [
"2015",
"2016",
"2017",
"2018"
]
}
]
}
}
I have to make the query match to one single result when search for the model and year of a car. i.e.:
GET items_production_20180411115923024/_search
{
"query":{
"bool":{
"must":{
"multi_match":{
"query":"golf 2010",
"type":"cross_fields",
"operator":"and",
"fields":[
"vehicles.name^8",
"vehicles.years^8"
]
}
}
}
},
"size":10,"from":0
}
I have to return all docs which are pieces of a golf year 2010. But my response is none?
What am I doing wrong? How can I make this query?
I see you have a typo in the query. Replace vehicle_names^8 with vehicles.name^8.
Is vehicles.years an analyzed field? If not, it will not match against the term "golf 2010".
You might be able to use copy_to in your mapping to get terms from the name and the year in one field. Then you can do a simple query against that.

Resources