Elasticsearch (PHP) multiple range filters issue - php

I am pretty new to Elasticsearch, so apologies if this is is an obvious question!
I am using the PHP library and trying to do a pretty straightforward query across an index. The only thing I wanted to do was perform a filter against two dates (visible_from and visible_to) which I have set to date types when creating the mapping.
I am building up a filtered query in the code, which ends up looking like this:
array (
'index' => 'site',
'from' => 0,
'size' => 10,
'body' =>
array (
'query' =>
array (
'filtered' =>
array (
'filter' =>
array (
'and' =>
array (
'range' =>
array (
'visible_from' =>
array (
'lt' => '2014-06-09 09:06:47',
),
'visible_to' =>
array (
'gt' => '2014-06-09 09:06:47',
),
),
),
),
'query' =>
array (
'match' =>
array (
'_all' => 'example',
),
),
),
),
),
)
This results in a BadRequest400Exception being thrown - in the content of the exception I can see that Elasticssearch is saying
QueryParsingException[[site] [and] filter does not support [lt]]
If I remove the AND filter and just perform the range filter against one of the dates, then it works perfectly, I am just unable to get it working with the AND filter
Thanks in advance for any help!

Make a try with below query, Try with different less than and greater than date.
array
(
"query" => array
(
"filtered" => array
(
"query" => array
(
"match" => array(
'_all' => 'example',
),
),
"filter" => array
(
"and" => array
(
"filters" => array
(
"0" => array
(
"range" => array
(
"ID" => array
(
"gt" => '2014-06-09 09:06:47',
),
),
),
"1" => array
(
"range" => array
(
"ID" => array
(
"lt" => '2014-06-09 09:06:47',
),
),
),
),
),
),
),
),
)

Related

How can I avoid that array_merge_recursive() tranform properties into arrays?

I'm trying to array_merge_recursive two arrays.
$arrayA = array(
'properties' => array(
'path' => array(
'default' => '',
),
'multiple' => array(
'default' => true,
),
)
);
$arrayB = array(
'properties' => array(
'path' => array(
'default' => 'foo',
),
'multiple' => array(
'default' => false,
),
)
);
$array = array_merge_recursive($arrayA,$arrayB);
print_r($array);
Which gives
Array (
[properties] => Array (
[path] => Array (
[default] => Array (
[0] =>
[1] => foo
)
)
[multiple] => Array (
[default] => Array (
[0] => 1
[1] =>
)
)
)
)
As you can see, the default and multiple properties are merged into arrays since it exists in both arrays.
But I don't want them to be transformed into arrays, I want that the last value to override the previous one.
And of course they DO exists in both array, I cannot declare it only in the second one.
How could I achieve this ?
Thanks !

How to output a value from a post meta which contains multiple arrayed post meta keys - values

I have a custom field fw_options which looks like below. I am trying to extract data such as gender, _books_read, other_skills (especially this) etc but I can't get my head around this type of custom field. I know how to output a custom field using get_post_meta( get_the_ID(), I just don't know how to extract data from this kind of format. I am a complete beginner in PHP but I'm really trying to understand.
My Code
<span>$ <?php echo get_post_meta( get_the_ID(), 'fw_options', true ); ?></span>
Post Meta Key - fw_options
Value (below)
0 =>
array (
'gender' => 'male',
'tag_line' => 'I like reading journals and thriller books,',
'_books_read' => 12,
'address' => '',
'longitude' => '',
'latitude' => '',
'country' =>
array (
0 => 720,
),
'other_skills' =>
array (
0 =>
array (
'other_skill' =>
array (
0 => '833',
),
'value' => '80',
),
1 =>
array (
'other_skill' =>
array (
0 => '507',
),
'value' => '99',
),
2 =>
array (
'other_skill' =>
array (
0 => '1169',
),
'value' => '99',
),
),
'current_books' =>
array (
),
'awards' =>
array (
),
'experience_level' =>
array (
),
'education_level' =>
array (
),
'banner_image' =>
array (
),
'resume' =>
array (
),
'tutorials' =>
array (
),
'english_level' => 'fluent',
'reader_type' => 'medium',
),
)

How can i access all values within a nested array in php by defining a specific key?

I am creating an routing application and get the result as an json array. After transforming it into an php array i get the whole distance and whole duration correctly. Now i need for every value in the key "legs" the distances and durations too but all i did to get the data doesnt work.
The json output of the array looks like this:
array (
'routes' =>
array (
0 =>
array (
'legs' =>
array (
0 =>
array (
'summary' => '',
'weight' => 3741.9,
'duration' => 2912.3, // This value is what i want access
'steps' =>
array (
),
'distance' => 21603.1, // This value is what i want access
),
1 =>
array (
'summary' => '',
'weight' => 3642.1,
'duration' => 2777.4, // This value is what i want access
'steps' =>
array (
),
'distance' => 21611.8, // This value is what i want access
),
),
'weight_name' => 'routability',
'weight' => 7384,
'duration' => 5689.700000000001, // This value i can acesss
'distance' => 43214.899999999994, // This value i can acesss too
),
),
'waypoints' =>
array (
0 =>
array (
'hint' => '',
'distance' => 16.78277948979663, // This value is what i want access
'name' => 'Weg',
'location' =>
array (
0 => 11.4623,
1 => 50.7126,
),
),
1 =>
array (
'hint' => '',
'distance' => 16.62835508134535,
'name' => 'Weg',
'location' =>
array (
0 => 12.6069,
1 => 51.5398,
),
),
2 =>
array (
'hint' => '',
'distance' => 16.78277948979663,
'name' => 'Weg',
'location' =>
array (
0 => 12.343,
1 => 51.576,
),
),
),
'code' => 'Ok',
)
The whole distance (43214.8) and whole duration (5689.7) i get by the following code:
foreach($res2['routes'] as $item)
{
$distances = array_push_assoc($distances, $item['distance'], $item['duration']);
}
In order to get the distances and durations i did the following:
foreach($res2['routes']['legs'] as $item)
{
$durations = array_push_assoc($durations , "DUR", $item['duration']);
}
How can i get the distances and durations from "legs"? Why doenst work $res2['routes']['legs']?
Thank you!
Do notice the the "legs" array exists in index 0 of the "routes" array so looping on it will require using $res2['routes'][0]['legs'].
Morever, notice that using array_push_assoc in loop with the same hard-coded key (as "DUR" in your example) will override the key each time so your data gets lost - you better change it to:
foreach($res2['routes'][0]['legs'] as $item) {
$durations[] = $item['duration'];
}

How to get distance with $near?

I can get my elements, with the following code :
$lines = $loc->find(
array("loc" =>
array('$near' =>
array('$geometry' =>
array('type' => 'Point', 'coordinates' =>
array(floatval($longitude), floatval($latitude))
),
'$maxDistance' => 10000 //meters
)
)
)
);
This is the results :
Array
(
[_id] => MongoId Object
(
[$id] => 5490003c815289663d8bbd95
)
[name] => My adress
[loc] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => 5.050948
[1] => 45.040419
)
)
)
But now, I need to get the distance from my given point.
Is it possible ?
EDIT :
This is the code used to insert datas :
$loc = $client->localisation->localisation;
$loc->ensureIndex( array("loc" => "2dsphere"));
$loc->insert(
array(
"idobject" => $myValue
"name" => $myName
"loc" => array("type" => "Point", "coordinates" => array($longitude, $latitude))
));
The $near operator does not return a distance along with the returned results. It only orders the documents in the response.
Under the PHP driver the easiest form is to use the $geoNear aggregation pipeline stage instead. This allows you to project a field in the results, or even use it in other pipeline stages:
$loc->aggregate(
array(
array(
'$geoNear' => array(
'near' => array(
'type' => 'Point',
'coordinates' =>array(floatval($longitude), floatval($latitude))
),
'maxDistance' => 1000,
'distanceField' => 'distance',
'spherical' => true
)
)
)
);
There is is a database command form for geoNear as well, but direct command results are often not as nice.

How to remove an item of array and update order index of array in mongodb

Now I have a "answers" array and I want to remove the first of this array but index item of this array is 1 but not 0. I don't know mongodb has support remove item array and update index array ?
My code:
Before remove:
array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (
'0' =>
array (
'by' => new MongoId("52818ec2d069fe90058b4567"),
'content' => '<p>Ok, testing<br /> </p> ',
'date' => new MongoInt32(1384222656),
),
'1' =>
array (
'by' => new MongoId("528192acd21b6310188b4567"),
'content' => 'test',
'date' => new MongoInt32(1384224360),
),
),
After remove the first item of answers array
array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (
'1' =>
array (
'by' => new MongoId("528192acd21b6310188b4567"),
'content' => 'test',
'date' => new MongoInt32(1384224360),
),
),
My result needs
array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (
'0' =>
array (
'by' => new MongoId("528192acd21b6310188b4567"),
'content' => 'test',
'date' => new MongoInt32(1384224360),
),
),
My English very bad but please help me !
use $db.collection.update( {array('_id' => new MongoId("52818f9ad069feed198b4567")),
array( $unset=> array(
'0'=> true
)
)
)
use $db.collection.update( {array(),
array( $unset=> array(
'0'=> true
)
)
)
to make the same modification for every documents.

Categories