Skip to content

Commit f87202c

Browse files
authored
Update ElasticaQueryBuilder to support ruflin/elastica v5.x (#15)
* start elastica upgrade to 5.x and php7ification * php7ification nodes and abstract builder * Update ElasticaQueryBuilder to support ruflin/elastica v5.x * don't cast when scalar typehint already enforces type
1 parent 713f4e6 commit f87202c

35 files changed

+1094
-1017
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
# - hhvm # currently failing with: Message was: "DateTimeZone::__construct(): Unknown or bad timezone (+00:00)"
4+
- 7.1
5+
- 7.2
86

97
before_script:
108
- composer self-update

CHANGELOG-0.x.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
This changelog references the relevant changes done in 0.x versions.
33

44

5+
## v0.3.0
6+
__BREAKING CHANGES__
7+
8+
* Update `ElasticaQueryBuilder` to use `"ruflin/elastica": "~5.3"`.
9+
* Require php `>=7.1` in `composer.json`.
10+
* Add php7 type hinting and use `declare(strict_types=1);`.
11+
12+
513
## v0.2.1
614
* pull #9: Respect boolean operator preceding subquery.
715

816

917
## v0.2.0
1018
__BREAKING CHANGES__
1119

12-
* issue #7: Update ElasticaQueryBuilder to use 2.x queries/filters. Requires `"ruflin/elastica": "~3.2"`.
20+
* issue #7: Update `ElasticaQueryBuilder` to use 2.x queries/filters. Requires `"ruflin/elastica": "~3.2"`.
1321
* issue #6: Make TimeZone configurable on any builders that use date nodes.
1422
* The `Number` class was renamed to `Numbr` to prevent issue with scalar type hints in php7.
1523

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"type": "library",
66
"license": "Apache-2.0",
77
"require": {
8-
"php": ">=5.4",
8+
"php": ">=7.1",
99
"gdbots/common": "~0.1|~1.0"
1010
},
1111
"require-dev": {
12-
"phpunit/phpunit": "~4.3",
13-
"ruflin/elastica": "~3.2"
12+
"phpunit/phpunit": "~6.4",
13+
"ruflin/elastica": "~5.3"
1414
},
1515
"autoload": {
1616
"psr-4": {
@@ -22,9 +22,12 @@
2222
"Gdbots\\Tests\\QueryParser\\": "tests"
2323
}
2424
},
25+
"scripts": {
26+
"test": "vendor/bin/phpunit"
27+
},
2528
"extra": {
2629
"branch-alias": {
27-
"dev-master": "0.2.x-dev"
30+
"dev-master": "0.3.x-dev"
2831
}
2932
}
3033
}

examples/elastica.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ class EchoLogger implements \Psr\Log\LoggerInterface
1616
* @param mixed $level
1717
* @param string $message
1818
* @param array $context
19-
*
20-
* @return null
2119
*/
2220
public function log($level, $message, array $context = array())
2321
{
2422
echo $message.PHP_EOL;
23+
24+
// nuke hits from context since we print those
25+
// after response data anyways
26+
if (isset($context['response']) && isset($context['response']['hits']) && isset($context['response']['hits']['hits'])) {
27+
unset($context['response']['hits']['hits']);
28+
}
29+
2530
echo json_encode($context, JSON_PRETTY_PRINT).PHP_EOL.PHP_EOL;
2631
echo str_repeat(PHP_EOL, 2).str_repeat('*', 70).str_repeat(PHP_EOL, 2);
2732
}
@@ -34,11 +39,16 @@ public function log($level, $message, array $context = array())
3439
$client->setLogger(new EchoLogger());
3540

3641
$parser = new QueryParser();
42+
/** @var ElasticaQueryBuilder $builder */
3743
$builder = (new ElasticaQueryBuilder())
44+
->addNestedField('dynamic_fields')
3845
->setDefaultFieldName('_all')
3946
->setEmoticonFieldName('emoticons')
4047
->setHashtagFieldName('hashtags')
4148
->setMentionFieldName('mentions')
49+
->addFullTextSearchField('subject')
50+
->addFullTextSearchField('dynamic_fields.string_val')
51+
->addFullTextSearchField('dynamic_fields.text_val')
4252
->setLocalTimeZone(new DateTimeZone('America/Los_Angeles'))
4353
;
4454

@@ -58,13 +68,13 @@ public function log($level, $message, array $context = array())
5868
->setQuery($query)
5969
->setBoostMode(FunctionScore::BOOST_MODE_SUM)
6070
->addFunction('field_value_factor', [
61-
'field' => '__popularity',
71+
'field' => 'priority',
6272
'modifier' => 'none',
6373
], null, 0.4);
6474
*/
6575
$query = \Elastica\Query::create($query);
6676
//$query->setExplain(true);
67-
//$query->setSort(['published_at' => 'desc']);
77+
$query->setSort(['date_sent' => 'desc']);
6878
$results = $client->getIndex($index)->search($query, $options);
6979

7080
echo 'Total Time (ms) / Records Found:' . PHP_EOL;

0 commit comments

Comments
 (0)