Manybubbles has uploaded a new change for review.

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

Change subject: Port scripts to groovy
......................................................................

Port scripts to groovy

Campared to MVEL Groovy is faster, more stable, and sandboxed.  It is only
supported in Elasticsearch 1.3 so we can't merge this until 1.3 is live
AND it won't work in mediawiki-vagrant without
I0de89fa9053b8e84aabaa004c6a9f39f21b40435 or production without
Iebd0a94888140a836189580c38ee4666dc5df8b2.

Change-Id: I1289fb52a65e0e6d4cc5fcb8c5931ec5425875bb
---
M includes/OtherIndexes.php
M includes/Searcher.php
M includes/Updater.php
M maintenance/updateOneSearchIndexConfig.php
4 files changed, 23 insertions(+), 19 deletions(-)


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

diff --git a/includes/OtherIndexes.php b/includes/OtherIndexes.php
index afe565d..ed7e382 100644
--- a/includes/OtherIndexes.php
+++ b/includes/OtherIndexes.php
@@ -69,8 +69,8 @@
         * @param Array(Title) $titles titles for which to add to the tracking 
list
         */
        public function addLocalSiteToOtherIndex( $titles ) {
-               // Script is in MVEL and is run in a context with local_site 
set to this wiki's name
-               $script  = <<<MVEL
+               // Script is in groovy and is run in a context with local_site 
set to this wiki's name
+               $script  = <<<GROOVY
                        if (!ctx._source.containsKey("local_sites_with_dupe")) {
                                ctx._source.local_sites_with_dupe = [local_site]
                        } else if 
(ctx._source.local_sites_with_dupe.contains(local_site)) {
@@ -78,7 +78,7 @@
                        } else {
                                ctx._source.local_sites_with_dupe += local_site
                        }
-MVEL;
+GROOVY;
                $this->updateOtherIndex( 'addLocalSite', $script, $titles );
        }
 
@@ -87,21 +87,21 @@
         * @param array(Title) $titles titles for which to remove the tracking 
field
         */
        public function removeLocalSiteFromOtherIndex( $titles ) {
-               // Script is in MVEL and is run in a context with local_site 
set to this wiki's name
-               $script  = <<<MVEL
+               // Script is in groovy and is run in a context with local_site 
set to this wiki's name
+               $script  = <<<GROOVY
                        if (!ctx._source.containsKey("local_sites_with_dupe")) {
                                ctx.op = "none"
                        } else if 
(!ctx._source.local_sites_with_dupe.remove(local_site)) {
                                ctx.op = "none"
                        }
-MVEL;
+GROOVY;
                $this->updateOtherIndex( 'removeLocalSite', $script, $titles );
        }
 
        /**
         * Update the indexes for other wiki that also store information about 
$titles.
         * @param string $actionName name of the action to report in logging
-        * @param string $scriptSource MVEL source script for performing the 
update
+        * @param string $scriptSource groovy source script for performing the 
update
         * @param array(Title) $titles titles in other indexes to update
         * @return bool false on failure, null otherwise
         */
@@ -131,7 +131,7 @@
                                $findIdsMultiSearch->addSearch( 
$type->createSearch( $query ) );
                                $findIdsClosures[] = function( $id ) use
                                                ( $scriptSource, $bulk, 
$otherIndex, $localSite, &$updatesInBulk ) {
-                                       $script = new \Elastica\Script( 
$scriptSource, array( 'local_site' => $localSite ), 'mvel' );
+                                       $script = new \Elastica\Script( 
$scriptSource, array( 'local_site' => $localSite ), 'groovy' );
                                        $script->setId( $id );
                                        $script->setParam( '_type', 'page' );
                                        $script->setParam( '_index', 
$otherIndex );
diff --git a/includes/Searcher.php b/includes/Searcher.php
index 38a68b1..4c0020e 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -418,7 +418,7 @@
                                // The setAllowMutate call is documented to 
speed up operations but be thread unsafe.  You'd think
                                // that is ok because scripts are always 
executed in a single thread but it isn't ok.  It causes
                                // all operations to unstable, so far as I can 
tell.
-                               $script = <<<MVEL
+                               $script = <<<GROOVY
 import org.apache.lucene.util.automaton.*;
 sourceText = _source.get("source_text");
 if (sourceText == null) {
@@ -438,7 +438,7 @@
        automaton.run(sourceText);
 }
 
-MVEL;
+GROOVY;
                                $filterDestination[] = new 
\Elastica\Filter\Script( new \Elastica\Script(
                                        $script,
                                        array(
@@ -450,7 +450,7 @@
                                                'automaton' => null,
                                                'locale' => null,
                                        ),
-                                       'mvel'
+                                       'groovy'
                                ) );
                        }
                );
@@ -1229,8 +1229,8 @@
                // Customize score by boosting based on incoming links count
                if ( $this->boostLinks ) {
                        $incomingLinks = "(doc['incoming_links'].isEmpty() ? 0 
: doc['incoming_links'].value)";
-                       $scoreBoostMvel = "log10($incomingLinks + 2)";
-                       $functionScore->addScriptScoreFunction( new 
\Elastica\Script( $scoreBoostMvel, null, 'mvel' ) );
+                       $scoreBoostGroovy = "log10($incomingLinks + 2)";
+                       $functionScore->addScriptScoreFunction( new 
\Elastica\Script( $scoreBoostGroovy, null, 'groovy' ) );
                        $useFunctionScore = true;
                }
 
@@ -1239,15 +1239,15 @@
                        // Convert half life for time in days to decay constant 
for time in milliseconds.
                        $decayConstant = log( 2 ) / $this->preferRecentHalfLife 
/ 86400000;
                        // e^ct - 1 where t is last modified time - now which 
is negative
-                       $exponentialDecayMvel = "Math.expm1($decayConstant * 
(doc['timestamp'].value - time()))";
+                       $exponentialDecayGroovy = "Math.expm1($decayConstant * 
(doc['timestamp'].value - Instant.now().getMillis()))";
                        // p(e^ct - 1)
                        if ( $this->preferRecentDecayPortion !== 1.0 ) {
-                               $exponentialDecayMvel = "$exponentialDecayMvel 
* $this->preferRecentDecayPortion";
+                               $exponentialDecayGroovy = 
"$exponentialDecayGroovy * $this->preferRecentDecayPortion";
                        }
                        // p(e^ct - 1) + 1 which is easier to calculate than, 
but reduces to 1 - p + pe^ct
                        // Which breaks the score into an unscaled portion (1 - 
p) and a scaled portion (p)
-                       $lastUpdateDecayMvel = "$exponentialDecayMvel + 1";
-                       $functionScore->addScriptScoreFunction( new 
\Elastica\Script( $lastUpdateDecayMvel, null, 'mvel' ) );
+                       $exponentialDecayGroovy = "$exponentialDecayGroovy + 1";
+                       $functionScore->addScriptScoreFunction( new 
\Elastica\Script( $exponentialDecayGroovy, null, 'groovy' ) );
                        $useFunctionScore = true;
                }
 
diff --git a/includes/Updater.php b/includes/Updater.php
index 5ae9d01..5783e59 100644
--- a/includes/Updater.php
+++ b/includes/Updater.php
@@ -362,6 +362,8 @@
        }
 
        private function docToScript( $doc ) {
+               // !!!!!!!!!NOTE!!!!!!!!
+               // This has not been ported to groovy because it is not in 
active use.  Please port if using.
                $scriptText = <<<MVEL
 changed = false;
 
diff --git a/maintenance/updateOneSearchIndexConfig.php 
b/maintenance/updateOneSearchIndexConfig.php
index d56dbda..ecb4653 100644
--- a/maintenance/updateOneSearchIndexConfig.php
+++ b/maintenance/updateOneSearchIndexConfig.php
@@ -749,8 +749,10 @@
                        $messagePrefix = "[$childNumber] ";
                        $this->output( $this->indent . $messagePrefix . 
"Starting child process reindex\n" );
                        // Note that it is not ok to abs(_uid.hashCode) because 
hashCode(Integer.MIN_VALUE) == Integer.MIN_VALUE
-                       $filter = new Elastica\Filter\Script(
-                               "(doc['_uid'].value.hashCode() & 
Integer.MAX_VALUE) % $children == $childNumber" );
+                       $filter = new Elastica\Filter\Script( array(
+                               'script' => "(doc['_uid'].value.hashCode() & 
Integer.MAX_VALUE) % $children == $childNumber",
+                               'lang' => 'groovy'
+                       ) );
                }
                $pageProperties = new 
\CirrusSearch\Maintenance\MappingConfigBuilder(
                        $this->prefixSearchStartsWithAny, $this->phraseUseText,

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

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

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

Reply via email to