Smalyshev has uploaded a new change for review.

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

Change subject: Add nested field support
......................................................................

Add nested field support

Bug: T89733
Change-Id: I82a82526e2e254edc1fa7d861d7ac23d9cf07d1c
---
M CirrusSearch.php
M autoload.php
M includes/Search/CirrusIndexField.php
A includes/Search/NestedIndexField.php
M includes/Search/TextIndexField.php
A tests/unit/IndexFieldsTest.php
6 files changed, 66 insertions(+), 2 deletions(-)


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

diff --git a/CirrusSearch.php b/CirrusSearch.php
index 8d39bbe..7211a28 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -990,7 +990,7 @@
        SearchIndexField::INDEX_TYPE_INTEGER => 
'CirrusSearch\\IntegerIndexField',
        SearchIndexField::INDEX_TYPE_NUMBER => 'CirrusSearch\\NumberIndexField',
        SearchIndexField::INDEX_TYPE_DATETIME => 
'CirrusSearch\\DatetimeIndexField',
-//     SearchIndexField::INDEX_TYPE_NESTED => 'CirrusSearch\\NestedIndexField',
+       SearchIndexField::INDEX_TYPE_NESTED => 'CirrusSearch\\NestedIndexField',
 );
 
 /**
diff --git a/autoload.php b/autoload.php
index d8d3ad5..0591497 100644
--- a/autoload.php
+++ b/autoload.php
@@ -86,6 +86,7 @@
        'CirrusSearch\\Maintenance\\Validators\\SpecificAliasValidator' => 
__DIR__ . '/includes/Maintenance/Validators/SpecificAliasValidator.php',
        'CirrusSearch\\Maintenance\\Validators\\Validator' => __DIR__ . 
'/includes/Maintenance/Validators/Validator.php',
        'CirrusSearch\\NearMatchPicker' => __DIR__ . 
'/includes/NearMatchPicker.php',
+       'CirrusSearch\\NestedIndexField' => __DIR__ . 
'/includes/Search/NestedIndexField.php',
        'CirrusSearch\\NumberIndexField' => __DIR__ . 
'/includes/Search/NumberIndexField.php',
        'CirrusSearch\\OtherIndexes' => __DIR__ . '/includes/OtherIndexes.php',
        'CirrusSearch\\Saneitize' => __DIR__ . '/maintenance/saneitize.php',
diff --git a/includes/Search/CirrusIndexField.php 
b/includes/Search/CirrusIndexField.php
index e882094..a4421f0 100644
--- a/includes/Search/CirrusIndexField.php
+++ b/includes/Search/CirrusIndexField.php
@@ -1,7 +1,6 @@
 <?php
 namespace CirrusSearch;
 
-
 use SearchEngine;
 use SearchIndexFieldDefinition;
 use SearchIndexField;
diff --git a/includes/Search/NestedIndexField.php 
b/includes/Search/NestedIndexField.php
new file mode 100644
index 0000000..b36d808
--- /dev/null
+++ b/includes/Search/NestedIndexField.php
@@ -0,0 +1,26 @@
+<?php
+namespace CirrusSearch;
+
+use SearchIndexField;
+use SearchEngine;
+
+class NestedIndexField extends CirrusIndexField {
+       protected $typeName = "nested";
+
+       /**
+        * Add sub-field for nested field
+        * @param      string      $name  Field name
+        * @param SearchIndexField $subfield Field object
+        */
+       public function addSubfield($name, SearchIndexField $subfield) {
+               $this->subfields[$name] = $subfield;
+       }
+
+       public function getMapping( SearchEngine $engine ) {
+               $fields = parent::getMapping( $engine );
+               foreach ( $this->subfields as $name => $sub ) {
+                       $fields['properties'][$name] = $sub->getMapping( 
$engine );
+               }
+               return $fields;
+       }
+}
\ No newline at end of file
diff --git a/includes/Search/TextIndexField.php 
b/includes/Search/TextIndexField.php
index acf4584..5f12d8f 100644
--- a/includes/Search/TextIndexField.php
+++ b/includes/Search/TextIndexField.php
@@ -3,6 +3,7 @@
 
 use CirrusSearch\Maintenance\MappingConfigBuilder;
 use SearchIndexField;
+use SearchEngine;
 
 /**
  * Index field representing keyword.
diff --git a/tests/unit/IndexFieldsTest.php b/tests/unit/IndexFieldsTest.php
new file mode 100644
index 0000000..1ad16b6
--- /dev/null
+++ b/tests/unit/IndexFieldsTest.php
@@ -0,0 +1,37 @@
+<?php
+
+use MediaWiki\MediaWikiServices;
+
+class IndexFieldsTest extends MediaWikiTestCase {
+
+       public function getTypes() {
+               return [
+                       [ SearchIndexField::INDEX_TYPE_TEXT, 'string', 
'CirrusSearch\\TextIndexField' ],
+                       [ SearchIndexField::INDEX_TYPE_KEYWORD, 'string', 
'CirrusSearch\\KeywordIndexField' ],
+                       [ SearchIndexField::INDEX_TYPE_INTEGER, 'long', 
'CirrusSearch\\IntegerIndexField' ],
+                       [ SearchIndexField::INDEX_TYPE_NUMBER, 'double', 
'CirrusSearch\\NumberIndexField' ],
+                       [ SearchIndexField::INDEX_TYPE_DATETIME, 'date', 
'CirrusSearch\\DatetimeIndexField' ],
+                       [ SearchIndexField::INDEX_TYPE_NESTED, 'nested', 
'CirrusSearch\\NestedIndexField' ],
+               ];
+       }
+
+       /**
+        * @dataProvider getTypes
+        * @param int    $type Field type
+        * @param string $typeName Internal type name
+        * @param string $klass Class name
+        */
+       public function testFieldTypes( $type, $typeName, $klass ) {
+               $config =
+                       
MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 
'CirrusSearch' );
+               $engine = new CirrusSearch();
+               /**
+                * @var \CirrusSearch\CirrusIndexField $idxField
+                */
+               $idxField = new $klass( "test$typeName", $type, $config );
+               $map = $idxField->getMapping( $engine );
+               $this->assertEquals( $typeName, $map['type'] );
+               $this->assertEquals( $type, $idxField->getIndexType() );
+               $this->assertEquals( "test$typeName", $idxField->getName() );
+       }
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I82a82526e2e254edc1fa7d861d7ac23d9cf07d1c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <smalys...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to