Road to elasticsearch

About me

Benjamin Cremer

Benjamin Cremer
shopware AG
Follow me @benjamincremer

Questions?

Feel free to ask at any time

Give Feedback please

joind.in/event/view/3800

joind.in/15057

shopware AG

AntiDemo

Kick

Problems

  • Duplicate Code
  • Tight Coupling to Persistence layer
  • Tight Coupling to framework
  • Primitive extensibility
  • Big Queries that select too much
  • Performance / Scale issues
  • Undocumented Arrays with minor differences

        ProductNumber < ListProduct < Product
    
ProductNumber Product Identification
ListProduct Basic Product Data (Category Listing, Related Products)
Product Full Product data, contains ListProducts (Product Detail Page)

Separation of concerns

  • Search Definition
  • Find product numbers
  • Fetch Product data by product number
  • Bridge to framework

Search Definition

  • Description of what to search for
  • You will find no WHERE or INNER JOIN here
  • Lets call that "Criteria"

Criteria


$criteria = new Criteria();
    

$criteria->addCondition(
    new HasFreeShippingCondition()
);
    

$criteria->addCondition(
    new SearchTermCondition('tee')
);
    

$criteria->addCondition(
    new PriceRangeCondition(5, 50)
);
    

$criteria->addSorting(
    new PriceSorting(SortingInterface::SORT_ASC)
);
    

$criteria->limit(5);
$criteria->offset(0);
    

Condition


class PriceRangeCondition implements ConditionInterface
{
    /**
     * @param float $minPrice
     * @param float $maxPrice
     */
    public function __construct($minPrice, $maxPrice)
    {
        Assertion::float($minPrice); // see github.com/beberlei/assert
        Assertion::float($maxPrice);

        $this->minPrice = $minPrice;
        $this->maxPrice = $maxPrice;
    }

    public function getMinPrice() {};
    public function getMaxPrice() {};
}
    

Condition

  • Value Object
  • Immutable
  • Only scalars
  • Domain Specific
  • Ensures basic consistency
  • We can easily persist that
  • Search Definition ✔
  • Find Product Numbers?

Context

  • Global shop state
  • User / Runtime dependent information
  • Session / Cookie

$context = $contextFactory->createProductContext();
    

$context->getShopId(); // 1
$context->getCustomerLocale(); // de_DE
$context->getLowTaxRate(); // 7.0
    

Search Interface


interface ProductNumberSearchInterface
{
    /**
     * @return ProductNumberSearchResult
     */
    public function search(Criteria $criteria, ContextInterface $context);
}
    

Search


$criteria = Criteria();
$criteria->addCondition(new SearchTermCondition('tee'));
$criteria->limit(5);
[..]
$context = [..]
    

$search = $container->get(ProductNumberSearchInterface::class);

/** @var ProductNumberSearchResult $result */
$result = $search->search($criteria, $context);
    

echo $result->getTotalCount();

/** @var ProductNumber $product */
foreach ($result->getProducts() as $product) {
    echo $product->getNumber();
}
    
  • Search Definition ✔
  • Find Product Numbers ✔
  • Fetch Product Data ?

Fetch Product Data


interface ListProductServiceInterface
{
    /**
     * @return Struct\ListProduct
     */
    public function get($number, ContextInterface $context);

    /**
     * @return Struct\ListProduct[] Indexed by the products order number
     */
    public function getList(array $numbers, ContextInterface $context);
}
    

interface ProductServiceInterface
{
    /**
     * @return Struct\Product
     */
    public function get($number, ContextInterface $context);

    /**
     * @return Struct\Product[] Indexed by the products order number
     */
    public function getList($numbers, ContextInterface $context);
}
    

$productListService = $container->get(ListProductServiceInterface::class);

/** @var ListProduct $product */
$product = $productListService->get('sw-555', $context);

/** @var ListProduct[] $products */
$products = $productListService->getList(['sw-555', 'sw-111'], $context);
    
  • Search Definition ✔
  • Find Product Numbers ✔
  • Fetch Product Data ✔
  • Combine both?

/** @var ProductNumberSearchResult $searchResult */
$searchResult = $this->productNumberSearch->search($condition, $context);

$productListService = $container->get(ListProductServiceInterface::class);
    

$listProducts = [];
foreach ($result->getProducts() as $product) {
    $listProducts = $productListService->get($product->getNumber(), $context);
}
    

$listProducts = $productListService->getList(
    array_keys($result->getProducts()),
    $context
);
    

interface ProductSearchInterface
{
    /**
     * @return ProductSearchResult
     */
    public function search(Criteria $criteria, ContextInterface $context);
}
    

class ProductSearch implements ProductSearchInterface
{
    public function __construct(
        ProductNumberSearchInterface $productSearch  // Doctrine DBAL
        ListProductServiceInterface $productService, // Doctrine DBAL
    );

    /**
     * @return ProductSearchResult
     */
    public function search(Criteria $criteria, ContextInterface $context);
}
        

class ProductSearch implements ProductSearchInterface
{
    public function __construct(
        ProductNumberSearchInterface $productSearch  // Elasticsearch
        ListProductServiceInterface $productService, // Doctrine DBAL
    );

    /**
     * @return ProductSearchResult
     */
    public function search(Criteria $criteria, ContextInterface $context);
}
        

class ProductSearch implements ProductSearchInterface
{
    public function __construct(
        ProductNumberSearchInterface $productSearch  // Elasticsearch
        ListProductServiceInterface $productService, // Elasticsearch
    );

    /**
     * @return ProductSearchResult
     */
    public function search(Criteria $criteria, ContextInterface $context);
}
        

$condition = [..];
$context   = [..];

$search = $container->get(ProductSearchInterface::class);

$result = $search->search($criteria, $context);

echo "Totalcount: ". $result->getTotalCount(). "\n";

/** @var ListProduct $product */
foreach ($result->getProducts() as $product) {
    echo $product->getName();
};
    
  • Search Definition ✔
  • Find Product Numbers ✔
  • Fetch Product Data ✔
  • Combine both ✔

Implementation

Doctrine DBAL

  • Doctrine\DBAL\Query\QueryBuilder
  • You know doctrine anyway...

interface ConditionHandlerInterface
{
    /**
     * @param ConditionInterface $condition
     * @return bool
     */
    public function supportsCondition(ConditionInterface $condition);

    /**
     * @param ConditionInterface $condition
     * @param QueryBuilder $query
     * @param ContextInterface $context
     */
    public function generateCondition(
        ConditionInterface $condition,
        QueryBuilder $query,
        ContextInterface $context
    );
}
    

class ManufacturerConditionHandler implements ConditionHandlerInterface
{
    public function supportsCondition(ConditionInterface $condition)
    {
        return ($condition instanceof ManufacturerCondition);
    }

    public function generateCondition(
        ConditionInterface $condition,
        QueryBuilder $query,
        ContextInterface $context
    ) {
        /* @var $condition ManufacturerCondition */

        $query->innerJoin(
            'product',
            's_articles_manufacturer',
            'manufacturer',
            'manufacturer.id = product.manufacturerID AND product.manufacturerID IN (:manufacturer)'
        );

        $query->setParameter(
            ':manufacturer',
            $condition->getManufacturerIds(),
            Connection::PARAM_INT_ARRAY
        );
    }
}
    

class ProductNameSortingHandler implements SortingHandlerInterface
{
    public function supportsSorting(SortingInterface $sorting)
    {
        return ($sorting instanceof ProductNameSorting);
    }

    public function generateSorting(
        SortingInterface $sorting,
        QueryBuilder $query,
        ContextInterface $context
    ) {
        $query->addOrderBy('product.name', $sorting->getDirection())
              ->addOrderBy('product.id', $sorting->getDirection());
    }
}

    

Elasticsearch

“Elasticsearch is a search server based on Lucene. It provides a distributed, multitenant-capable full-text search engine with a RESTful web interface and schema-free JSON documents.”

https://en.wikipedia.org/wiki/Elasticsearch

Elasticsearch

  • Scalable search
  • Cross-platform
  • Open Source (Apache License)
  • JSON based Query Language

ElasticsearchDSL

  • Integrated in Shopware 5.1 (CE)
  • Shopware/Bundle/SearchBundle
  • Shopware/Bundle/SearchBundleDBAL

  • Shopware/Bundle/SearchBundleES
    NCLOC: 2616
  • Shopware/Bundle/ESIndexingBundle
    NCLOC: 2772

Architecture

tideways.io/profiler/blog/high-performance-shopware-51-with-elasticsearch

Shopware/Bundle/SearchBundleES


class SalesConditionHandler implements HandlerInterface
{
    public function supports(CriteriaPartInterface $criteriaPart)
    {
        return ($criteriaPart instanceof SalesCondition);
    }

    public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ContextInterface $context
    ) {
        $filter = new RangeFilter(
            'sales',
            ['gt' => $criteriaPart->getMinSales()]
        );

        $search->addFilter($filter);
    }
}
    

Shopware/Bundle/ESIndexingBundle

$ ./bin/console sw:es:index:populate

$ ./bin/console sw:es:backlog:sync

Need for speed

Performance gains

100.000 Producs / 100 Categories

MySQL

Elasticsearch

tideways.io/profiler/blog/high-performance-shopware-51-with-elasticsearch

Recap

Questions?

PHP Usergroup Münster

meetup.com/phpugms
@phpugms
November 12, 2015

Thank you!

Please give feedback

joind.in/15057

@benjamincremer

One More