I have sku like this '1-83574148-0'.
when I searched with keyword '1835741480' . It's no result .
How can I search by remove ' - ' from my SKU ?
I had this issue before, after i did some analyses I found that the best approach for this to add new attribute to product, this attribute should be searchable, then I saved the SKU without "-" to this attribute.
In this case you don not need to write any code and only you will use Magento standard.
Try rewriting the below file where the search filter happens,
In di.xml,
<preference for="Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match"
type="Namespace\Modulename\Model\Search\QueryBuilder"/>
Model file,
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Namespace\Modulename\Model\Search;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match;
use Magento\Framework\DB\Helper\Mysql\Fulltext;
use Magento\Framework\DB\Select;
use Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface;
use Magento\Framework\Search\Adapter\Mysql\Field\ResolverInterface;
use Magento\Framework\Search\Adapter\Mysql\ScoreBuilder;
use Magento\Framework\Search\Request\Query\BoolExpression;
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
use Magento\Framework\Search\Adapter\Preprocessor\PreprocessorInterface;
class QueryBuilder extends Match
{
const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';
const MINIMAL_CHARACTER_LENGTH = 3;
/**
* @var string[]
*/
private $replaceSymbols = [];
/**
* @var Http
*/
private $request;
/**
* @var ResolverInterface
*/
private $resolver;
/**
* @var Fulltext
*/
private $fulltextHelper;
/**
* @var string
*/
private $fulltextSearchMode;
/**
* @var PreprocessorInterface[]
*/
protected $preprocessors;
/**
* @param Http $request
* @param ResolverInterface $resolver
* @param Fulltext $fulltextHelper
* @param string $fulltextSearchMode
* @param PreprocessorInterface[] $preprocessors
*/
public function __construct(
Http $request,
ResolverInterface $resolver,
Fulltext $fulltextHelper,
$fulltextSearchMode = Fulltext::FULLTEXT_MODE_BOOLEAN,
array $preprocessors = []
) {
$this->resolver = $resolver;
$this->replaceSymbols = str_split(self::SPECIAL_CHARACTERS, 1);
$this->fulltextHelper = $fulltextHelper;
$this->fulltextSearchMode = $fulltextSearchMode;
$this->preprocessors = $preprocessors;
$this->request = $request;
parent::__construct($resolver,$fulltextHelper,$fulltextSearchMode,$preprocessors);
}
/**
* {@inheritdoc}
*/
public function build(
ScoreBuilder $scoreBuilder,
Select $select,
RequestQueryInterface $query,
$conditionType
) {
/* Code customization */
}
}