Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
f59fa30e
Commit
f59fa30e
authored
Mar 17, 2015
by
wickedOne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added stats to pivots
parent
e993186e
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
385 additions
and
5 deletions
+385
-5
.gitignore
.gitignore
+1
-1
library/Solarium/QueryType/Select/Query/Component/Facet/Pivot.php
...Solarium/QueryType/Select/Query/Component/Facet/Pivot.php
+104
-2
library/Solarium/QueryType/Select/Query/Component/Stats/Field.php
...Solarium/QueryType/Select/Query/Component/Stats/Field.php
+98
-0
library/Solarium/QueryType/Select/RequestBuilder/Component/FacetSet.php
...um/QueryType/Select/RequestBuilder/Component/FacetSet.php
+12
-1
library/Solarium/QueryType/Select/RequestBuilder/Component/Stats.php
...arium/QueryType/Select/RequestBuilder/Component/Stats.php
+3
-1
library/Solarium/QueryType/Select/Result/Facet/Pivot/PivotItem.php
...olarium/QueryType/Select/Result/Facet/Pivot/PivotItem.php
+23
-0
tests/Solarium/Tests/QueryType/Select/Query/Component/Facet/PivotTest.php
...ests/QueryType/Select/Query/Component/Facet/PivotTest.php
+54
-0
tests/Solarium/Tests/QueryType/Select/Query/Component/Facet/RangeTest.php
...ests/QueryType/Select/Query/Component/Facet/RangeTest.php
+7
-0
tests/Solarium/Tests/QueryType/Select/Query/Component/Stats/FieldTest.php
...ests/QueryType/Select/Query/Component/Stats/FieldTest.php
+48
-0
tests/Solarium/Tests/QueryType/Select/RequestBuilder/Component/FacetSetTest.php
...ueryType/Select/RequestBuilder/Component/FacetSetTest.php
+24
-0
tests/Solarium/Tests/QueryType/Select/ResponseParser/Component/FacetSetTest.php
...ueryType/Select/ResponseParser/Component/FacetSetTest.php
+11
-0
No files found.
.gitignore
View file @
f59fa30e
...
...
@@ -2,4 +2,4 @@ build
phpunit.xml
composer.phar
composer.lock
vendor
\ No newline at end of file
vendor
library/Solarium/QueryType/Select/Query/Component/Facet/Pivot.php
View file @
f59fa30e
...
...
@@ -54,6 +54,13 @@ class Pivot extends Facet
*/
protected
$fields
=
array
();
/**
* Optional stats
*
* @var array
*/
protected
$stats
=
array
();
/**
* Initialize options
*
...
...
@@ -61,8 +68,15 @@ class Pivot extends Facet
*/
protected
function
init
()
{
if
(
isset
(
$this
->
options
[
'fields'
]))
{
$this
->
addFields
(
$this
->
options
[
'fields'
]);
foreach
(
$this
->
options
as
$name
=>
$value
)
{
switch
(
$name
)
{
case
'fields'
:
$this
->
addFields
(
$value
);
break
;
case
'stats'
:
$this
->
setStats
(
$value
);
break
;
}
}
}
...
...
@@ -184,4 +198,92 @@ class Pivot extends Facet
return
$this
;
}
/**
* Add stat
*
* @param string $stat
* @return self Provides fluent interface
*/
public
function
addStat
(
$stat
)
{
$this
->
stats
[
$stat
]
=
true
;
return
$this
;
}
/**
* Specify multiple Stats
*
* @param string|array $stats can be an array or string with comma
* separated statnames
*
* @return self Provides fluent interface
*/
public
function
addStats
(
$stats
)
{
if
(
is_string
(
$stats
))
{
$stats
=
explode
(
','
,
$stats
);
$stats
=
array_map
(
'trim'
,
$stats
);
}
foreach
(
$stats
as
$stat
)
{
$this
->
addStat
(
$stat
);
}
return
$this
;
}
/**
* Remove a stat from the stats list
*
* @param string $stat
* @return self Provides fluent interface
*/
public
function
removeStat
(
$stat
)
{
if
(
isset
(
$this
->
stats
[
$stat
]))
{
unset
(
$this
->
stats
[
$stat
]);
}
return
$this
;
}
/**
* Remove all stats from the stats list.
*
* @return self Provides fluent interface
*/
public
function
clearStats
()
{
$this
->
stats
=
array
();
return
$this
;
}
/**
* Get the list of stats
*
* @return array
*/
public
function
getStats
()
{
return
array_keys
(
$this
->
stats
);
}
/**
* Set multiple stats
*
* This overwrites any existing stats
*
* @param array $stats
* @return self Provides fluent interface
*/
public
function
setStats
(
$stats
)
{
$this
->
clearStats
();
$this
->
addStats
(
$stats
);
return
$this
;
}
}
library/Solarium/QueryType/Select/Query/Component/Stats/Field.php
View file @
f59fa30e
...
...
@@ -52,6 +52,13 @@ class Field extends Configurable
*/
protected
$facets
=
array
();
/**
* pivot facets for these stats
*
* @var array
*/
protected
$pivots
=
array
();
/**
* Initialize options
*
...
...
@@ -67,6 +74,9 @@ class Field extends Configurable
case
'facet'
:
$this
->
setFacets
(
$value
);
break
;
case
'pivot'
:
$this
->
setPivots
(
$value
);
break
;
}
}
}
...
...
@@ -179,4 +189,92 @@ class Field extends Configurable
return
$this
;
}
/**
* Add pivot
*
* @param string $pivot
* @return self Provides fluent interface
*/
public
function
addPivot
(
$pivot
)
{
$this
->
pivots
[
$pivot
]
=
true
;
return
$this
;
}
/**
* Specify multiple Pivots
*
* @param string|array $pivots can be an array or string with comma
* separated facetnames
*
* @return self Provides fluent interface
*/
public
function
addPivots
(
$pivots
)
{
if
(
is_string
(
$pivots
))
{
$pivots
=
explode
(
','
,
$pivots
);
$pivots
=
array_map
(
'trim'
,
$pivots
);
}
foreach
(
$pivots
as
$facet
)
{
$this
->
addPivot
(
$facet
);
}
return
$this
;
}
/**
* Remove a pivot facet from the pivot list
*
* @param string $pivot
* @return self Provides fluent interface
*/
public
function
removePivot
(
$pivot
)
{
if
(
isset
(
$this
->
pivots
[
$pivot
]))
{
unset
(
$this
->
pivots
[
$pivot
]);
}
return
$this
;
}
/**
* Remove all pivot facets from the pivot list.
*
* @return self Provides fluent interface
*/
public
function
clearPivots
()
{
$this
->
pivots
=
array
();
return
$this
;
}
/**
* Get the list of pivot facets
*
* @return array
*/
public
function
getPivots
()
{
return
array_keys
(
$this
->
pivots
);
}
/**
* Set multiple pivot facets
*
* This overwrites any existing pivots
*
* @param array $pivots
* @return self Provides fluent interface
*/
public
function
setPivots
(
$pivots
)
{
$this
->
clearPivots
();
$this
->
addPivots
(
$pivots
);
return
$this
;
}
}
library/Solarium/QueryType/Select/RequestBuilder/Component/FacetSet.php
View file @
f59fa30e
...
...
@@ -205,11 +205,22 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac
*/
public
function
addFacetPivot
(
$request
,
$facet
)
{
$stats
=
$facet
->
getStats
();
if
(
count
(
$stats
)
>
0
)
{
$key
=
array
(
'stats'
=>
implode
(
''
,
$stats
));
// when specifying stats, solr sets the field as key
$facet
->
setKey
(
implode
(
','
,
$facet
->
getFields
()));
}
else
{
$key
=
array
(
'key'
=>
$facet
->
getKey
());
}
$request
->
addParam
(
'facet.pivot'
,
$this
->
renderLocalParams
(
implode
(
','
,
$facet
->
getFields
()),
array
(
'key'
=>
$facet
->
getKey
(),
'ex'
=>
$facet
->
getExcludes
(
))
array
_merge
(
$key
,
array
(
'ex'
=>
$facet
->
getExcludes
()
))
)
);
$request
->
addParam
(
'facet.pivot.mincount'
,
$facet
->
getMinCount
(),
true
);
...
...
library/Solarium/QueryType/Select/RequestBuilder/Component/Stats.php
View file @
f59fa30e
...
...
@@ -60,8 +60,10 @@ class Stats implements ComponentRequestBuilderInterface
// add fields
foreach
(
$component
->
getFields
()
as
$field
)
{
$pivots
=
$field
->
getPivots
();
$request
->
addParam
(
'stats.field'
,
$field
->
getKey
());
$prefix
=
(
count
(
$pivots
)
>
0
)
?
'{!tag='
.
implode
(
','
,
$pivots
)
.
'}'
:
''
;
$request
->
addParam
(
'stats.field'
,
$prefix
.
$field
->
getKey
());
// add field specific facet stats
foreach
(
$field
->
getFacets
()
as
$facet
)
{
...
...
library/Solarium/QueryType/Select/Result/Facet/Pivot/PivotItem.php
View file @
f59fa30e
...
...
@@ -38,6 +38,8 @@
*/
namespace
Solarium\QueryType\Select\Result\Facet\Pivot
;
use
Solarium\QueryType\Select\Result\Stats\Stats
;
/**
* Select field pivot result
*
...
...
@@ -65,6 +67,13 @@ class PivotItem extends Pivot
*/
protected
$count
;
/**
* Field stats
*
* @var mixed
*/
protected
$stats
;
/**
* Constructor
*
...
...
@@ -81,6 +90,10 @@ class PivotItem extends Pivot
$this
->
pivot
[]
=
new
PivotItem
(
$pivotData
);
}
}
if
(
isset
(
$data
[
'stats'
]))
{
$this
->
stats
=
new
Stats
(
$data
[
'stats'
]);
}
}
/**
...
...
@@ -112,4 +125,14 @@ class PivotItem extends Pivot
{
return
$this
->
count
;
}
/**
* Get stats
*
* @return Stats
*/
public
function
getStats
()
{
return
$this
->
stats
;
}
}
tests/Solarium/Tests/QueryType/Select/Query/Component/Facet/PivotTest.php
View file @
f59fa30e
...
...
@@ -67,6 +67,13 @@ class PivotTest extends \PHPUnit_Framework_TestCase
);
}
public
function
testSetMinCount
()
{
$this
->
facet
->
setMinCount
(
5
);
$this
->
assertEquals
(
5
,
$this
->
facet
->
getMinCount
());
}
public
function
testAddField
()
{
$expectedFields
=
$this
->
facet
->
getFields
();
...
...
@@ -113,4 +120,51 @@ class PivotTest extends \PHPUnit_Framework_TestCase
$this
->
facet
->
setFields
(
array
(
'field3'
,
'field4'
));
$this
->
assertEquals
(
array
(
'field3'
,
'field4'
),
$this
->
facet
->
getFields
());
}
public
function
testAddStat
()
{
$expectedStats
=
$this
->
facet
->
getStats
();
$expectedStats
[]
=
'newstat'
;
$this
->
facet
->
addStat
(
'newstat'
);
$this
->
assertEquals
(
$expectedStats
,
$this
->
facet
->
getStats
());
}
public
function
testClearStats
()
{
$this
->
facet
->
addStat
(
'newstat'
);
$this
->
facet
->
clearStats
();
$this
->
assertEquals
(
array
(),
$this
->
facet
->
getStats
());
}
public
function
testAddStats
()
{
$stats
=
array
(
'stat1'
,
'stat2'
);
$this
->
facet
->
clearStats
();
$this
->
facet
->
addStats
(
$stats
);
$this
->
assertEquals
(
$stats
,
$this
->
facet
->
getStats
());
}
public
function
testAddStatsAsStringWithTrim
()
{
$this
->
facet
->
clearStats
();
$this
->
facet
->
addStats
(
'stat1, stat2'
);
$this
->
assertEquals
(
array
(
'stat1'
,
'stat2'
),
$this
->
facet
->
getStats
());
}
public
function
testRemoveStat
()
{
$this
->
facet
->
clearStats
();
$this
->
facet
->
addStats
(
array
(
'stat1'
,
'stat2'
));
$this
->
facet
->
removeStat
(
'stat1'
);
$this
->
assertEquals
(
array
(
'stat2'
),
$this
->
facet
->
getstats
());
}
public
function
testSetStats
()
{
$this
->
facet
->
clearStats
();
$this
->
facet
->
addStats
(
array
(
'stat1'
,
'stat2'
));
$this
->
facet
->
setStats
(
array
(
'stat3'
,
'stat4'
));
$this
->
assertEquals
(
array
(
'stat3'
,
'stat4'
),
$this
->
facet
->
getStats
());
}
}
tests/Solarium/Tests/QueryType/Select/Query/Component/Facet/RangeTest.php
View file @
f59fa30e
...
...
@@ -82,6 +82,13 @@ class RangeTest extends \PHPUnit_Framework_TestCase
);
}
public
function
testSetMinCount
()
{
$this
->
facet
->
setMinCount
(
5
);
$this
->
assertEquals
(
5
,
$this
->
facet
->
getMinCount
());
}
public
function
testSetAndGetField
()
{
$this
->
facet
->
setField
(
'price'
);
...
...
tests/Solarium/Tests/QueryType/Select/Query/Component/Stats/FieldTest.php
View file @
f59fa30e
...
...
@@ -49,6 +49,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
{
$options
=
array
(
'facet'
=>
'field1, field2'
,
'pivot'
=>
'piv1'
);
$this
->
field
->
setOptions
(
$options
);
...
...
@@ -107,4 +108,51 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$this
->
field
->
setFacets
(
array
(
'facet3'
,
'facet4'
));
$this
->
assertEquals
(
array
(
'facet3'
,
'facet4'
),
$this
->
field
->
getFacets
());
}
public
function
testAddPivot
()
{
$expectedPivots
=
$this
->
field
->
getPivots
();
$expectedPivots
[]
=
'newpivot'
;
$this
->
field
->
addPivot
(
'newpivot'
);
$this
->
assertEquals
(
$expectedPivots
,
$this
->
field
->
getPivots
());
}
public
function
testClearPivots
()
{
$this
->
field
->
addPivot
(
'newpivot'
);
$this
->
field
->
clearPivots
();
$this
->
assertEquals
(
array
(),
$this
->
field
->
getPivots
());
}
public
function
testAddPivots
()
{
$pivots
=
array
(
'pivot1'
,
'pivot2'
);
$this
->
field
->
clearPivots
();
$this
->
field
->
addPivots
(
$pivots
);
$this
->
assertEquals
(
$pivots
,
$this
->
field
->
getPivots
());
}
public
function
testAddPivotsAsStringWithTrim
()
{
$this
->
field
->
clearPivots
();
$this
->
field
->
addPivots
(
'pivot1, pivot2'
);
$this
->
assertEquals
(
array
(
'pivot1'
,
'pivot2'
),
$this
->
field
->
getPivots
());
}
public
function
testRemovePivot
()
{
$this
->
field
->
clearPivots
();
$this
->
field
->
addPivots
(
array
(
'pivot1'
,
'pivot2'
));
$this
->
field
->
removePivot
(
'pivot1'
);
$this
->
assertEquals
(
array
(
'pivot2'
),
$this
->
field
->
getPivots
());
}
public
function
testSetPivots
()
{
$this
->
field
->
clearPivots
();
$this
->
field
->
addPivots
(
array
(
'pivot1'
,
'pivot2'
));
$this
->
field
->
setPivots
(
array
(
'pivot3'
,
'pivot4'
));
$this
->
assertEquals
(
array
(
'pivot3'
,
'pivot4'
),
$this
->
field
->
getPivots
());
}
}
tests/Solarium/Tests/QueryType/Select/RequestBuilder/Component/FacetSetTest.php
View file @
f59fa30e
...
...
@@ -208,6 +208,30 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
urldecode
(
$request
->
getUri
())
);
}
public
function
testBuildWithPivotStatFacet
()
{
$facet
=
new
FacetPivot
(
array
(
'key'
=>
'f1'
,
'fields'
=>
'cat,inStock'
,
'stats'
=>
'piv1'
)
);
$this
->
component
->
addFacet
(
$facet
);
$request
=
$this
->
builder
->
buildComponent
(
$this
->
component
,
$this
->
request
);
$this
->
assertEquals
(
null
,
$request
->
getRawData
()
);
$this
->
assertEquals
(
'?facet=true&facet.pivot={!stats=piv1}cat,inStock'
,
urldecode
(
$request
->
getUri
())
);
}
}
class
UnknownFacet
extends
FacetField
...
...
tests/Solarium/Tests/QueryType/Select/ResponseParser/Component/FacetSetTest.php
View file @
f59fa30e
...
...
@@ -201,6 +201,10 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
'pivot'
=>
array
(
array
(
'field'
=>
'price'
,
'value'
=>
1
,
'count'
=>
12
),
array
(
'field'
=>
'price'
,
'value'
=>
2
,
'count'
=>
8
),
),
'stats'
=>
array
(
'min'
=>
4
,
'max'
=>
6
,
)
)
),
...
...
@@ -262,6 +266,13 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
count
(
$facets
[
'cat,price'
])
);
$pivots
=
$facets
[
'cat,price'
]
->
getPivot
();
$this
->
assertEquals
(
2
,
count
(
$pivots
[
0
]
->
getStats
())
);
$this
->
query
=
new
Query
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment