Elasticsearch: Aggregations with conditions on both parent and child - parent-child

I have two indices named "ride" and "audit_log" where audit_log is child of ride. I need to fetch average of timestamp from audit_log table(index) based on some conditions. Conditions span to both parent and child. The query, I am trying to execute is:
curl -XGET 'http://localhost:9200/rides/audit_log/_search' -d
{
"size":0,
"query":{
"has_parent":{
"parent_type":"ride",
"query":{
"match":{
"ride_status":"Ride Completed"
}
}
},
"match":{
"status":"Driver Confirmed"
}
},
"aggs":{
"avg_time":{
"avg":{
"field":"createdAt"
}
}
}
}
here ride_status is from parent table ride.
And, I am getting following error from this api hit:
[has_parent] malformed query, expected [END_OBJECT] but found
[FIELD_NAME]","line":1,"col":107

Try using the term instead of match.
{
"size":0,
"query":{
"has_parent":{
"parent_type":"ride",
"score" : true, <---- add this
"query":{
"term": {
"ride_status":"Ride Completed"
}
}
},
"term": {
"status":"Driver Confirmed"
}
},
"aggs":{
"avg_time":{
"avg":{
"field":"createdAt"
}
}
}
}

Related

Why can't I use regexp wildcards in Kibana Query DSL?

I have to filter on a word in log.file.path. It works when I use a full string like this:
{
"query": {
"regexp": {
"log.file.path": "/Logs/APP/Business_TracingPerformance.POOL-16.txt"
}
}
}
But when I use a wildcard, nothing come out anymore
{
"query": {
"regexp": {
"log.file.path": "/Logs/APP/Business_TracingPerformance.*.txt"
}
}
}
Is there a reason why it doesn't work?

How Haetoas conform add Links

I want to add links to some related entities and collection, without all property data in one response, only the Link.
For unterstanding I structure the Question in some parts
Simple example of data Model
Order
Order->AddressFrom (Entity)
Order->PackageItems (Collection)
Which is the wright HAETOAS way to generate links or how to name entities in response.
First question: How to link address, double in entity name and links part and how to nam?
{
"_embedded":{
"orders":[
{
"id":"id",
"addressLinkFrom":{
"href":"link"
},
"_links":{
"self":{
"href":"link"
},
"addressFrom":{
"href":"d"
}
}
}
]
}
}
or
"addressFrom":{
"href":"link"
},
"_links":{
...
}
or
or
"addressFrom":{
"self":{
"href":"link"
}
},
"_links":{
...
}
Second: How to link collection to specific and not all entities
{
"_embedded":{
"orders":[
{
"id":"id",
"packageItemIds":[
{
"href":"link"
},
{
"href":"link"
}
]
}
]
}
}
or
"packageItemIds":[
{
"self:"{
"href":"link"
}
}]
Third: How is the right format for (update, post), which I have to parse, like the address, where I'm sending the ID or like the packageItems, where I sending the link.
{
"id":"id",
"addressFrom":{
"id":"12345"
}
"packageItems":[
{
"href":"link"
},
{
"href":"link"
}
]
}
For your information, as frontend Client I'm using lagoshny /
ngx-hateoas-client,
when someone has their extra information beside the HAETOAS standard, how is the best way, please tell me.
Thanks for your help.I want to add links to some related entities and collection, without all data in one response.

Firebase database().ref() nonexistent path

In firebase, my app will be creating nested data all the time. My question is do I first have to check if a key exists before i can reference it, or can i just...
const ref = firebase.database().ref("dataTree/" + childVar1 + "/" + childVar2);
ref.push({data: "this is data"});
and it will create the nested structure for me? i.e. :
{
"dataTree": {
"notChildVar1": {
"someChildName": {
"data": "test"
}
}
"alsoNotChildVar1": {
"someChildName": {
"data": "test"
}
}
}
}
Should Become...
{
"dataTree": {
"notChildVar1": {
"someChildName": {
"data": "test"
}
}
"alsoNotChildVar1": {
"someChildName": {
"data": "test"
}
}
"childVar1": {
"childVar2": {
"data": "test"
}
}
}
}
And if the above code will return an error, what can i do to test if a key exists, add it if it does not exist, and then push something in that?
Maybe you can try something like create the full content of childVar1 and insert directly in your database including the childVar2: {data: test} (I supposed that you're using RealtimeDatabase).
const ref = firebase.database().ref("dataTree/" + childVar1);
ref.push(
"childVar2": {
"data": "this is data"
}
);
You can write to any arbitrary location in the database, and any required nodes that do not exist will be created for you automatically. You don't have to create each child node individually.
Similarly, if all children of a node are deleted, the node itself will be deleted automatically (there is no such thing as an "empty" child node).

Elasticsearch - Count distinct

I have a basic index with logs
Some logs are visit of user1 to user2
I managed to count the total of visits a user has received, but I don't know how count the total of distinct users a user has received
This is giving me all the logs for a user
{
"post_filter":{
"bool":{
"must":[
{
"term":{
"message":"visit"
}
},
{
"term":{
"ctxt_user2":"733264"
}
}
]
}
},
"query":{
"match_all":{}
}
}
Actually, I'm using FoSElasticaBundle for Symfony2
$filter->addMust((new Term())->setTerm('message', 'visit'));
$filter->addMust((new Term())->setTerm('ctxt_user2', $this->search->getVisit()));
I read some pages in the ES doc with aggregator, but I never managed to get what I want
Convert to SQL, I just need
SELECT COUNT(DISCTING ctxt_user1)
FROM logs
WHERE ctxt_user2 = 733264
EDIT:
Cardinality seams to be what I need.
Now just need to find how use it with FosElasticaBundle
"aggs": {
"yourdistinctcount": {
"cardinality": {
"field": "ctxt_user1"
}
}
}
Try this query ( not tested...):
{
"query" : {
"bool":{
"must":[
{
"term":{
"message":"visit"
}
},
{
"term":{
"ctxt_user2":"733264"
}
}
]
}
},
"aggs": {
"yourdistinctcount": {
"terms": {
"field": "ctxt_user1"
}
}
}
}
The post_filter query cannot be used in your case. As it write on Elastic.co website: The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated.`
HtH,

Foselastica get only best matching child

NOTE: elasticsearch 1.7 and foselastica 3.1 is used!
How do I get only the best matching child in parent/child or nested?
I need to query the whole index (parent and chied fields). The query returns parents, and their children. But I only need parent and best matching child for the query.
I've tried to achieve it with TopChildren, HasChild and so on, but its not working.
This is what my query looks like
{
"query":{
"has_child":{
"type":"variants",
"query":{
"filtered":{
"query":{
"bool":{
"must":[
{
"match":{
"allField":{
"query":"shirt rot",
"operator":"AND",
"minimum_should_match":"80%",
"fuzziness":"0.8",
"analyzer":"custom_search_analyzer"
}
}
}
],
"should":[
{
"match":{
"shortName":{
"query":"shirt rot",
"operator":"AND",
"analyzer":"custom_search_analyzer",
"boost":5
}
}
}
]
}
},
"filter":{
"bool":{
"should":[
{
"terms":{
"customized":[
0
]
}
}
]
}
}
}
},
"max_children":1
}
}
}

Resources