From 734e4417c18c94b3fdde38116f3a33aca7543e95 Mon Sep 17 00:00:00 2001 From: thomascorthals Date: Fri, 20 Apr 2018 08:50:12 +0200 Subject: [PATCH] Added support for "sow" parameter (Split On Whitespace) (#586) Added support for "sow" parameter (Split On Whitespace) to select queries --- .../building-a-select-query.md | 29 ++++++++++--------- src/QueryType/Select/Query/Query.php | 27 +++++++++++++++++ src/QueryType/Select/RequestBuilder.php | 1 + .../Select/Query/AbstractQueryTest.php | 6 ++++ tests/QueryType/Select/RequestBuilderTest.php | 8 +++++ 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/docs/queries/select-query/building-a-select-query/building-a-select-query.md b/docs/queries/select-query/building-a-select-query/building-a-select-query.md index c58ed0d3a..c4256bc82 100644 --- a/docs/queries/select-query/building-a-select-query/building-a-select-query.md +++ b/docs/queries/select-query/building-a-select-query/building-a-select-query.md @@ -5,20 +5,21 @@ Options The options below can be set as query option values, but also by using the set/get methods. See the API docs for all available methods. -| Name | Type | Default value | Description | -|----------------------|------------------|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| handler | string | select | Name of the Solr request handler to use, without leading or trailing slashes | -| resultclass | string | Solarium\_Result\_Select | Classname for result. If you set a custom classname make sure the class is readily available (or through autoloading) | -| documentclass | string | Solarium\_Document\_ReadWrite | Classname for documents in the resultset. If you set a custom classname make sure the class is readily available (or through autoloading) | -| query | string | \*:\* | Query to execute | -| start | int | 0 | Start position (offset) in the complete Solr query resultset, to paginate big resultsets. | -| rows | integer | 10 | Number of rows to fetch, starting from the 'start' (offset) position. It's a limit, you might get less. | -| fields | string | - ,score | Comma separated list of fields to fetch from Solr. There are two special values: '\*' meaning 'all fields' and 'score' to also fetch the Solr document score value. | -| sort | array | empty array | Array with sort field as key and sort order as values. Multiple entries possible, they are used in the order of the array. Example: array('price' => 'asc') | -| querydefaultoperator | string | null | With a null value the default of your Solr config will be used. If you want to override this supply 'AND' or 'OR' as the value. | -| querydefaultfield | string | null | With a null value the default of your Solr config will be used. If you want to override this supply a field name as the value. | -| responsewriter | string | json | You can set this to 'phps' for improved response parsing performance, at the cost of a (possible) security risk. Only use 'phps' for trusted Solr instances. | -| tag | array of strings | null | You can supply one or multiple tags for the main query string, to allow for exclusion of the main query in facets | +| Name | Type | Default value | Description | +|----------------------|------------------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| handler | string | select | Name of the Solr request handler to use, without leading or trailing slashes | +| resultclass | string | Solarium\_Result\_Select | Classname for result. If you set a custom classname make sure the class is readily available (or through autoloading) | +| documentclass | string | Solarium\_Document\_ReadWrite | Classname for documents in the resultset. If you set a custom classname make sure the class is readily available (or through autoloading) | +| query | string | \*:\* | Query to execute | +| start | int | 0 | Start position (offset) in the complete Solr query resultset, to paginate big resultsets. | +| rows | integer | 10 | Number of rows to fetch, starting from the 'start' (offset) position. It's a limit, you might get less. | +| fields | string | - ,score | Comma separated list of fields to fetch from Solr. There are two special values: '\*' meaning 'all fields' and 'score' to also fetch the Solr document score value. | +| sort | array | empty array | Array with sort field as key and sort order as values. Multiple entries possible, they are used in the order of the array. Example: array('price' => 'asc') | +| querydefaultoperator | string | null | With a null value the default of your Solr config will be used. If you want to override this supply 'AND' or 'OR' as the value. | +| querydefaultfield | string | null | With a null value the default of your Solr config will be used. If you want to override this supply a field name as the value. | +| responsewriter | string | json | You can set this to 'phps' for improved response parsing performance, at the cost of a (possible) security risk. Only use 'phps' for trusted Solr instances. | +| tag | array of strings | null | You can supply one or multiple tags for the main query string, to allow for exclusion of the main query in facets | +| splitonwhitespace | bool | null | Specifies whether the query parser splits the query text on whitespace before it's sent to be analyzed. Available for 'lucene' and 'edismax' query parsers since Solr 6.5. | || Examples diff --git a/src/QueryType/Select/Query/Query.php b/src/QueryType/Select/Query/Query.php index d5824407d..cb8a8e439 100644 --- a/src/QueryType/Select/Query/Query.php +++ b/src/QueryType/Select/Query/Query.php @@ -763,6 +763,33 @@ public function clearCursormark() return $this->setOption('cursormark', null); } + /** + * Set SplitOnWhitespace option. + * + * Specifies whether the query parser splits the query text on whitespace before it's sent to be analyzed. + * + * The default is to split on whitespace, equivalent to &sow=true. + * The sow parameter was introduced in Solr 6.5. + * + * @param bool $splitOnWhitespace + * + * @return self Provides fluent interface + */ + public function setSplitOnWhitespace($splitOnWhitespace) + { + return $this->setOption('splitonwhitespace', $splitOnWhitespace); + } + + /** + * Get SplitOnWhitespace option. + * + * @return bool|null + */ + public function getSplitOnWhitespace() + { + return $this->getOption('splitonwhitespace'); + } + /** * Initialize options. * diff --git a/src/QueryType/Select/RequestBuilder.php b/src/QueryType/Select/RequestBuilder.php index 55203f868..0607b9935 100644 --- a/src/QueryType/Select/RequestBuilder.php +++ b/src/QueryType/Select/RequestBuilder.php @@ -37,6 +37,7 @@ public function build(QueryInterface $query) $request->addParam('q.op', $query->getQueryDefaultOperator()); $request->addParam('df', $query->getQueryDefaultField()); $request->addParam('cursorMark', $query->getCursormark()); + $request->addParam('sow', $query->getSplitOnWhitespace()); // add sort fields to request $sort = []; diff --git a/tests/QueryType/Select/Query/AbstractQueryTest.php b/tests/QueryType/Select/Query/AbstractQueryTest.php index 64f91a1dd..782629a03 100644 --- a/tests/QueryType/Select/Query/AbstractQueryTest.php +++ b/tests/QueryType/Select/Query/AbstractQueryTest.php @@ -689,6 +689,12 @@ public function testSetTags() $this->assertSame(['t3', 't4'], $this->query->getTags()); } + public function testSetAndGetSplitOnWhitespace() + { + $this->query->setSplitOnWhitespace(false); + $this->assertFalse($this->query->getSplitOnWhitespace()); + } + public function testGetSpatial() { $spatial = $this->query->getSpatial(); diff --git a/tests/QueryType/Select/RequestBuilderTest.php b/tests/QueryType/Select/RequestBuilderTest.php index a4e34ea03..ce1abce56 100644 --- a/tests/QueryType/Select/RequestBuilderTest.php +++ b/tests/QueryType/Select/RequestBuilderTest.php @@ -127,6 +127,14 @@ public function testWithTags() $this->assertSame('{!tag=t1,t2}cat:1', $request->getParam('q')); } + + public function testWithSplitOnWhitespace() + { + $this->query->setSplitOnWhitespace(false); + $request = $this->builder->build($this->query); + + $this->assertSame('false', $request->getParam('sow')); + } } class TestDummyComponent extends AbstractComponent