Skip to content

Commit 654bb28

Browse files
authored
Use elasticsearch 2.x query style (#8)
* + re #7: Update ElasticaQueryBuilder to use 2.x queries/filters. + re #6: Make TimeZone configurable on any builders that use date nodes. + The `Number` class was renamed to `Numbr` to prevent issue with scalar type hints in php7. * node factory fix for php7 name change * re #7 make ES builder min match null when a must exists * re #7 add special handling for _exists_ and _missing_ fields * full text search on some more commonly used field names * full text search on some more commonly used field names * nested queries * re #7 nested queries for nested fields.
1 parent 75eee7e commit 654bb28

File tree

13 files changed

+313
-124
lines changed

13 files changed

+313
-124
lines changed

CHANGELOG-0.x.md

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

44

5+
## v0.2.0
6+
__BREAKING CHANGES__
7+
8+
* issue #7: Update ElasticaQueryBuilder to use 2.x queries/filters. Requires `"ruflin/elastica": "~3.2"`.
9+
* issue #6: Make TimeZone configurable on any builders that use date nodes.
10+
* The `Number` class was renamed to `Numbr` to prevent issue with scalar type hints in php7.
11+
12+
513
## v0.1.2
614
* Allow for `gdbots/common` ~0.1 or ~1.0.
715

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"require-dev": {
1212
"phpunit/phpunit": "~4.3",
13-
"ruflin/elastica": "~1.4.3"
13+
"ruflin/elastica": "~3.2"
1414
},
1515
"autoload": {
1616
"psr-4": {
@@ -24,7 +24,7 @@
2424
},
2525
"extra": {
2626
"branch-alias": {
27-
"dev-master": "0.1.x-dev"
27+
"dev-master": "0.2.x-dev"
2828
}
2929
}
3030
}

examples/elastica.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function log($level, $message, array $context = array())
3939
->setEmoticonFieldName('emoticons')
4040
->setHashtagFieldName('hashtags')
4141
->setMentionFieldName('mentions')
42+
->setLocalTimeZone(new DateTimeZone('America/Los_Angeles'))
4243
;
4344

4445
$qs = isset($argv[1]) ? $argv[1] : 'test';
@@ -51,7 +52,7 @@ public function log($level, $message, array $context = array())
5152
$builder->addParsedQuery($parsedQuery);
5253

5354
$options = [Search::OPTION_FROM => 0, Search::OPTION_SIZE => 5];
54-
$query = $builder->getFilteredQuery();
55+
$query = $builder->getBoolQuery();
5556
/*
5657
$query = (new FunctionScore())
5758
->setQuery($query)

src/Builder/AbstractQueryBuilder.php

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Gdbots\QueryParser\Node\Hashtag;
1010
use Gdbots\QueryParser\Node\Mention;
1111
use Gdbots\QueryParser\Node\Node;
12-
use Gdbots\QueryParser\Node\Number;
12+
use Gdbots\QueryParser\Node\Numbr;
1313
use Gdbots\QueryParser\Node\Phrase;
1414
use Gdbots\QueryParser\Node\Range;
1515
use Gdbots\QueryParser\Node\Subquery;
@@ -18,29 +18,6 @@
1818
use Gdbots\QueryParser\Node\WordRange;
1919
use Gdbots\QueryParser\ParsedQuery;
2020

21-
/*
22-
* DEV NOTES...
23-
*
24-
* When is it a term? (exact value)
25-
* - if it's a Date, Emoji, Emoticon, Hashtag, Mention, Number, Url
26-
* - if it's in a field and the field does not support full text search. i.e. "+status:active"
27-
*
28-
*
29-
* When should "filter" be used?
30-
* - when a "term" is required or prohibited. and is "inField"
31-
*
32-
*
33-
* When should "should match" be used?
34-
* - when a word or phrase is not required or prohibited.
35-
* - when a word that is required is a stop word (possible/likely it's not even in the index).
36-
* - when a word that is required uses fuzzy or trailing wildcard
37-
*
38-
*
39-
* When should "should match term" be used?
40-
* - when a term is not in a field node and is not required.
41-
*
42-
*
43-
*/
4421
abstract class AbstractQueryBuilder implements QueryBuilder
4522
{
4623
/** @var Field */
@@ -73,16 +50,54 @@ abstract class AbstractQueryBuilder implements QueryBuilder
7350
'description' => true,
7451
'overview' => true,
7552
'summary' => true,
53+
'story' => true,
54+
'html' => true,
55+
'text' => true,
56+
'markdown' => true,
57+
'content' => true,
58+
'contents' => true,
59+
'contents-continued' => true,
60+
'contents-md' => true,
61+
'contents-mobile' => true,
62+
'mobile-contents' => true,
63+
'txt-contents' => true,
64+
'text-contents' => true,
7665
'abstract' => true,
7766
'search_text' => true,
67+
'cover' => true,
7868
'bio' => true,
7969
'mini_bio' => true,
70+
'meta_title' => true,
71+
'meta_description' => true,
72+
'meta_keywords' => true,
73+
'og_title' => true,
74+
'og_description' => true,
75+
'og_keywords' => true,
8076
'seo_title' => true,
77+
'seo_description' => true,
8178
'seo_keywords' => true,
8279
'img_credit' => true,
8380
'img_caption' => true,
8481
'credit' => true,
8582
'caption' => true,
83+
'img_credits' => true,
84+
'img_captions' => true,
85+
'image_credits' => true,
86+
'image_captions' => true,
87+
'credits' => true,
88+
'captions' => true,
89+
'full_name' => true,
90+
'first_name' => true,
91+
'last_name' => true,
92+
'street1' => true,
93+
'street2' => true,
94+
'city' => true,
95+
'address.street1' => true,
96+
'address.street2' => true,
97+
'address.city' => true,
98+
'ctx_ip_geo.street1' => true,
99+
'ctx_ip_geo.street2' => true,
100+
'ctx_ip_geo.city' => true,
86101
];
87102

88103
/** @var string */
@@ -100,6 +115,9 @@ abstract class AbstractQueryBuilder implements QueryBuilder
100115
/** @var string */
101116
protected $mentionFieldName;
102117

118+
/** @var \DateTimeZone */
119+
protected $localTimeZone;
120+
103121
/**
104122
* @return static
105123
*/
@@ -118,6 +136,26 @@ final public function setFullTextSearchFields(array $fields)
118136
return $this;
119137
}
120138

139+
/**
140+
* @param string $fieldName
141+
* @return static
142+
*/
143+
final public function addFullTextSearchField($fieldName)
144+
{
145+
$this->fullTextSearchFields[$fieldName] = true;
146+
return $this;
147+
}
148+
149+
/**
150+
* @param string $fieldName
151+
* @return static
152+
*/
153+
final public function removeFullTextSearchField($fieldName)
154+
{
155+
unset($this->fullTextSearchFields[$fieldName]);
156+
return $this;
157+
}
158+
121159
/**
122160
* @return array
123161
*/
@@ -185,6 +223,16 @@ final public function setMentionFieldName($fieldName)
185223
return $this;
186224
}
187225

226+
/**
227+
* @param \DateTimeZone $timeZone
228+
* @return static
229+
*/
230+
final public function setLocalTimeZone(\DateTimeZone $timeZone)
231+
{
232+
$this->localTimeZone = $timeZone;
233+
return $this;
234+
}
235+
188236
/**
189237
* @param ParsedQuery $parsedQuery
190238
* @return static
@@ -321,10 +369,10 @@ final public function addMention(Mention $mention)
321369
}
322370

323371
/**
324-
* @param \Gdbots\QueryParser\Node\Number $number
372+
* @param Numbr $number
325373
* @return static
326374
*/
327-
final public function addNumber(Number $number)
375+
final public function addNumber(Numbr $number)
328376
{
329377
$this->handleTerm($number);
330378
return $this;

0 commit comments

Comments
 (0)