Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Match.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
12 
13 class Match implements QueryInterface
14 {
18  const QUERY_CONDITION_MUST_NOT = 'must_not';
19 
23  private $fieldMapper;
24 
29 
34  public function __construct(
35  FieldMapperInterface $fieldMapper,
37  ) {
38  $this->fieldMapper = $fieldMapper;
39  $this->preprocessorContainer = $preprocessorContainer;
40  }
41 
45  public function build(array $selectQuery, RequestQueryInterface $requestQuery, $conditionType)
46  {
47  $queryValue = $this->prepareQuery($requestQuery->getValue(), $conditionType);
48  $queries = $this->buildQueries($requestQuery->getMatches(), $queryValue);
49  $requestQueryBoost = $requestQuery->getBoost() ?: 1;
50  foreach ($queries as $query) {
51  $queryBody = $query['body'];
52  $matchKey = isset($queryBody['match_phrase']) ? 'match_phrase' : 'match';
53  foreach ($queryBody[$matchKey] as $field => $matchQuery) {
54  $matchQuery['boost'] = $requestQueryBoost + $matchQuery['boost'];
55  $queryBody[$matchKey][$field] = $matchQuery;
56  }
57  $selectQuery['bool'][$query['condition']][] = $queryBody;
58  }
59 
60  return $selectQuery;
61  }
62 
68  protected function prepareQuery($queryValue, $conditionType)
69  {
70  $queryValue = $this->escape($queryValue);
71  foreach ($this->preprocessorContainer as $preprocessor) {
72  $queryValue = $preprocessor->process($queryValue);
73  }
74  $condition = $conditionType === BoolExpression::QUERY_CONDITION_NOT ?
75  self::QUERY_CONDITION_MUST_NOT : $conditionType;
76  return [
77  'condition' => $condition,
78  'value' => $queryValue,
79  ];
80  }
81 
96  protected function buildQueries(array $matches, array $queryValue)
97  {
98  $conditions = [];
99 
100  // Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
101  $count = 0;
102  $value = preg_replace('#^\\\\"(.*)\\\\"$#m', '$1', $queryValue['value'], -1, $count);
103  $condition = ($count) ? 'match_phrase' : 'match';
104 
105  foreach ($matches as $match) {
106  $resolvedField = $this->fieldMapper->getFieldName(
107  $match['field'],
108  ['type' => FieldMapperInterface::TYPE_QUERY]
109  );
110  $conditions[] = [
111  'condition' => $queryValue['condition'],
112  'body' => [
113  $condition => [
114  $resolvedField => [
115  'query' => $value,
116  'boost' => isset($match['boost']) ? $match['boost'] : 1,
117  ],
118  ],
119  ],
120  ];
121  }
122 
123  return $conditions;
124  }
125 
135  protected function escape($value)
136  {
137  $value = preg_replace('/@+|[@+-]+$/', '', $value);
138 
139  $pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
140  $replace = '\\\$1';
141 
142  return preg_replace($pattern, $replace, $value);
143  }
144 }
$pattern
Definition: website.php:22
$count
Definition: recent.phtml:13
__construct(FieldMapperInterface $fieldMapper, array $preprocessorContainer)
Definition: Match.php:34
buildQueries(array $matches, array $queryValue)
Definition: Match.php:96
$value
Definition: gender.phtml:16
build(array $selectQuery, RequestQueryInterface $requestQuery, $conditionType)
Definition: Match.php:45