elasticsearch bool query error in php client - php

ElasticSearch returns me [_na] query malformed, no field after start_object error when trying to look up entries using the following query. The field localtime is a new field of documents and exist in every document.
php code,
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array(
"bool" => array(
"must" => array(
"range" => array(
"localtime" => array(
"from" => "2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
),
"term" => array(
"query" => "1.2.3.4",
"fields" => array("ip")
),
"query_string" => array(
"query" => "*up*",
"default_field" => array("_all")
)
)
)
);
Why does this error appear?
anyhelp will be appreciated ! thanks!

Your bool/must clause must be a pure array not an associative array:
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array (
"bool" => array (
"must" => array(
array(
"range" => array (
"localtime" => array (
"from" =>"2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
)
),
array(
"term" => array(
"ip" => "1.2.3.4"
)
),
array(
"query_string" => array(
"query" => "*up*",
"default_field" => "_all"
)
)
)
)
)
);

Related

Uncaught exception 'MongoDB\Driver\Exception\RuntimeException' with message 'An object representing an expression must have exactly one field:

I have written like below lines of code
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)
),
array(
'$project' => array(
'AllotmentsDetails' => array(
'$filter' => array(
'input' => '$AllotmentsDetails',
'as' => 'allot',
'cond' => array(
'$and' => array(
'$lt' => array('$$allot.ToDate', new MongoDB\BSON\UTCDateTime((new DateTime())->getTimestamp() * 1000)),
'$eq' => array('$$allotment.RoomId', $this->RoomId)
)
)
)
),
)
)
))->toArray();
It is throwing error message " Uncaught exception 'MongoDB\Driver\Exception\RuntimeException' with message 'An object representing an expression must have exactly one field:"
Please help!!!
Just missing array notations and matching up in some wrong places:
$pipeline = array(
array( '$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)),
array('$project' => array(
'AllotmentsDetails' => array(
'$filter' => array(
'input' => '$AllotmentsDetails',
'as' => 'allotment',
'cond' => array(
'$and' => array(
array('$lt' => array(
'$$allotment.ToDate',
new MongoDB\BSON\UTCDateTime((new DateTime())->getTimestamp() * 1000)
)),
array('$eq' => array('$$allotment.RoomId', $this->RoomId))
)
)
)
)
))
);
$cursor = $this->collection->aggregate($pipeline)->toArray();
or since we are in a modern world, a bit easier to read:
$pipeline = [
['$match' => [
"_id" => new MongoDB\BSON\ObjectID($this->id)
]],
['$project' => [
'AllotmentsDetails' => [
'$filter' => [
'input' => '$AllotmentsDetails',
'as' => 'allotment',
'cond' => [
'$and' => [
['$lt' => [
'$$allotment.ToDate',
new MongoDB\BSON\UTCDateTime((new DateTime())->getTimestamp() * 1000)
]],
['$eq' => ['$$allotment.RoomId', $this->RoomId] ]
]
]
]
]
]]
];
$cursor = $this->collection->aggregate($pipeline)->toArray();
I suggest a better editor and also to use json_encode() on your data structures in order to check that the generated JSON matches what you see as examples in the documentation.

Mongodb date range fetching data is not working in aggregation

Below is my one of sample document in mongodb
{
"_id" : ObjectId("59c5fcedc817c3da1b8b456a"),
"zoneid" : "20",
"accountid" : "34",
"sitename" : "aaa",
"requestid" : "2efa1052-7255-41cd-9d39-8628dac5ed3e",
"winbid" : NumberLong("1"),
"bidder" : "innity",
"cpm" : NumberLong("1"),
"responsetime" : NumberLong("207"),
"slotid" : "20x300x250x34x9206",
"viewerid" : "_87cd184234b181d1c601144adc0c3621",
"uniquecount" : NumberLong("0"),
"datetime" : "2017-09-23 06:19:25"
}
and below is my code to fetch data in mongodb in php,
$ops = array(
array(
'$group' => array(
"_id" => array(
"slotid" => '$slotid',
"bidder" => '$bidder',
"viewerid" => '$viewerid',
"datetime" => '$datetime'
) ,
"total" => array(
'$sum' => 1
) ,
"sitename" => array(
'$addToSet' => '$sitename'
) ,
"aid" => array(
'$addToSet' => '$accountid'
)
)
) ,
array(
'$match' => array(
"datetime" => array(
'$gt' => '2017-09-23 06:00:00'
)
)
)
);
$reqbids = $db->requestbids->aggregate($ops);
Its return empty result but if I remove either '$group' array or '$match' array means result will shown,but both will used means empty result shown,like below query its shown result
$ops = array(
array(
'$group' => array(
"_id" => array(
"slotid" => '$slotid',
"bidder" => '$bidder',
"viewerid" => '$viewerid',
"datetime" => '$datetime'
) ,
"total" => array(
'$sum' => 1
) ,
"sitename" => array(
'$addToSet' => '$sitename'
) ,
"aid" => array(
'$addToSet' => '$accountid'
)
)
)
);
$reqbids = $db->requestbids->aggregate($ops);
In above query I removed '$match' array , then its shown result.whats the issue in the code,Anyone help to solve my problem,Any help appreciated...

How to convert mongoDB query into PHP?

My query is working in mongodb and produce output Properly. But same query i tried to run using php then it gives following error
Array
(
[ok] => 0
[errmsg] => A pipeline stage specification object must contain exactly one field.
[code] => 16435
)
Following is my MongoDB Query
db.sample_coll.aggregate(
{
$unwind: {
path:"$KeyValues",
includeArrayIndex:"arrayIndex",
preserveNullAndEmptyArrays:true
}
},
{
$project: {
timestamp:{
"$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}]
} ,
"InputVolt":"$KeyValues.InputVolt",
arrayIndex:1
}
},
{
$match: {
$and: [
{InputVolt: {$ne: null}}
]
}
}
);
The Above query is converted in php
$pipeline = array(
array('$unwind' =>
array( 'path' => '$KeyValues'),
array( 'includeArrayIndex' => 'arrayIndex' ),
array( 'preserveNullAndEmptyArrays' => 'true' )
),
array(
'$project' => array(
'timestamp' => array(
'$add' => array(
'$EventTS',
array('$multiply' => array( 60000, '$arrayIndex' ))
)
),
array( 'InputVolt' => array( '$KeyValues', 'InputVolt' ) ) ,
array('arrayIndex' => 1)
)
),
array(
'$match' => array(
'$and' => array(
array('InputVolt' => array('$ne' => null )),
)
)
)
);
$result = $collection->aggregate($pipeline);
Kindly help to resolve this issue.
Thanks in advance
Your pipeline in PHP:
$pipeline = array(
array('$unwind' => array(
'path' => '$KeyValues',
'includeArrayIndex' => 'arrayIndex',
'preserveNullAndEmptyArrays' => true
)),
array('$project' => array(
'timestamp' => array(
'$add' => array('$EventTS', array('$multiply' => array(60000, '$arrayIndex')))
),
'InputVolt' => '$KeyValues.InputVolt',
'arrayIndex' => 1
)),
array('$match' => array(
'InputVolt' => array('$ne' => null)
))
);

Sement by tag Onesignal

I try send push notifications only to users segmented by tags from my app with Onesignal by php but the notificatión is send to all users, this is my code:
$fields = array(
'app_id' => $onesignal_wp_settings['app_id'],
'headings' => array("en" => get_the_title($post->ID), "es" => get_the_title($post->ID)),
'isAnyWeb' => false,
'url' => get_permalink($post->ID),
'contents' => array("es" => max_words(array(
"max" => 40,
"id_post" => $post -> ID
)),
"es" => max_words(array(
"max" => 40,
"id_post" => $post -> ID
))),
'tags' => array(
array(
"key" => "municipio",
"relation" => "=",
"value" => (string)$id_municipio
),
array(
"operator" => "OR"
),
array(
"key" => "estado",
"relation" => "=",
"value" => (string)$id_estado
)
)
);
The contents field of your code isn't correct. It can only contain an array of languages with a string for it's contents. Your post_id should be added to data instead.
'contents' => array("en" => "English message",
"es" => "Spanish message"),
'data' => array("id_post" => $post -> ID)
Your tags field looks correct however I am not sure what your variables contain. You should print out your full JSON payload to make sure you don't have any errors in the format.

Mysql Between query in elasticsearch

Currently I am Working on Elastic Search For My Current Project. I need MySql BETWEEN in my elastic. I searched So Many Examples But Not Get Any Solution I need to sort using no. of employees by Company. I already Wrote Some Filters Its Working Fine. But I Need Range Filter. and my code as follows
$query = array("from" => $start,
"size" => $recordslimit,
"sort" => array(array('id' => 'desc')),
"query" => array(
"filtered" => array(
"query" => array("match_all" => array()),
"query" => (!empty($keyword)) ? array("match" => array('_all' => $keyword)) : null,
"filter" => array(
"bool" => array(
'should' => array(
array("match" => array('location' => $headerLocation))
),
'must' => array(
($type == '1') ? array('term' => array('user_sub_type' => '1')) : null,
array('term' => array('user_type' => 'v')),
array('term' => array('status' => 'a'))
),
'must_not' => array(
array('term' => array('subscription_type' => 'n'))
))
)
))
);
}
}
$data = $this->elasticsearch->advancedquery("users",json_encode($query));
this is current query.. and also i add in filter
"range" => array(
'num_of_employees' => array(
'gt' => 100,
'lte' => 1000
)
)
please helpout with this situation. Thanks in Advanced.

Categories