Skip to content

Commit

Permalink
Allow arrays for listtype and type in index search functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Feb 27, 2017
1 parent f2cabc9 commit 8d37307
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
7 changes: 5 additions & 2 deletions lib/mshoplib/src/MShop/Index/Manager/Text/MySQL.php
Expand Up @@ -26,15 +26,18 @@ class MySQL
'index.text.id' => array(
'code'=>'index.text.id',
'internalcode'=>'mindte."textid"',
'internaldeps'=>array( 'LEFT JOIN "mshop_index_text" AS mindte USE INDEX ("idx_msindte_value", "idx_msindte_p_s_lt_la_ty_do_va") ON mindte."prodid" = mpro."id"' ),
'internaldeps'=>array( 'LEFT JOIN "mshop_index_text" AS mindte
USE INDEX ("idx_msindte_value", "idx_msindte_p_s_lt_la_ty_do_va") ON mindte."prodid" = mpro."id"' ),
'label'=>'Product index text ID',
'type'=> 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'index.text.relevance' => array(
'code' => 'index.text.relevance()',
'internalcode' => ':site AND mindte."listtype" = $1 AND ( mindte."langid" = $2 OR mindte."langid" IS NULL ) AND MATCH( mindte."value" ) AGAINST( $3 IN BOOLEAN MODE )',
'internalcode' => ':site AND mindte."listtype" IN ($1)
AND ( mindte."langid" = $2 OR mindte."langid" IS NULL )
AND MATCH( mindte."value" ) AGAINST( $3 IN BOOLEAN MODE )',
'label' => 'Product texts, parameter(<list type code>,<language ID>,<search term>)',
'type' => 'float',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
Expand Down
4 changes: 3 additions & 1 deletion lib/mshoplib/src/MShop/Index/Manager/Text/PgSQL.php
Expand Up @@ -24,7 +24,9 @@ class PgSQL
private $searchConfig = array(
'index.text.relevance' => array(
'code' => 'index.text.relevance()',
'internalcode' => ':site AND mindte."listtype" = $1 AND ( mindte."langid" = $2 OR mindte."langid" IS NULL ) AND CAST( mindte."value" @@ to_tsquery( $3 ) AS integer )',
'internalcode' => ':site AND mindte."listtype" IN ($1)
AND ( mindte."langid" = $2 OR mindte."langid" IS NULL )
AND CAST( mindte."value" @@ to_tsquery( $3 ) AS integer )',
'label' => 'Product texts, parameter(<list type code>,<language ID>,<search term>)',
'type' => 'float',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
Expand Down
8 changes: 5 additions & 3 deletions lib/mshoplib/src/MShop/Index/Manager/Text/Standard.php
Expand Up @@ -35,7 +35,7 @@ class Standard
'code'=>'index.text.relevance()',
'internalcode'=>'( SELECT COUNT(DISTINCT mindte2."prodid")
FROM "mshop_index_text" AS mindte2
WHERE mpro."id" = mindte2."prodid" AND :site AND mindte2."listtype" = $1
WHERE mpro."id" = mindte2."prodid" AND :site AND mindte2."listtype" IN ($1)
AND ( mindte2."langid" = $2 OR mindte2."langid" IS NULL ) AND POSITION( $3 IN mindte2."value" ) > 0 )',
'label'=>'Product texts, parameter(<list type code>,<language ID>,<search term>)',
'type'=> 'float',
Expand All @@ -46,7 +46,7 @@ class Standard
'code'=>'sort:index.text.relevance()',
'internalcode'=>'( SELECT COUNT(DISTINCT mindte2."prodid")
FROM "mshop_index_text" AS mindte2
WHERE mpro."id" = mindte2."prodid" AND :site AND mindte2."listtype" = $1
WHERE mpro."id" = mindte2."prodid" AND :site AND mindte2."listtype" IN ($1)
AND ( mindte2."langid" = $2 OR mindte2."langid" IS NULL ) AND POSITION( $3 IN mindte2."value" ) > 0 )',
'label'=>'Product texts, parameter(<list type code>,<language ID>,<search term>)',
'type'=> 'float',
Expand All @@ -55,7 +55,9 @@ class Standard
),
'index.text.value' => array(
'code'=>'index.text.value()',
'internalcode'=>':site AND mindte."listtype" = $1 AND ( mindte."langid" = $2 OR mindte."langid" IS NULL ) AND mindte."type" = $3 AND mindte."domain" = $4 AND mindte."value"',
'internalcode'=>':site AND mindte."listtype" IN ($1)
AND ( mindte."langid" = $2 OR mindte."langid" IS NULL )
AND mindte."type" IN ($3) AND mindte."domain" = $4 AND mindte."value"',
'label'=>'Product text by type, parameter(<list type code>,<language ID>,<text type code>,<domain>)',
'type'=> 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
Expand Down
83 changes: 65 additions & 18 deletions lib/mshoplib/tests/MShop/Index/Manager/Text/StandardTest.php
Expand Up @@ -145,33 +145,64 @@ public function testGetSubManager()
public function testSearchItems()
{
$search = $this->object->createSearch();
$search->setConditions( $search->compare( '!=', 'index.text.id', null ) );

$result = $this->object->searchItems( $search, array() );

$this->assertGreaterThanOrEqual( 2, count( $result ) );
}


public function testSearchItemsTextId()
{
$textItems = self::$products['CNC']->getRefItems( 'text', 'name' );
if( ( $textItem = reset( $textItems ) ) === false ) {
throw new \RuntimeException( 'No text with type "name" available in product CNC' );
}

$search = $this->object->createSearch();
$search->setConditions( $search->compare( '==', 'index.text.id', $textItem->getId() ) );
$result = $this->object->searchItems( $search, array() );

$this->assertEquals( 1, count( $result ) );
}

$search->setConditions( $search->compare( '!=', 'index.text.id', null ) );

public function testSearchItemsRelevance()
{
$search = $this->object->createSearch();

$func = $search->createFunction( 'index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
$search->setConditions( $search->compare( '>', $func, 0 ) ); // text relevance

$sortfunc = $search->createFunction( 'sort:index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );

$result = $this->object->searchItems( $search, array() );

$this->assertGreaterThanOrEqual( 2, count( $result ) );
$this->assertEquals( 2, count( $result ) );
}


$func = $search->createFunction( 'index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
public function testSearchItemsRelevanceListtypes()
{
$search = $this->object->createSearch();

$func = $search->createFunction( 'index.text.relevance', array( ['default', 'unittype13'], 'de', 'Expr' ) );
$search->setConditions( $search->compare( '>', $func, 0 ) ); // text relevance

$sortfunc = $search->createFunction( 'sort:index.text.relevance', array( 'unittype13', 'de', 'Expr' ) );
$sortfunc = $search->createFunction( 'sort:index.text.relevance', array( ['default', 'unittype13'], 'de', 'Expr' ) );
$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );

$result = $this->object->searchItems( $search, array() );

$this->assertEquals( 2, count( $result ) );
}


public function testSearchItemsValue()
{
$search = $this->object->createSearch();

$func = $search->createFunction( 'index.text.value', array( 'unittype13', 'de', 'name', 'product' ) );
$search->setConditions( $search->compare( '~=', $func, 'Expr' ) ); // text value
Expand All @@ -185,24 +216,41 @@ public function testSearchItems()
}


public function testSearchTexts()
public function testSearchItemsValueListtypes()
{
$context = \TestHelperMShop::getContext();
$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( $context );
$search = $this->object->createSearch();

$search = $productManager->createSearch();
$conditions = array(
$search->compare( '==', 'product.code', 'CNC' ),
$search->compare( '==', 'product.editor', $this->editor )
);
$search->setConditions( $search->combine( '&&', $conditions ) );
$result = $productManager->searchItems( $search );
$func = $search->createFunction( 'index.text.value', array( ['default', 'unittype13'], 'de', 'name', 'product' ) );
$search->setConditions( $search->compare( '~=', $func, 'Expr' ) ); // text value

if( ( $product = reset( $result ) ) === false ) {
throw new \RuntimeException( 'No product found' );
}
$sortfunc = $search->createFunction( 'sort:index.text.value', array( ['default', 'unittype13'], 'de', 'name' ) );
$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );

$result = $this->object->searchItems( $search, array() );

$this->assertEquals( 1, count( $result ) );
}


public function testSearchItemsValueTypes()
{
$search = $this->object->createSearch();

$func = $search->createFunction( 'index.text.value', array( 'unittype13', 'de', ['code', 'name'], 'product' ) );
$search->setConditions( $search->compare( '~=', $func, 'Expr' ) ); // text value

$sortfunc = $search->createFunction( 'sort:index.text.value', array( 'default', 'de', ['code', 'name'] ) );
$search->setSortations( array( $search->sort( '+', $sortfunc ) ) );

$result = $this->object->searchItems( $search, array() );

$this->assertEquals( 1, count( $result ) );
}


public function testSearchTexts()
{
$context = \TestHelperMShop::getContext();
$langid = $context->getLocale()->getLanguageId();

$search = $this->object->createSearch();
Expand All @@ -214,7 +262,6 @@ public function testSearchTexts()

$result = $this->object->searchTexts( $search );

$this->assertArrayHasKey( $product->getId(), $result );
$this->assertContains( 'Cafe Noire Cappuccino', $result );
}

Expand Down

0 comments on commit 8d37307

Please sign in to comment.