Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
facets
Commits
888290d8
Commit
888290d8
authored
Sep 29, 2017
by
ekes
Committed by
borisson_
Sep 29, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2872404
by borisson_, ekes: Use native facet range queries with solr
parent
2ed48251
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
19 deletions
+65
-19
src/Plugin/facets/facet_source/SearchApiDisplay.php
src/Plugin/facets/facet_source/SearchApiDisplay.php
+1
-1
src/QueryType/QueryTypeRangeBase.php
src/QueryType/QueryTypeRangeBase.php
+38
-14
tests/src/Kernel/Plugin/query_type/SearchApiDateTest.php
tests/src/Kernel/Plugin/query_type/SearchApiDateTest.php
+13
-2
tests/src/Unit/Plugin/query_type/SearchApiGranularTest.php
tests/src/Unit/Plugin/query_type/SearchApiGranularTest.php
+13
-2
No files found.
src/Plugin/facets/facet_source/SearchApiDisplay.php
View file @
888290d8
...
...
@@ -183,7 +183,7 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
// query type.
foreach
(
$facets
as
$facet
)
{
$configuration
=
[
'query'
=>
NULL
,
'query'
=>
$results
->
getQuery
()
,
'facet'
=>
$facet
,
'results'
=>
isset
(
$facet_results
[
$facet
->
getFieldIdentifier
()])
?
$facet_results
[
$facet
->
getFieldIdentifier
()]
:
[],
];
...
...
src/QueryType/QueryTypeRangeBase.php
View file @
888290d8
...
...
@@ -68,27 +68,51 @@ abstract class QueryTypeRangeBase extends QueryTypePluginBase {
public
function
build
()
{
$query_operator
=
$this
->
facet
->
getQueryOperator
();
// Go through the results and add facet results grouped by filters
// defined by self::calculateResultFilter().
if
(
!
empty
(
$this
->
results
))
{
// If there were no results or no query object, we can't do anything.
if
(
empty
(
$this
->
results
))
{
return
$this
->
facet
;
}
$supportedFeatures
=
array_flip
(
$this
->
query
->
getIndex
()
->
getServerInstance
()
->
getBackend
()
->
getSupportedFeatures
());
// Range grouping is supported.
if
(
isset
(
$supportedFeatures
[
'search_api_granular'
]))
{
$facet_results
=
[];
foreach
(
$this
->
results
as
$result
)
{
if
(
$result
[
'count'
]
||
$query_operator
==
'or'
)
{
$count
=
$result
[
'count'
];
$result_filter
=
$this
->
calculateResultFilter
(
trim
(
$result
[
'filter'
],
'"'
));
if
(
isset
(
$facet_results
[
$result_filter
[
'raw'
]]))
{
$facet_results
[
$result_filter
[
'raw'
]]
->
setCount
(
$facet_results
[
$result_filter
[
'raw'
]]
->
getCount
()
+
$count
);
}
else
{
$facet_results
[
$result_filter
[
'raw'
]]
=
new
Result
(
$this
->
facet
,
$result_filter
[
'raw'
],
$result_filter
[
'display'
],
$count
);
}
$result_filter
=
trim
(
$result
[
'filter'
],
'"'
);
$facet_results
[]
=
new
Result
(
$this
->
facet
,
$result_filter
,
$result_filter
,
$result
[
'count'
]);
}
}
$this
->
facet
->
setResults
(
$facet_results
);
return
$this
->
facet
;
}
// Non supported backend range grouping.
$facet_results
=
[];
foreach
(
$this
->
results
as
$result
)
{
// Go through the results and add facet results grouped by filters
// defined by self::calculateResultFilter().
if
(
$result
[
'count'
]
||
$query_operator
==
'or'
)
{
$count
=
$result
[
'count'
];
$result_filter
=
$this
->
calculateResultFilter
(
trim
(
$result
[
'filter'
],
'"'
));
if
(
isset
(
$facet_results
[
$result_filter
[
'raw'
]]))
{
$facet_results
[
$result_filter
[
'raw'
]]
->
setCount
(
$facet_results
[
$result_filter
[
'raw'
]]
->
getCount
()
+
$count
);
}
else
{
$facet_results
[
$result_filter
[
'raw'
]]
=
new
Result
(
$this
->
facet
,
$result_filter
[
'raw'
],
$result_filter
[
'display'
],
$count
);
}
}
}
$this
->
facet
->
setResults
(
$facet_results
);
return
$this
->
facet
;
}
...
...
tests/src/Kernel/Plugin/query_type/SearchApiDateTest.php
View file @
888290d8
...
...
@@ -5,7 +5,10 @@ namespace Drupal\Tests\facets\Kernel\Plugin\query_type;
use
Drupal\KernelTests\KernelTestBase
;
use
Drupal\facets\Entity\Facet
;
use
Drupal\facets\Plugin\facets\query_type
\
SearchApiDate
;
use
Drupal\search_api
\
Backend\BackendInterface
;
use
Drupal\search_api
\
IndexInterface
;
use
Drupal\search_api
\
Plugin\views\query\SearchApiQuery
;
use
Drupal\search_api
\
ServerInterface
;
/**
* Kernel test for date query type.
...
...
@@ -32,7 +35,15 @@ class SearchApiDateTest extends KernelTestBase {
* @dataProvider resultsProvider
*/
public
function
testQueryTypeAnd
(
$granularity
,
$original_results
,
$grouped_results
)
{
$query
=
new
SearchApiQuery
([],
'search_api_query'
,
[]);
$backend
=
$this
->
prophesize
(
BackendInterface
::
class
);
$backend
->
getSupportedFeatures
()
->
willReturn
([]);
$server
=
$this
->
prophesize
(
ServerInterface
::
class
);
$server
->
getBackend
()
->
willReturn
(
$backend
);
$index
=
$this
->
prophesize
(
IndexInterface
::
class
);
$index
->
getServerInstance
()
->
willReturn
(
$server
);
$query
=
$this
->
prophesize
(
SearchApiQuery
::
class
);
$query
->
getIndex
()
->
willReturn
(
$index
);
$facetReflection
=
new
\
ReflectionClass
(
'Drupal\facets\Entity\Facet'
);
$facet
=
new
Facet
(
[
'query_operator'
=>
'AND'
,
'widget'
=>
'links'
],
...
...
@@ -54,7 +65,7 @@ class SearchApiDateTest extends KernelTestBase {
$query_type
=
new
SearchApiDate
(
[
'facet'
=>
$facet
,
'query'
=>
$query
,
'query'
=>
$query
->
reveal
()
,
'results'
=>
$original_results
,
],
'search_api_string'
,
...
...
tests/src/Unit/Plugin/query_type/SearchApiGranularTest.php
View file @
888290d8
...
...
@@ -4,7 +4,10 @@ namespace Drupal\Tests\facets\Unit\Plugin\query_type;
use
Drupal\facets\Entity\Facet
;
use
Drupal\facets\Plugin\facets\query_type
\
SearchApiGranular
;
use
Drupal\search_api
\
Backend\BackendInterface
;
use
Drupal\search_api
\
IndexInterface
;
use
Drupal\search_api
\
Plugin\views\query\SearchApiQuery
;
use
Drupal\search_api
\
ServerInterface
;
use
Drupal\Tests\UnitTestCase
;
/**
...
...
@@ -18,7 +21,15 @@ class SearchApiGranularTest extends UnitTestCase {
* Tests string query type without executing the query with an "AND" operator.
*/
public
function
testQueryTypeAnd
()
{
$query
=
new
SearchApiQuery
([],
'search_api_query'
,
[]);
$backend
=
$this
->
prophesize
(
BackendInterface
::
class
);
$backend
->
getSupportedFeatures
()
->
willReturn
([]);
$server
=
$this
->
prophesize
(
ServerInterface
::
class
);
$server
->
getBackend
()
->
willReturn
(
$backend
);
$index
=
$this
->
prophesize
(
IndexInterface
::
class
);
$index
->
getServerInstance
()
->
willReturn
(
$server
);
$query
=
$this
->
prophesize
(
SearchApiQuery
::
class
);
$query
->
getIndex
()
->
willReturn
(
$index
);
$facetReflection
=
new
\
ReflectionClass
(
'Drupal\facets\Entity\Facet'
);
$facet
=
new
Facet
(
[
'query_operator'
=>
'AND'
,
'widget'
=>
'links'
],
...
...
@@ -49,7 +60,7 @@ class SearchApiGranularTest extends UnitTestCase {
$query_type
=
new
SearchApiGranular
(
[
'facet'
=>
$facet
,
'query'
=>
$query
,
'query'
=>
$query
->
reveal
()
,
'results'
=>
$original_results
,
],
'search_api_string'
,
...
...
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