-
Notifications
You must be signed in to change notification settings - Fork 395
Support queries where part of the query is using ExactSettings #4973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
124bcf0
cd87e57
c7cf054
8b65cc3
1b5513f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -133,6 +133,8 @@ public function __construct( | |||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| public function build(AbstractQuery $query, ?ParamBag $params = null) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| $query = $this->possiblyConvertMixedExactQueryIntoAdvanced($query); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $newParams = new ParamBag(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Add spelling query if applicable -- note that we must set this up before | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -210,6 +212,64 @@ public function build(AbstractQuery $query, ?ParamBag $params = null) | |||||||||||||||||||||||||||||||||||||
| return $newParams; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Converts a simple query (Query) into an advanced one (QueryGroup) if part of it should be an exact query. | ||||||||||||||||||||||||||||||||||||||
| * This only supports a single exact query (surrounded with quotes) combined with a non-exact query. | ||||||||||||||||||||||||||||||||||||||
| * Logical operators can be used, but not parenthesis or field names. | ||||||||||||||||||||||||||||||||||||||
|
damien-git marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||
| * The original query is returned for any non-supported case. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * @param QueryGroup|Query $query User query | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * @return QueryGroup|Query | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| protected function possiblyConvertMixedExactQueryIntoAdvanced($query) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if ($query instanceof QueryGroup) { | ||||||||||||||||||||||||||||||||||||||
| return $query; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to use QueryInterface for greater flexibility, and have more explicit types. Maybe something like:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I was using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, that's why I suggested the It may be worthwhile to invest some effort into cleaning up the other type issues you mention as a dev-12.0 PR (I'm sure problems have accumulated over time as the code has evolved), but I'll leave it to you whether or not now is the time for that. :-)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will go with your solution, just using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That being said, if you think it's better to use |
||||||||||||||||||||||||||||||||||||||
| $handler = $query->getHandler(); | ||||||||||||||||||||||||||||||||||||||
| if ($handler && !isset($this->exactSpecs[strtolower($handler)])) { | ||||||||||||||||||||||||||||||||||||||
| return $query; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| $queryString = trim($query->getString()); | ||||||||||||||||||||||||||||||||||||||
| if (!preg_match('/^([^":()+]*)"([^"]+)"([^":()]*)$/u', $queryString, $parts)) { | ||||||||||||||||||||||||||||||||||||||
| return $query; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| $groupOperator = 'AND'; | ||||||||||||||||||||||||||||||||||||||
| $negateQuotedPart = false; | ||||||||||||||||||||||||||||||||||||||
| $before = trim($parts[1]); | ||||||||||||||||||||||||||||||||||||||
| if (preg_match('/^(.*)\s*(NOT|-)$/u', $before, $notParts)) { | ||||||||||||||||||||||||||||||||||||||
| $before = $notParts[1]; | ||||||||||||||||||||||||||||||||||||||
| $negateQuotedPart = true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (preg_match('/^(.*)\s*(AND|OR)$/u', $before, $beforeParts)) { | ||||||||||||||||||||||||||||||||||||||
|
demiankatz marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||
| $before = $beforeParts[1]; | ||||||||||||||||||||||||||||||||||||||
| $groupOperator = $beforeParts[2]; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| $quoted = '"' . $parts[2] . '"'; | ||||||||||||||||||||||||||||||||||||||
| $after = trim($parts[3]); | ||||||||||||||||||||||||||||||||||||||
| if (preg_match('/^(AND|OR)\s*(.*)$/u', $after, $afterParts)) { | ||||||||||||||||||||||||||||||||||||||
| $groupOperator = $afterParts[1]; | ||||||||||||||||||||||||||||||||||||||
| $after = $afterParts[2]; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (($before == '' && $after == '') || ($before != '' && $after != '')) { | ||||||||||||||||||||||||||||||||||||||
| return $query; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| $subQueries = []; | ||||||||||||||||||||||||||||||||||||||
| if ($before != '') { | ||||||||||||||||||||||||||||||||||||||
| $subQueries[] = new Query($before, $handler); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if ($negateQuotedPart) { | ||||||||||||||||||||||||||||||||||||||
| $subQueries[] = new QueryGroup('NOT', [ new Query($quoted, $handler) ]); | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| $subQueries[] = new Query($quoted, $handler); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if ($after != '') { | ||||||||||||||||||||||||||||||||||||||
| $subQueries[] = new Query($after, $handler); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return new QueryGroup($groupOperator, $subQueries); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Check if the conditions match for an extra parameter | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,6 +295,74 @@ public function testExactQueryHandler() | |
| $this->assertEquals('c d', $qf[0]); | ||
| } | ||
|
|
||
| /** | ||
| * Test queries with mixed exact and non-exact parts. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testMixedExactQueryHandler() | ||
| { | ||
| // Check QueryBuilder without ExactSettings | ||
| $qb = new QueryBuilder( | ||
| [ | ||
| 'TestHandler' => [ | ||
| 'DismaxFields' => ['a'], | ||
| 'DismaxHandler' => 'edismax', | ||
| ], | ||
| ] | ||
| ); | ||
| $q = new Query('"t1" AND t2', 'TestHandler'); | ||
| $response = $qb->build($q); | ||
| $queryString = $response->get('q')[0]; | ||
| $this->assertEquals('"t1" AND t2', $queryString); | ||
|
|
||
| // Expected inputs and outputs with ExactSettings: | ||
| $tests = [ | ||
| ['"t1"', '"t1"'], // simple exact queries are not affected | ||
| ['("t1" OR t2) AND t3', '("t1" OR t2) AND t3'], // queries with parenthesis are not supported | ||
| ['"t1" AND title:t2', '"t1" AND title:t2'], // queries with field are not supported | ||
| ['"t1" AND "t2"', '"t1" AND "t2"'], // queries with multiple exact parts are not supported | ||
| ['t1 AND "t2" AND t3', 't1 AND "t2" AND t3'], // queries with an exact part in the middle are not supported | ||
| ['"t1" t2', '((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\"") AND ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2"))'], | ||
| ['"t1" AND t2', '((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\"") AND ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2"))'], | ||
| ['"t1" OR t2', '((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\"") OR ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2"))'], | ||
| ['t1 AND "t2"', '((_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t1") AND ' . | ||
| '(_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t2\""))'], | ||
| ['NOT "t1" AND t2', '((*:* NOT ((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\""))) AND ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2"))'], | ||
| ['t1 AND NOT "t2"', '((_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t1 AND") AND ' . | ||
| '(*:* NOT ((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t2\""))))'], | ||
| ['-"t1" t2', '((*:* NOT ((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\""))) AND ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2"))'], | ||
| ['"t1" AND t2 AND t3', '((_query_:"{!edismax qf=\"b\" mm=\\\'0%\\\'}\"t1\"") AND ' . | ||
| '(_query_:"{!edismax qf=\"a\" mm=\\\'0%\\\'}t2 AND t3"))'], // would be different with dismax | ||
| ]; | ||
|
Comment on lines
+320
to
+342
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this part be better handled as a separate test using a data provider?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is consistent with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, these existing tests are very old and not using best practices. Even
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened #4981 as a demonstration of the sort of change I had in mind. |
||
|
|
||
| $qb = new QueryBuilder( | ||
| [ | ||
| 'TestHandler' => [ | ||
| 'DismaxFields' => ['a'], | ||
| 'DismaxHandler' => 'edismax', | ||
| 'ExactSettings' => [ | ||
| 'DismaxFields' => ['b'], | ||
| 'DismaxHandler' => 'edismax', | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| foreach ($tests as $test) { | ||
| [$input, $output] = $test; | ||
| $q = new Query($input, 'TestHandler'); | ||
| $response = $qb->build($q); | ||
| $queryString = $response->get('q')[0]; | ||
| $this->assertEquals($output, $queryString); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test generation with a query handler with a filter set and DisMax settings | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.