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
429d2d9c
Commit
429d2d9c
authored
Dec 23, 2016
by
Bas de Nooijer
Committed by
GitHub
Dec 23, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #468 from chadicus/bug/group-format-simple
Enable Simple Group Format
parents
71bf3f5a
c975aad9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
62 deletions
+160
-62
library/Solarium/QueryType/Select/ResponseParser/Component/Grouping.php
...um/QueryType/Select/ResponseParser/Component/Grouping.php
+87
-62
tests/Solarium/Tests/QueryType/Select/ResponseParser/Component/GroupingTest.php
...ueryType/Select/ResponseParser/Component/GroupingTest.php
+73
-0
No files found.
library/Solarium/QueryType/Select/ResponseParser/Component/Grouping.php
View file @
429d2d9c
...
...
@@ -61,49 +61,39 @@ class Grouping implements ComponentParserInterface
*/
public
function
parse
(
$query
,
$grouping
,
$data
)
{
if
(
!
isset
(
$data
[
'grouped'
]))
{
return
new
Result
(
array
());
}
$groups
=
array
();
if
(
isset
(
$data
[
'grouped'
]))
{
// parse field groups
$valueResultClass
=
$grouping
->
getOption
(
'resultvaluegroupclass'
);
$documentClass
=
$query
->
getOption
(
'documentclass'
);
// check grouping fields as well as the grouping function (either can be used in the query)
foreach
(
array_merge
(
$grouping
->
getFields
(),
array
(
$grouping
->
getFunction
()))
as
$field
)
{
if
(
isset
(
$data
[
'grouped'
][
$field
]))
{
if
(
!
isset
(
$data
[
'grouped'
][
$field
]))
{
continue
;
}
$result
=
$data
[
'grouped'
][
$field
];
$matches
=
(
isset
(
$result
[
'matches'
]))
?
$result
[
'matches'
]
:
null
;
$groupCount
=
(
isset
(
$result
[
'ngroups'
]))
?
$result
[
'ngroups'
]
:
null
;
$valueGroups
=
array
();
foreach
(
$result
[
'groups'
]
as
$valueGroupResult
)
{
$value
=
(
isset
(
$valueGroupResult
[
'groupValue'
]))
?
$valueGroupResult
[
'groupValue'
]
:
null
;
$numFound
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'numFound'
]))
?
$valueGroupResult
[
'doclist'
][
'numFound'
]
:
null
;
$start
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'start'
]))
?
$valueGroupResult
[
'doclist'
][
'start'
]
:
null
;
$maxScore
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'maxScore'
]))
?
$valueGroupResult
[
'doclist'
][
'maxScore'
]
:
null
;
// create document instances
$documents
=
array
();
if
(
isset
(
$valueGroupResult
[
'doclist'
][
'docs'
])
&&
is_array
(
$valueGroupResult
[
'doclist'
][
'docs'
]))
{
foreach
(
$valueGroupResult
[
'doclist'
][
'docs'
]
as
$doc
)
{
$documents
[]
=
new
$documentClass
(
$doc
);
}
if
(
$grouping
->
getFormat
()
===
GroupingComponent
::
FORMAT_SIMPLE
)
{
$valueGroups
=
[
$this
->
extractValueGroup
(
$valueResultClass
,
$documentClass
,
$result
,
$query
)];
$groups
[
$field
]
=
new
FieldGroup
(
$matches
,
$groupCount
,
$valueGroups
);
continue
;
}
$valueGroups
[]
=
new
$valueResultClass
(
$value
,
$numFound
,
$start
,
$documents
,
$maxScore
,
$query
);
$valueGroups
=
array
();
foreach
(
$result
[
'groups'
]
as
$valueGroupResult
)
{
$valueGroups
[]
=
$this
->
extractValueGroup
(
$valueResultClass
,
$documentClass
,
$valueGroupResult
,
$query
);
}
$groups
[
$field
]
=
new
FieldGroup
(
$matches
,
$groupCount
,
$valueGroups
);
}
}
// parse query groups
$groupResultClass
=
$grouping
->
getOption
(
'resultquerygroupclass'
);
...
...
@@ -131,8 +121,43 @@ class Grouping implements ComponentParserInterface
$groups
[
$groupQuery
]
=
$group
;
}
}
}
return
new
Result
(
$groups
);
}
/**
* Helper method to extract a ValueGroup object from the given value group result array.
*
* @param string $valueResultClass The grouping resultvaluegroupclass option.
* @param string $documentClass The name of the solr document class to use.
* @param array $valueGroupResult The group result from the solr response.
* @param Query $query The current solr query.
*
* @return object
*/
private
function
extractValueGroup
(
$valueResultClass
,
$documentClass
,
$valueGroupResult
,
$query
)
{
$value
=
(
isset
(
$valueGroupResult
[
'groupValue'
]))
?
$valueGroupResult
[
'groupValue'
]
:
null
;
$numFound
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'numFound'
]))
?
$valueGroupResult
[
'doclist'
][
'numFound'
]
:
null
;
$start
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'start'
]))
?
$valueGroupResult
[
'doclist'
][
'start'
]
:
null
;
$maxScore
=
(
isset
(
$valueGroupResult
[
'doclist'
][
'maxScore'
]))
?
$valueGroupResult
[
'doclist'
][
'maxScore'
]
:
null
;
// create document instances
$documents
=
array
();
if
(
isset
(
$valueGroupResult
[
'doclist'
][
'docs'
])
&&
is_array
(
$valueGroupResult
[
'doclist'
][
'docs'
]))
{
foreach
(
$valueGroupResult
[
'doclist'
][
'docs'
]
as
$doc
)
{
$documents
[]
=
new
$documentClass
(
$doc
);
}
}
return
new
$valueResultClass
(
$value
,
$numFound
,
$start
,
$documents
,
$maxScore
,
$query
);
}
}
tests/Solarium/Tests/QueryType/Select/ResponseParser/Component/GroupingTest.php
View file @
429d2d9c
...
...
@@ -161,6 +161,43 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
array
(),
$result
->
getGroups
());
}
public
function
testParseMissingGroupField
()
{
//data does not contain 'fieldA'
$data
=
array
(
'grouped'
=>
array
(
'functionF'
=>
array
(
'matches'
=>
8
,
'ngroups'
=>
3
,
'groups'
=>
array
(
array
(
'groupValue'
=>
true
,
'doclist'
=>
array
(
'numFound'
=>
5
,
'docs'
=>
array
(
array
(
'id'
=>
3
,
'name'
=>
'fun'
),
),
),
),
),
),
'cat:1'
=>
array
(
'matches'
=>
40
,
'doclist'
=>
array
(
'numFound'
=>
22
,
'docs'
=>
array
(
array
(
'id'
=>
2
,
'name'
=>
'dummy2'
),
array
(
'id'
=>
5
,
'name'
=>
'dummy5'
),
),
),
),
),
);
$result
=
$this
->
parser
->
parse
(
$this
->
query
,
$this
->
grouping
,
$data
);
$this
->
assertNull
(
$result
->
getGroup
(
'fieldA'
));
}
public
function
testFunctionGroupParsing
()
{
$fieldGroup
=
$this
->
result
->
getGroup
(
'functionF'
);
...
...
@@ -176,4 +213,40 @@ class GroupingTest extends \PHPUnit_Framework_TestCase
$docs
=
$valueGroup
->
getDocuments
();
$this
->
assertEquals
(
'fun'
,
$docs
[
0
]
->
name
);
}
public
function
testsParseWithSimpleFormat
()
{
$data
=
array
(
'grouped'
=>
array
(
'fieldA'
=>
array
(
'matches'
=>
25
,
'ngroups'
=>
12
,
'doclist'
=>
array
(
'numFound'
=>
13
,
'docs'
=>
array
(
array
(
'id'
=>
1
,
'name'
=>
'test'
),
array
(
'id'
=>
2
,
'name'
=>
'test2'
),
),
),
),
),
);
$this
->
grouping
->
setFormat
(
Component
::
FORMAT_SIMPLE
);
$result
=
$this
->
parser
->
parse
(
$this
->
query
,
$this
->
grouping
,
$data
);
$fieldGroup
=
$result
->
getGroup
(
'fieldA'
);
$valueGroups
=
$fieldGroup
->
getValueGroups
();
$this
->
assertEquals
(
25
,
$fieldGroup
->
getMatches
());
$this
->
assertEquals
(
12
,
$fieldGroup
->
getNumberOfGroups
());
$this
->
assertEquals
(
1
,
count
(
$valueGroups
));
$valueGroup
=
$valueGroups
[
0
];
$this
->
assertEquals
(
13
,
$valueGroup
->
getNumFound
());
$docs
=
$valueGroup
->
getDocuments
();
$this
->
assertEquals
(
'test2'
,
$docs
[
1
]
->
name
);
}
}
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