Skip to content

Commit b268f35

Browse files
author
Štefan Peťovský
committed
allow mixing inclusive [] and exclusive {} endpoints in range expression
so expressions like “date:[NOW/DAY-1DAY TO NOW/DAY+1DAY}” are possible http://lucidworks.com/blog/date-math-now-and-filter-queries/
1 parent a4a5596 commit b268f35

File tree

8 files changed

+101
-45
lines changed

8 files changed

+101
-45
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: php
22
php:
3-
- 5.4
4-
- 5.5
53
- 5.6
64
- hhvm
75

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222
],
2323
"require": {
24+
"php": ">=5.6.0",
2425
"lstrojny/functional-php": "dev-master",
2526
"internations/solr-utils": "~0.4"
2627
},

docs/01-expression-builder-usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ TO 2012-12-31T23:59:56]`).
8585
$eb->field('publishedOn', $eb->dateRange(new DateTime('2012-01-01 00:00:00'), new DateTime('2012-12-31 23:59:59')));
8686
```
8787

88+
We can also mix inclusive “[]” and exclusive “{}” endpoints, so expressions like `date:[2015-08-27T15:35:04Z/DAY TO 2015-08-27T15:35:04Z/DAY+1DAY}` are possible (as of Solr 4.0, more information http://lucidworks.com/blog/date-math-now-and-filter-queries/)
89+
90+
```php
91+
$eb->field('date', $eb->dateRange(new DateTime('2015-08-27 15:35:04') . '/DAY', new DateTime('2015-08-27 15:35:04') . '/DAY+1DAY'), true, false);
92+
```
93+
8894
## Grouping
8995

9096
Grouping is a powerful feature of Lucene’s search syntax. Let’s search for a list of product names

src/InterNations/Component/Solr/Expression/ExpressionBuilder.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ public function fzz($expr, $similarity = null)
139139
*
140140
* @param string $start
141141
* @param string $end
142-
* @param boolean $inclusive
142+
* @param boolean $inclusiveFrom
143+
* @param boolean $inclusiveTo
143144
* @return ExpressionInterface
144145
*/
145-
public function range($start = null, $end = null, $inclusive = true)
146+
public function range($start = null, $end = null, $inclusiveFrom = true, $inclusiveTo = true)
146147
{
147-
return new RangeExpression($start, $end, $inclusive);
148+
return new RangeExpression($start, $end, $inclusiveFrom, $inclusiveTo);
148149
}
149150

150151
/**
@@ -156,7 +157,7 @@ public function range($start = null, $end = null, $inclusive = true)
156157
*/
157158
public function btwnRange($start = null, $end = null)
158159
{
159-
return new RangeExpression($start, $end, false);
160+
return new RangeExpression($start, $end, false, false);
160161
}
161162

162163
/**
@@ -403,12 +404,17 @@ public function date(DateTime $date = null, $timezone = false)
403404
* Create a range between two dates (one side may be unlimited which is indicated by passing null)
404405
*
405406
* @param DateTime $from
407+
* @param boolean $inclusiveFrom
406408
* @param DateTime $to
407-
* @param boolean $inclusive
409+
* @param boolean $inclusiveTo
408410
* @param boolean $timezone
409411
* @return ExpressionInterface|null
410412
*/
411-
public function dateRange(DateTime $from = null, DateTime $to = null, $inclusive = true, $timezone = false)
413+
public function dateRange(DateTime $from = null,
414+
DateTime $to = null,
415+
$inclusiveFrom = true,
416+
$inclusiveTo = true,
417+
$timezone = false)
412418
{
413419
if ($from === null && $to === null) {
414420
return null;
@@ -417,7 +423,8 @@ public function dateRange(DateTime $from = null, DateTime $to = null, $inclusive
417423
return $this->range(
418424
$this->lit($this->date($from, $timezone)),
419425
$this->lit($this->date($to, $timezone)),
420-
$inclusive
426+
$inclusiveFrom,
427+
$inclusiveTo
421428
);
422429
}
423430

src/InterNations/Component/Solr/Expression/RangeExpression.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Range expression class
88
*
9-
* Let you specify range queries in the like of field:[<start> TO <end>] or field:{<start> TO <end>}
9+
* Let you specify range queries in the like of field:[<start> TO <end>] or field:{<start> TO <end>} or also mixing inclusive/exclusive:[<start> TO <end>} as of Solr 4.0
1010
*/
1111
class RangeExpression extends Expression
1212
{
@@ -25,24 +25,33 @@ class RangeExpression extends Expression
2525
protected $end;
2626

2727
/**
28-
* Inclusive or exclusive the range start/end?
28+
* Inclusive or exclusive the range start?
2929
*
3030
* @var boolean
3131
*/
32-
protected $inclusive;
32+
protected $inclusiveFrom;
33+
34+
/**
35+
* Inclusive or exclusive the range end?
36+
*
37+
* @var boolean
38+
*/
39+
protected $inclusiveTo;
3340

3441
/**
3542
* Create new range query object
3643
*
3744
* @param string|integer|Expression $start
3845
* @param string|integer|Expression $end
39-
* @param boolean $inclusive
46+
* @param boolean $inclusiveFrom
47+
* @param boolean $inclusiveTo
4048
*/
41-
public function __construct($start = null, $end = null, $inclusive = true)
49+
public function __construct($start = null, $end = null, $inclusiveFrom = true, $inclusiveTo = true)
4250
{
4351
$this->start = $start;
4452
$this->end = $end;
45-
$this->inclusive = (bool) $inclusive;
53+
$this->inclusiveFrom = (bool) $inclusiveFrom;
54+
$this->inclusiveTo = (bool) $inclusiveTo;
4655
}
4756

4857
/**
@@ -52,10 +61,10 @@ public function __toString()
5261
{
5362
return sprintf(
5463
'%s%s TO %s%s',
55-
$this->inclusive ? '[' : '{',
64+
$this->inclusiveFrom ? '[' : '{',
5665
$this->cast($this->start),
5766
$this->cast($this->end),
58-
$this->inclusive ? ']' : '}'
67+
$this->inclusiveTo ? ']' : '}'
5968
);
6069
}
6170

tests/InterNations/Component/Solr/Tests/Expression/ExpressionBuilderTest.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -426,27 +426,30 @@ public static function getDateRangeData()
426426
'Europe/Berlin'
427427
),
428428
array(
429-
'[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]',
429+
'{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]',
430430
'2010-10-11 02:00:00',
431431
'Europe/Berlin',
432432
'2010-10-22 01:59:59',
433433
'Europe/Berlin',
434-
true
434+
false
435435
),
436436
array(
437-
'[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]',
437+
'{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}',
438438
'2010-10-11 02:00:00',
439439
'Europe/Berlin',
440440
'2010-10-22 01:59:59',
441-
'Europe/Berlin'
441+
'Europe/Berlin',
442+
false,
443+
false
442444
),
443445
array(
444-
'[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z]',
446+
'[2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}',
445447
'2010-10-11 02:00:00',
446448
'Europe/Berlin',
447449
'2010-10-22 01:59:59',
448450
'Europe/Berlin',
449-
true
451+
true,
452+
false
450453
),
451454
array(
452455
'[* TO 2010-10-21T23:59:59Z]',
@@ -456,18 +459,30 @@ public static function getDateRangeData()
456459
'Europe/Berlin'
457460
),
458461
array(
459-
'[2010-10-11T00:00:00Z TO *]',
462+
'{* TO 2010-10-21T23:59:59Z}',
463+
null,
464+
null,
465+
'2010-10-22 01:59:59',
466+
'Europe/Berlin',
467+
false,
468+
false
469+
),
470+
array(
471+
'{2010-10-11T00:00:00Z TO *]',
460472
'2010-10-11 02:00:00',
461473
'Europe/Berlin',
462474
null,
463-
null
475+
null,
476+
false,
477+
true
464478
),
465479
array(
466480
'{2010-10-11T00:00:00Z TO 2010-10-21T23:59:59Z}',
467481
'2010-10-11 02:00:00',
468482
'Europe/Berlin',
469483
'2010-10-22 01:59:59',
470484
'Europe/Berlin',
485+
false,
471486
false
472487
),
473488
array(
@@ -478,12 +493,13 @@ public static function getDateRangeData()
478493
null
479494
),
480495
array(
481-
'[2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z]',
496+
'{2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z}',
482497
'2010-10-11 02:00:00',
483498
'Europe/Berlin',
484499
'2010-10-22 01:59:59',
485500
'Europe/Berlin',
486-
null,
501+
false,
502+
false,
487503
'null',
488504
'Europe/Moscow',
489505
),
@@ -494,16 +510,18 @@ public static function getDateRangeData()
494510
'2010-10-22 01:59:59',
495511
'Europe/Berlin',
496512
true,
513+
true,
497514
'null',
498515
'Europe/Moscow',
499516
),
500517
array(
501-
'[2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z]',
518+
'{2010-10-11T04:00:00Z TO 2010-10-22T03:59:59Z}',
502519
'2010-10-11 02:00:00',
503520
'Europe/Berlin',
504521
'2010-10-22 01:59:59',
505522
'Europe/Berlin',
506-
null,
523+
false,
524+
false,
507525
'null',
508526
'Europe/Moscow',
509527
),
@@ -514,26 +532,29 @@ public static function getDateRangeData()
514532
'2010-10-22 01:59:59',
515533
'Europe/Berlin',
516534
true,
535+
true,
517536
'null',
518537
'Europe/Moscow',
519538
),
520539
array(
521-
'[* TO 2010-10-22T03:59:59Z]',
540+
'{* TO 2010-10-22T03:59:59Z}',
522541
null,
523542
null,
524543
'2010-10-22 01:59:59',
525544
'Europe/Berlin',
526-
null,
545+
false,
546+
false,
527547
'null',
528548
'Europe/Moscow'
529549
),
530550
array(
531-
'[2010-10-11T04:00:00Z TO *]',
551+
'{2010-10-11T04:00:00Z TO *}',
532552
'2010-10-11 02:00:00',
533553
'Europe/Berlin',
534554
null,
535555
null,
536-
null,
556+
false,
557+
false,
537558
'null',
538559
'Europe/Moscow',
539560
),
@@ -544,6 +565,7 @@ public static function getDateRangeData()
544565
'2010-10-22 01:59:59',
545566
'Europe/Berlin',
546567
false,
568+
false,
547569
'null',
548570
'Europe/Moscow',
549571
),
@@ -554,6 +576,7 @@ public static function getDateRangeData()
554576
'2010-10-22 01:59:59',
555577
'Europe/Berlin',
556578
false,
579+
false,
557580
'Europe/Berlin',
558581
'Europe/Moscow',
559582
),
@@ -567,7 +590,8 @@ public function testDateRange(
567590
$fromTimezone,
568591
$to,
569592
$toTimezone,
570-
$inclusive = null,
593+
$inclusiveFrom = null,
594+
$inclusiveTo = null,
571595
$solrTimezone = 'null',
572596
$defaultTimezone = null
573597
)
@@ -583,11 +607,13 @@ public function testDateRange(
583607
}
584608

585609
$arguments = array($from, $to);
586-
if ($inclusive !== null) {
587-
$arguments[] = $inclusive;
588-
589-
if ($solrTimezone !== 'null') {
590-
$arguments[] = $solrTimezone;
610+
if ($inclusiveFrom !== null) {
611+
$arguments[] = $inclusiveFrom;
612+
if ($inclusiveTo !== null) {
613+
$arguments[] = $inclusiveTo;
614+
if ($solrTimezone !== 'null') {
615+
$arguments[] = $solrTimezone;
616+
}
591617
}
592618
}
593619

@@ -598,8 +624,10 @@ public function testDateRange(
598624
public function testRange()
599625
{
600626
$this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z'));
601-
$this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z', true));
602-
$this->assertSame('{"A" TO "Z"}', (string) $this->eb->range('A', 'Z', false));
627+
$this->assertSame('["A" TO "Z"]', (string) $this->eb->range('A', 'Z', true, true));
628+
$this->assertSame('{"A" TO "Z"}', (string) $this->eb->range('A', 'Z', false, false));
629+
$this->assertSame('{"A" TO "Z"]', (string) $this->eb->range('A', 'Z', false, true));
630+
$this->assertSame('["A" TO "Z"}', (string) $this->eb->range('A', 'Z', true, false));
603631
}
604632

605633
public function testFunc()

tests/InterNations/Component/Solr/Tests/Expression/ExpressionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ public function testProximityExpression()
111111

112112
public function testRangeExpression()
113113
{
114-
$this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', true));
114+
$this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', true, true));
115+
$this->assertSame('{"foo" TO "bar"}', (string) new RangeExpression('foo', 'bar', false, false));
116+
$this->assertSame('{"foo" TO "bar"]', (string) new RangeExpression('foo', 'bar', false, true));
115117
$this->assertSame('["foo" TO "bar"]', (string) new RangeExpression('foo', 'bar'));
116118
$this->assertSame('["foo" TO "foo bar"]', (string) new RangeExpression('foo', new PhraseExpression('foo bar')));
117119
$this->assertSame('{"foo" TO "foo bar"}', (string) new RangeExpression('foo', new PhraseExpression('foo bar'), null, false));
118120
$this->assertSame(
119121
'{"foo" TO "foo bar?"}',
120-
(string) new RangeExpression('foo', new WildcardExpression('?', new PhraseExpression('foo bar')), false)
122+
(string) new RangeExpression('foo', new WildcardExpression('?', new PhraseExpression('foo bar')), false, false)
121123
);
122124
$this->assertSame('[-1 TO 0]', (string) new RangeExpression(-1, 0));
123125
$this->assertSame('[0 TO 1]', (string) new RangeExpression(0, 1));

tests/InterNations/Component/Solr/Tests/Query/QueryStringTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ public function testQueryWithPlaceholder_Boolean()
7272
public function testQueryWithPlaceholder_Expression()
7373
{
7474
$query = new QueryString('field:<ph>');
75-
$query->setPlaceholder('ph', new RangeExpression(0, 100, false));
76-
75+
$query->setPlaceholder('ph', new RangeExpression(0, 100, false, false));
7776
$this->assertSame('field:{0 TO 100}', (string) $query);
77+
$query = new QueryString('field:<ph>');
78+
$query->setPlaceholder('ph', new RangeExpression(0, 100, false, true));
79+
$this->assertSame('field:{0 TO 100]', (string) $query);
80+
$query = new QueryString('field:<ph>');
81+
$query->setPlaceholder('ph', new RangeExpression(0, 100, true, false));
82+
$this->assertSame('field:[0 TO 100}', (string) $query);
7883
}
7984

8085
public function testSetPlaceholders()

0 commit comments

Comments
 (0)