Smalyshev has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/230927

Change subject: API for _suggest for Elasticsearch
......................................................................

API for _suggest for Elasticsearch

Change-Id: I9c36b9c02a23c30038b1f0d8e8b8a9e310829e7e
---
M CirrusSearch.php
M autoload.php
A includes/Api/Suggest.php
M includes/Searcher.php
4 files changed, 92 insertions(+), 0 deletions(-)


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

diff --git a/CirrusSearch.php b/CirrusSearch.php
index da85959..fe18b58 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -849,6 +849,7 @@
 $wgAPIModules['cirrus-config-dump'] = 'CirrusSearch\Api\ConfigDump';
 $wgAPIModules['cirrus-mapping-dump'] = 'CirrusSearch\Api\MappingDump';
 $wgAPIModules['cirrus-settings-dump'] = 'CirrusSearch\Api\SettingsDump';
+$wgAPIModules['cirrus-suggest'] = 'CirrusSearch\Api\Suggest';
 
 /**
  * Jenkins configuration required to get all the browser tests passing cleanly.
diff --git a/autoload.php b/autoload.php
index f40c246..b9b893a 100644
--- a/autoload.php
+++ b/autoload.php
@@ -9,6 +9,7 @@
        'CirrusSearch\\Api\\FreezeWritesToCluster' => __DIR__ . 
'/includes/Api/FreezeWritesToCluster.php',
        'CirrusSearch\\Api\\MappingDump' => __DIR__ . 
'/includes/Api/MappingDump.php',
        'CirrusSearch\\Api\\SettingsDump' => __DIR__ . 
'/includes/Api/SettingsDump.php',
+       'CirrusSearch\\Api\\Suggest' => __DIR__ . '/includes/Api/Suggest.php',
        'CirrusSearch\\BuildDocument\\Builder' => __DIR__ . 
'/includes/BuildDocument/Builder.php',
        'CirrusSearch\\BuildDocument\\FileDataBuilder' => __DIR__ . 
'/includes/BuildDocument/FileDataBuilder.php',
        'CirrusSearch\\BuildDocument\\IncomingsLinksScoringMethod' => __DIR__ . 
'/includes/BuildDocument/SuggestScoring.php',
diff --git a/includes/Api/Suggest.php b/includes/Api/Suggest.php
new file mode 100644
index 0000000..268ca6f
--- /dev/null
+++ b/includes/Api/Suggest.php
@@ -0,0 +1,58 @@
+<?php
+namespace CirrusSearch\Api;
+
+use \ApiBase;
+use CirrusSearch\Searcher;
+use RequestContext;
+
+/**
+ * Use ElasticSearch suggestion API
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+class Suggest extends ApiBase {
+
+       public function execute() {
+               $context = RequestContext::getMain();
+               $user = $context->getUser();
+               $searcher = new Searcher( 0, 10, null, $user );
+
+               $queryText = "duc";
+               $fieldName = "suggestor";
+ini_set('display_errors', 0);
+               $result = $searcher->suggest( $queryText, $fieldName );
+               if($result->isOK()) {
+                       $data = $result->getValue()->getData();
+                       //var_dump($data); exit();
+                       if(isset($data[$fieldName])) {
+                               $this->getResult()->addValue( 'suggest', 
$fieldName, $data[$fieldName]);
+                       }
+               } else {
+                       $this->getResult()->addValue( null, "error", 
$result->getErrorsArray());
+               }
+       }
+
+       public function getAllowedParams() {
+               return array();
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getDescription() {
+               return 'Call ElasticSearch suggestion API.';
+       }
+}
diff --git a/includes/Searcher.php b/includes/Searcher.php
index b7e216e..60e780d 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -17,6 +17,7 @@
 use \Title;
 use \UsageException;
 use User;
+use Elastica\Request;
 
 /**
  * Performs searches using Elasticsearch.  Note that each instance of this 
class
@@ -758,6 +759,36 @@
                return $result;
        }
 
+       public function suggest( $text, $fieldName ) {
+               $this->term = $text;
+               // 
https://www.elastic.co/guide/en/elasticsearch/reference/1.6/search-suggesters-completion.html
+               $suggest = array(
+                       $fieldName => array(
+                                       'text' => $text,
+                                       'completion' => array(
+                                                       'field' => 'suggest',
+                                       )
+                       ),
+               );
+               $queryOptions = array();
+               $queryOptions[ 'timeout' ] = $GLOBALS[ 
'wgCirrusSearchSearchShardTimeout' ][ 'default' ];
+               Connection::setTimeout( $GLOBALS[ 
'wgCirrusSearchSearchShardTimeout' ][ 'default' ] );
+
+               $index = Connection::getIndex( wfWikiId(), 
Connection::TITLE_SUGGEST_TYPE );
+               $description = "suggest query for {query}";
+               $logContext = array(
+                               'query' => $text,
+               );
+
+               $this->start( $description, $logContext );
+               try {
+                       $result = $index->request("_suggest", Request::POST, 
$suggest, $queryOptions);
+                       return $this->success( $result );
+               } catch ( \Elastica\Exception\ExceptionInterface $e ) {
+                       return $this->failure( $e );
+               }
+       }
+
        /**
         * Builds a match query against $field for $title.  $title is munged to 
make title matching better more
         * intuitive for users.
@@ -1182,6 +1213,7 @@
                                'path' => $search->getPath(),
                                'params' => $search->getOptions(),
                                'query' => $query->toArray(),
+                               'options' => $queryOptions,
                        ) );
                }
 

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

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

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

Reply via email to