How to select related events from the same table on Elasticsearch? - elasticsearch

How do I make the next self query on Elasticsearch?
SELECT e.user_id AS user_id,
e.datetime AS started_at,
(SELECT MIN(datetime) ## taking the closest "end" event datetime of that userId ##
FROM events
WHERE type = "end" AND
user_id = e.user_id AND
datetime > e.datetime) AS end_at,
FROM events AS e
WHERE e.type = "start"
Over the next event data table:
{"_id" : "1", "type": "start", "datetime": "2022-02-01T10:15Z", "userId": "1"},
{"_id" : "2", "type": "end", "datetime": "2022-02-01T10:20Z", "userId": "1"},
{"_id" : "3", "type": "start", "datetime": "2022-02-01T10:16Z", "userId": "2"},
{"_id" : "4", "type": "end", "datetime": "2022-02-01T10:21Z", "userId": "2"},
{"_id" : "5", "type": "start", "datetime": "2022-02-02T11:01Z", "userId": "1"},
{"_id" : "6", "type": "end", "datetime": "2022-02-02T11:02Z", "userId": "1"}
The expected result should look like:
user_id
started_at
end_at
1
2022-02-01T10:15Z
2022-02-01T10:20Z
2
2022-02-01T10:16Z
2022-02-01T10:21Z
1
2022-02-02T11:01Z
2022-02-02T11:02Z

Related

How to sort multiple ElasticSearch indexes with different priorities?

Is it possible to sort ElasticSearch results by their index. I don't want to change their score by index. But I have artists and songs indexes and I would like to show artist results first and then songs, preferable when keeping original score.
Is that possible?
Thank you.
you can sort by _index in a query, eg as below;
POST artist/_doc/1
{
"user_id" : 1234,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582720371,
"event_info" : "example info"
}
POST songs/_doc/2
{
"user_id" : 1234,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582760371,
"event_info" : "example info"
}
POST artist/_doc/3
{
"user_id" : 12345,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1582720371,
"event_info" : "example info"
}
POST songs/_doc/4
{
"user_id" : 1235,
"acc_id" : 4321,
"type_of_event" : "event",
"event_label" : "example activity",
"time_stamp" : 1592720371,
"event_info" : "example info"
}
POST /artist,songs/_search
{
"sort" : [ { "_index" : {"order" : "desc" }} ]
}
and the output is;
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "songs",
"_id": "2",
"_score": null,
"_source": {
"user_id": 1234,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582760371,
"event_info": "example info"
},
"sort": [
"songs"
]
},
{
"_index": "songs",
"_id": "4",
"_score": null,
"_source": {
"user_id": 1235,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1592720371,
"event_info": "example info"
},
"sort": [
"songs"
]
},
{
"_index": "artist",
"_id": "1",
"_score": null,
"_source": {
"user_id": 1234,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582720371,
"event_info": "example info"
},
"sort": [
"artist"
]
},
{
"_index": "artist",
"_id": "3",
"_score": null,
"_source": {
"user_id": 12345,
"acc_id": 4321,
"type_of_event": "event",
"event_label": "example activity",
"time_stamp": 1582720371,
"event_info": "example info"
},
"sort": [
"artist"
]
}
]
}
}
you can change desc to asc if you want

Vuetify's v-select doesn't show all the items for some reason

This is extremely weird for me - my code is very basic:
<v-select
chips
multiple
:items="areas"
v-model="person.applicant.areas"
:label="trans('Areas of interest')"
item-text="name"
item-value="id"
></v-select>
The items when echoed right before that with {{areas}} returns as follows:
[
{ "interestsAreas": "TEst", "id": 0, "name": "TEst" },
{ "interestsAreas": "Test2", "id": 1, "name": "Test2" },
{ "interestsAreas": "Something", "id": 0, "name": "Something" },
{ "interestsAreas": "1", "id": 1, "name": "1" },
{ "interestsAreas": "2", "id": 2, "name": "2" },
{ "interestsAreas": "3", "id": 3, "name": "3" }
]
Yet, whatever I do the v-select drops "Something" and "1" out of the dropdown even if I rename them or whatever.
I can't catch the logic behind dropping some of them, is there any known bug or something that I am not doing right in the invocation?
I am guessing it has something to do with the item-value, as id needs to be unique
[
{ "interestsAreas": "TEst", "id": 0, "name": "TEst" },
{ "interestsAreas": "Test2", "id": 1, "name": "Test2" },
{ "interestsAreas": "Something", "id": 0, "name": "Something" },// the id value is already used so try changing it
{ "interestsAreas": "1", "id": 1, "name": "1" },//same for this one as well
{ "interestsAreas": "2", "id": 2, "name": "2" },
{ "interestsAreas": "3", "id": 3, "name": "3" }
]

How do I extract unique data from Hyperledger Composer using query or js

I want to get unique data from a duplicates data at Hyperledger Composer. How can I achieve this?
suppose I have a data like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "2",
"dataType": "in",
"productName": “A",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "4",
"dataType": "in",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
what I want to get is a list of unique Product name.
So the result I want maybe something like this
[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "C",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
]
I don't really care about the sequence. I just want to know what productName do I have in my database.
I think this needs something to do with the logic.js part. I can do queries and get the data using post method. But I don't know how to do that using logic.js
I don't know how to get the data from logic.js. The example at official page only delete or trade. They don't return a new data.
any help is appreciated. I want to know how to send data using the js.
The js code to get the unique productname from your data would be
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const distinctProductName = [...new Set(data.map(x => x.productName))];
You will have your distinct ProductName in distinctProductName but you won't have the details of product. From your questions, it didnot feel necessary too. If you need the details too, you could do something like
const data =[
{
"$class": "org.stock.mynetwork.Commodity",
"time": "1",
"dataType" : "in",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "3",
"dataType": "out",
"productName": "A",
"quantity": 1,
"country": "Unknown",
"owner": "GAGA"
},
{
"$class": "org.stock.mynetwork.Commodity",
"time": "5",
"dataType": "out",
"productName": "B",
"quantity": 1,
"country": "Unknown",
"owner": "BABA"
}
];
const result = Array.from(new Set(data.map(x => x.productName)))
.map(productName => {
return{
productName: productName,
dataType : data.find(x => x.productName ===productName).dataType
//you can obtain other values as I obtained dataType
};
});
You will have your data in result.

Not able to plot diagram from Elasticsearch Lucene query in Grafana 2.x

I am using Grafana with Elasticsearchas data source.
On each of my documents, I have a field "id" and "lastmodifiedDate".
I would like to show the table with the count of id's accordingly last modified date.
here is my document:
{
"_index": "global_search",
"_type": "News_Item",
"_id": "48",
"_score": 1,
"_source": {
"id": "48",
"dueDate": 1478802600000,
"lastModifiedDate": 1478802600000,
"whenCreatedDate": 1477506600000,
"ownerEmail": "abc#gmail.com",
"owner": {
"firstname": "ABC",
"lastname": "Kumar",
"email": "abc#gmail.com"
},
"subject": "USA','According to NBCDFW.com, an explosion at a silicon plant ",
"status": "NEW",
"updatedby": "ABC Kumar",
"content": "Text data"
}
},
{
"_index": "global_search",
"_type": "News_Item",
"_id": "26",
"_score": 1,
"_source": {
"id": "26",
"dueDate": 1477679400000,
"lastModifiedDate": 1477679400000,
"whenCreatedDate": 1477506600000,
"researchTitle": "Flash Floods Hit Parts of Southern France",
"ownerEmail": "xyz#gmail.com",
"owner": {
"firstname": "XYZ",
"lastname": "Kumar",
"email": "xyz#gmail.com"
},
"subject": "Flash Floods Hit Parts of Southern France",
"status": "MONITORING",
"updatedby": "XYZ",
"content": "Text2 data"
}
}
Question: How to plot table with count of id's according date?What is the exact Lucene query to plot the table?
You can get it with below query parameters:
Metric:
Count
Group by:
type: Date Histogram
field: lastModifiedDate
Query field can be left blank.

Get particular key's and value's from JSON array - Ruby [duplicate]

This question already has answers here:
Ruby: Easiest Way to Filter Hash Keys?
(14 answers)
Closed 6 years ago.
I am a beginner with Ruby, and I have the following Json array:
"elements": [
{
"type": "Contact",
"id": "1",
"createdAt": "131231235",
"name": "test",
"updatedAt": "1456328049",
"accountName": "Mr Test",
"country": "China",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"type": "Contact",
"id": "2",
"createdAt": "156453447",
"name": "test2",
"updatedAt": "124464554",
"accountName": "Mr Test2",
"country": "Germany",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I want to filter out only a few keys + values: for example I want to return only the id,name,accountName,firstname and lastname.
So the exspected output is the following:
"elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I tried the following: create a filter array which has the elements I want to return and then map over the items but then I get stuck..
filters = []
filters.push("accountName")
filters.push("lastName")
filters.push("firstName")
filters.push("Id")
output["elements"].each do |item|
result = []
item.map {|key,value|filters.include? key}
result.push(?)
Thank you for the help.
Check this out, you should be able to work out from this:
output = { "elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
"somethoong": "sdsad"
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2"
}
]}
attribs = %w(accountName lastName firstName id)
output[:elements].each do |item|
item.delete_if{|k,v| !attribs.include?(k.to_s)}
end

Resources