DCausse has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/387823 )

Change subject: Switch comp suggest to msearch
......................................................................

Switch comp suggest to msearch

preliminary patch prior adding prefix search

Bug: T178474
Change-Id: I6b4efd6ed37f9cdee0da9e2273cbee468f8e34bb
---
M includes/CompletionSuggester.php
M tests/unit/fixtures/requestLogging/completion_basic_001.response
M tests/unit/fixtures/requestLogging/completion_basic_002.response
3 files changed, 203 insertions(+), 139 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/23/387823/1

diff --git a/includes/CompletionSuggester.php b/includes/CompletionSuggester.php
index fd2f0db..0f79d68 100644
--- a/includes/CompletionSuggester.php
+++ b/includes/CompletionSuggester.php
@@ -3,10 +3,14 @@
 namespace CirrusSearch;
 
 use CirrusSearch\Query\CompSuggestQueryBuilder;
+use CirrusSearch\Query\PrefixSearchQueryBuilder;
 use Elastica\Exception\ExceptionInterface;
 use Elastica\Index;
+use Elastica\Multi\ResultSet;
+use Elastica\Multi\Search as MultiSearch;
 use Elastica\Query;
 use CirrusSearch\Search\SearchContext;
+use Elastica\Search;
 use MediaWiki\MediaWikiServices;
 use SearchSuggestionSet;
 use Status;
@@ -56,6 +60,11 @@
  */
 class CompletionSuggester extends ElasticsearchIntermediary {
        /**
+        * @const string multisearch key to identify the comp suggest request
+        */
+       const MSEARCH_KEY_SUGGEST = "suggest";
+
+       /**
         * @var integer maximum number of result (final)
         */
        private $limit;
@@ -90,6 +99,11 @@
         * @var CompSuggestQueryBuilder $compSuggestBuilder (final)
         */
        private $compSuggestBuilder;
+
+       /**
+        * @var PrefixSearchQueryBuilder $prefixSearchQueryBuilder (final)
+        */
+       private $prefixSearchQueryBuilder;
 
        /**
         * @param Connection $conn
@@ -129,6 +143,7 @@
                        $limit,
                        $offset
                );
+               $this->prefixSearchQueryBuilder = new 
PrefixSearchQueryBuilder();
        }
 
        /**
@@ -143,30 +158,35 @@
         * @return Status
         */
        public function suggest( $text, $variants = null ) {
-               if ( !$this->compSuggestBuilder->areResultsPossible() ) {
+               $suggestSearch = $this->getSuggestSearchRequest( $text, 
$variants );
+               $msearch = new MultiSearch( $this->connection->getClient() );
+               $msearch->addSearch( $suggestSearch, self::MSEARCH_KEY_SUGGEST 
);
+
+               if ( empty( $msearch->getSearches() ) ) {
                        return Status::newGood( 
SearchSuggestionSet::emptySuggestionSet() );
                }
 
-               $suggest = $this->compSuggestBuilder->build( $text, $variants );
                $this->connection->setTimeout( $this->config->getElement( 
'wgCirrusSearchClientSideSearchTimeout', 'default' ) );
                $result = Util::doPoolCounterWork(
                        'CirrusSearch-Completion',
                        $this->user,
-                       function () use( $suggest, $text ) {
+                       function () use( $msearch, $text ) {
                                $log = $this->newLog( "{queryType} search for 
'{query}'", "comp_suggest", [
                                        'query' => $text,
                                        'offset' => $this->offset,
                                ] );
                                $this->start( $log );
                                try {
-                                       $query = new Query( new 
Query\MatchNone() );
-                                       $query->setSize( 0 );
-                                       $query->setSuggest( $suggest );
-                                       $query->setSource( [ 'target_title' ] );
-                                       $result = 
$this->completionIndex->search( $query );
-                                       $result = 
$this->compSuggestBuilder->postProcess( $result,
-                                               
$this->completionIndex->getName(), $log );
-                                       return $this->success( $result );
+                                       $results = $msearch->search();
+                                       if ( $results->hasError() ||
+                                               // Catches HTTP errors (ex: 
5xx) not reported
+                                               // by hasError()
+                                               !$results->getResponse()->isOk()
+                                       ) {
+                                               return $this->multiFailure( 
$results );
+                                       }
+                                       $st = $this->success( 
$this->processMSearchResponse( $results, $log ) );
+                                       return $st;
                                } catch ( ExceptionInterface $e ) {
                                        return $this->failure( $e );
                                }
@@ -176,6 +196,46 @@
        }
 
        /**
+        * @param ResultSet $results
+        * @param CompletionRequestLog $log
+        * @return SearchSuggestionSet
+        */
+       private function processMSearchResponse( ResultSet $results, 
CompletionRequestLog $log ) {
+               if ( isset( 
$results->getResultSets()[self::MSEARCH_KEY_SUGGEST] ) ) {
+                       $suggestSet = $this->compSuggestBuilder->postProcess(
+                               
$results->getResultSets()[self::MSEARCH_KEY_SUGGEST],
+                               $this->completionIndex->getName(),
+                               $log
+                       );
+
+                       return $suggestSet;
+               }
+               return SearchSuggestionSet::emptySuggestionSet();
+       }
+
+       /**
+        * @param string $text Search term
+        * @param string[]|null $variants Search term variants
+        * (usually issued from $wgContLang->autoConvertToAllVariants( $text ) )
+        * @return Search|null
+        */
+       private function getSuggestSearchRequest( $text, $variants ) {
+               if ( !$this->compSuggestBuilder->areResultsPossible() ) {
+                       return null;
+               }
+
+               $suggest = $this->compSuggestBuilder->build( $text, $variants );
+               $query = new Query( new Query\MatchNone() );
+               $query->setSize( 0 );
+               $query->setSuggest( $suggest );
+               $query->setSource( [ 'target_title' ] );
+               $search = new Search( $this->connection->getClient() );
+               $search->addIndex( $this->completionIndex );
+               $search->setQuery( $query );
+               return $search;
+       }
+
+       /**
         * @param string $description
         * @param string $queryType
         * @param string[] $extra
diff --git a/tests/unit/fixtures/requestLogging/completion_basic_001.response 
b/tests/unit/fixtures/requestLogging/completion_basic_001.response
index a814f28..9a9d3de 100644
--- a/tests/unit/fixtures/requestLogging/completion_basic_001.response
+++ b/tests/unit/fixtures/requestLogging/completion_basic_001.response
@@ -1,82 +1,84 @@
-[
-    {
-        "took": 13,
-        "timed_out": false,
-        "_shards": {
-            "total": 4,
-            "successful": 4,
-            "failed": 0
-        },
-        "hits": {
-            "total": 0,
-            "max_score": 0,
-            "hits": []
-        },
-        "suggest": {
-            "plain": [
-                {
-                    "text": "main p",
-                    "offset": 0,
-                    "length": 6,
-                    "options": [
-                        {
-                            "text": "Main Page",
-                            "_index": "cirrustestwiki_titlesuggest_1485266182",
-                            "_type": "titlesuggest",
-                            "_id": "1t",
-                            "_score": 5679327
-                        }
-                    ]
-                }
-            ],
-            "plain_fuzzy_1": [
-                {
-                    "text": "main p",
-                    "offset": 0,
-                    "length": 6,
-                    "options": [
-                        {
-                            "text": "Main Page",
-                            "_index": "cirrustestwiki_titlesuggest_1485266182",
-                            "_type": "titlesuggest",
-                            "_id": "1t",
-                            "_score": 29487956
-                        }
-                    ]
-                }
-            ],
-            "plain_stop": [
-                {
-                    "text": "main p",
-                    "offset": 0,
-                    "length": 6,
-                    "options": [
-                        {
-                            "text": "Main Page",
-                            "_index": "cirrustestwiki_titlesuggest_1485266182",
-                            "_type": "titlesuggest",
-                            "_id": "1t",
-                            "_score": 5679327
-                        }
-                    ]
-                }
-            ],
-            "plain_stop_fuzzy_1": [
-                {
-                    "text": "main p",
-                    "offset": 0,
-                    "length": 6,
-                    "options": [
-                        {
-                            "text": "Main Page",
-                            "_index": "cirrustestwiki_titlesuggest_1485266182",
-                            "_type": "titlesuggest",
-                            "_id": "1t",
-                            "_score": 23590364
-                        }
-                    ]
-                }
-            ]
+[{
+    "responses": [
+        {
+            "took": 13,
+            "timed_out": false,
+            "_shards": {
+                "total": 4,
+                "successful": 4,
+                "failed": 0
+            },
+            "hits": {
+                "total": 0,
+                "max_score": 0,
+                "hits": []
+            },
+            "suggest": {
+                "plain": [
+                    {
+                        "text": "main p",
+                        "offset": 0,
+                        "length": 6,
+                        "options": [
+                            {
+                                "text": "Main Page",
+                                "_index": 
"cirrustestwiki_titlesuggest_1485266182",
+                                "_type": "titlesuggest",
+                                "_id": "1t",
+                                "_score": 5679327
+                            }
+                        ]
+                    }
+                ],
+                "plain_fuzzy_1": [
+                    {
+                        "text": "main p",
+                        "offset": 0,
+                        "length": 6,
+                        "options": [
+                            {
+                                "text": "Main Page",
+                                "_index": 
"cirrustestwiki_titlesuggest_1485266182",
+                                "_type": "titlesuggest",
+                                "_id": "1t",
+                                "_score": 29487956
+                            }
+                        ]
+                    }
+                ],
+                "plain_stop": [
+                    {
+                        "text": "main p",
+                        "offset": 0,
+                        "length": 6,
+                        "options": [
+                            {
+                                "text": "Main Page",
+                                "_index": 
"cirrustestwiki_titlesuggest_1485266182",
+                                "_type": "titlesuggest",
+                                "_id": "1t",
+                                "_score": 5679327
+                            }
+                        ]
+                    }
+                ],
+                "plain_stop_fuzzy_1": [
+                    {
+                        "text": "main p",
+                        "offset": 0,
+                        "length": 6,
+                        "options": [
+                            {
+                                "text": "Main Page",
+                                "_index": 
"cirrustestwiki_titlesuggest_1485266182",
+                                "_type": "titlesuggest",
+                                "_id": "1t",
+                                "_score": 23590364
+                            }
+                        ]
+                    }
+                ]
+            }
         }
-    }
-]
+    ]
+}]
diff --git a/tests/unit/fixtures/requestLogging/completion_basic_002.response 
b/tests/unit/fixtures/requestLogging/completion_basic_002.response
index ba8f83a..a071a4b 100644
--- a/tests/unit/fixtures/requestLogging/completion_basic_002.response
+++ b/tests/unit/fixtures/requestLogging/completion_basic_002.response
@@ -1,48 +1,50 @@
-[{
-    "took": 28,
-    "timed_out": false,
-    "_shards": {
-        "total": 4,
-        "successful": 4,
-        "failed": 0
-    },
-    "hits": {
-        "total": 0,
-        "max_score": 0,
-        "hits": []
-    },
-    "suggest": {
-        "plain": [
-            {
-                "text": "does not exist or at least i hope not",
-                "offset": 0,
-                "length": 37,
-                "options": []
-            }
-        ],
-        "plain_fuzzy_1": [
-            {
-                "text": "does not exist or at least i hope not",
-                "offset": 0,
-                "length": 37,
-                "options": []
-            }
-        ],
-        "plain_stop": [
-            {
-                "text": "does not exist or at least i hope not",
-                "offset": 0,
-                "length": 37,
-                "options": []
-            }
-        ],
-        "plain_stop_fuzzy_1": [
-            {
-                "text": "does not exist or at least i hope not",
-                "offset": 0,
-                "length": 37,
-                "options": []
-            }
-        ]
-    }
+[{"responses":
+    [{
+        "took": 28,
+        "timed_out": false,
+        "_shards": {
+            "total": 4,
+            "successful": 4,
+            "failed": 0
+        },
+        "hits": {
+            "total": 0,
+            "max_score": 0,
+            "hits": []
+        },
+        "suggest": {
+            "plain": [
+                {
+                    "text": "does not exist or at least i hope not",
+                    "offset": 0,
+                    "length": 37,
+                    "options": []
+                }
+            ],
+            "plain_fuzzy_1": [
+                {
+                    "text": "does not exist or at least i hope not",
+                    "offset": 0,
+                    "length": 37,
+                    "options": []
+                }
+            ],
+            "plain_stop": [
+                {
+                    "text": "does not exist or at least i hope not",
+                    "offset": 0,
+                    "length": 37,
+                    "options": []
+                }
+            ],
+            "plain_stop_fuzzy_1": [
+                {
+                    "text": "does not exist or at least i hope not",
+                    "offset": 0,
+                    "length": 37,
+                    "options": []
+                }
+            ]
+        }
+    }]
 }]

-- 
To view, visit https://gerrit.wikimedia.org/r/387823
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b4efd6ed37f9cdee0da9e2273cbee468f8e34bb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: DCausse <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to