/** * {@inheritdoc} */ public function build(ScoreBuilder $scoreBuilder, Select $select, RequestQueryInterface $query, $conditionType) { /** @var $query \Magento\Framework\Search\Request\Query\Match */ $queryValue = $this->prepareQuery($query->getValue(), $conditionType); $fieldList = []; foreach ($query->getMatches() as $match) { $fieldList[] = $match['field']; } $resolvedFieldList = $this->resolver->resolve($fieldList); $fieldIds = []; $columns = []; foreach ($resolvedFieldList as $field) { if ($field->getType() === FieldInterface::TYPE_FULLTEXT && $field->getAttributeId()) { $fieldIds[] = $field->getAttributeId(); } $column = $field->getColumn(); $columns[$column] = $column; } $matchQuery = $this->fulltextHelper->getMatchQuery($columns, $queryValue, $this->fulltextSearchMode); $scoreBuilder->addCondition($matchQuery, true); if ($fieldIds) { $matchQuery = sprintf('(%s AND search_index.attribute_id IN (%s))', $matchQuery, implode(',', $fieldIds)); } $select->where($matchQuery); return $select; }
/** * Custom load model: Get data by user query phrase and store view id * * @param SynReaderModel $object * @param string $value * @return $this */ public function loadByPhrase(SynReaderModel $object, $value) { $phrase = strtolower($value); $matchQuery = $this->fullTextSelect->getMatchQuery(['synonyms' => 'synonyms'], $phrase, Fulltext::FULLTEXT_MODE_BOOLEAN); $query = $this->getConnection()->select()->from($this->getMainTable())->where('store_id = ?', $object->getStoreViewId())->where($matchQuery); $rows = $this->getConnection()->fetchAll($query); $object->setData($rows); $this->_afterLoad($object); return $this; }
public function testBuildQuery() { /** @var Select|\PHPUnit_Framework_MockObject_MockObject $select */ $select = $this->getMockBuilder('Magento\Framework\DB\Select') ->setMethods(['getMatchQuery', 'match', 'where']) ->disableOriginalConstructor() ->getMock(); $this->fulltextHelper->expects($this->once()) ->method('getMatchQuery') ->with($this->equalTo(['some_field' => 'some_field']), $this->equalTo('-some_value*')) ->will($this->returnValue('matchedQuery')); $select->expects($this->once()) ->method('where') ->with('matchedQuery') ->willReturnSelf(); $this->resolver->expects($this->once()) ->method('resolve') ->willReturnCallback(function ($fieldList) { $resolvedFields = []; foreach ($fieldList as $column) { $field = $this->getMockBuilder('\Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface') ->disableOriginalConstructor() ->getMockForAbstractClass(); $field->expects($this->any()) ->method('getColumn') ->willReturn($column); $resolvedFields[] = $field; } return $resolvedFields; }); /** @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject $query */ $query = $this->getMockBuilder('Magento\Framework\Search\Request\Query\Match') ->setMethods(['getMatches', 'getValue']) ->disableOriginalConstructor() ->getMock(); $query->expects($this->once()) ->method('getValue') ->willReturn('some_value '); $query->expects($this->once()) ->method('getMatches') ->willReturn([['field' => 'some_field']]); $this->scoreBuilder->expects($this->once()) ->method('addCondition'); $result = $this->match->build($this->scoreBuilder, $select, $query, Bool::QUERY_CONDITION_NOT); $this->assertEquals($select, $result); }
/** * {@inheritdoc} */ public function build(ScoreBuilder $scoreBuilder, Select $select, RequestQueryInterface $query, $conditionType) { /** @var $query \Magento\Framework\Search\Request\Query\Match */ $queryValue = $this->prepareQuery($query->getValue(), $conditionType); $fieldList = []; foreach ($query->getMatches() as $match) { $fieldList[] = $match['field']; } $resolvedFieldList = $this->resolver->resolve($fieldList); $queryBoost = $query->getBoost(); $scoreBuilder->addCondition($this->fulltextHelper->getMatchQuery($resolvedFieldList, $queryValue, Fulltext::FULLTEXT_MODE_BOOLEAN), $queryBoost !== null ? $queryBoost : 1); $select = $this->fulltextHelper->match($select, $resolvedFieldList, $queryValue, true, Fulltext::FULLTEXT_MODE_BOOLEAN); return $select; }
public function testBuildQuery() { $boost = 3.14; /** @var Select|\PHPUnit_Framework_MockObject_MockObject $select */ $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->setMethods(['getMatchQuery', 'match'])->disableOriginalConstructor()->getMock(); $this->fulltextHelper->expects($this->once())->method('getMatchQuery')->with($this->equalTo(['some_field']), $this->equalTo('-some_value*'))->will($this->returnValue('matchedQuery')); $this->fulltextHelper->expects($this->once())->method('match')->withConsecutive([$select, ['some_field'], '-some_value*', true, Fulltext::FULLTEXT_MODE_BOOLEAN])->willReturn($select); $this->resolver->expects($this->once())->method('resolve')->willReturnArgument(0); /** @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject $query */ $query = $this->getMockBuilder('Magento\\Framework\\Search\\Request\\Query\\Match')->setMethods(['getMatches', 'getValue', 'getBoost'])->disableOriginalConstructor()->getMock(); $query->expects($this->once())->method('getValue')->willReturn('some_value '); $query->expects($this->once())->method('getBoost')->willReturn($boost); $query->expects($this->once())->method('getMatches')->willReturn([['field' => 'some_field']]); $this->scoreBuilder->expects($this->once())->method('addCondition'); $result = $this->match->build($this->scoreBuilder, $select, $query, Bool::QUERY_CONDITION_NOT); $this->assertEquals($select, $result); }
/** * A helper function to query by phrase and get results * * @param string $phrase * @return array */ private function queryByPhrase($phrase) { $matchQuery = $this->fullTextSelect->getMatchQuery(['synonyms' => 'synonyms'], $phrase, Fulltext::FULLTEXT_MODE_BOOLEAN); $query = $this->getConnection()->select()->from($this->getMainTable())->where($matchQuery); return $this->getConnection()->fetchAll($query); }