<commit_before><?php
namespace Scat;

use PDO;

class SearchService
{
  private $pdo;

  public function __construct(array $config) {
    $this->pdo= new PDO($config['dsn'], $config['user'], $config['pass']);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  }

  public function search($q) {
    return [
      'items' => $this->searchItems($q),
      'products' => $this->searchProducts($q)
    ];
  }

  public function searchItems($q) {
    $scanner= new \OE\Lukas\Parser\QueryScanner();
    $parser= new \OE\Lukas\Parser\QueryParser($scanner);
    $parser->readString($q);
    $query= $parser->parse();

    $v= new \Scat\SearchVisitor();
    $query->accept($v);

    $items= \Model::factory('Item')->select('item.*')
                                   ->left_outer_join('brand',
                                                     array('brand.id', '=',
                                                           'item.brand'))
                                   ->left_outer_join('barcode',
                                                     array('barcode.item', '=',
                                                           'item.id'))
                                   ->where_raw($v->where_clause())
                                   ->where_gte('item.active',
                                               $v->force_all ? 0 : 1)
                                   ->where_not_equal('item.deleted', 1)
                                   ->group_by('item.id')
                                   ->order_by_asc('item.code')
                                   ->find_many();

    return $items;
  }

  public function searchProducts($terms) {
    // This should rank products for which we stock items higher
    $q= "SELECT id, WEIGHT() weight<commit_msg>Capture/log errors from searching<commit_after><?php
namespace Scat;

use PDO;

class SearchService
{
  private $pdo;

  public function __construct(array $config) {
    $this->pdo= new PDO($config['dsn'], $config['user'], $config['pass']);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  }

  public function search($q) {
    $items= $products= $errors= [];
    try {
      $items= $this->searchItems($q);
      # / trips up SphinxSearch parser, but we like to use it
      $q= preg_replace('#([/])#', '\\/', $q);
      $products= $this->searchProducts($q);
    } catch (\Exception $e) {
      $errors[]= $e->getMessage();
    }
    return [
      'items' => $items,
      'products' => $products,
      'errors' => $errors,
    ];
  }

  public function searchItems($q) {
    $scanner= new \OE\Lukas\Parser\QueryScanner();
    $parser= new \OE\Lukas\Parser\QueryParser($scanner);
    $parser->readString($q);
    $query= $parser->parse();

    if (!$query) {
      $feedback= $parser->getFeedback();
      foreach ($feedback as $msg) {
        error_log($msg);
      }
      throw new \Exception($msg);
    }

    $v= new \Scat\SearchVisitor();
    $query->accept($v);

    $items= \Model::factory('Item')->select('item.*')
                                   ->left_outer_join('brand',
                                                     array('brand.id', '=',
                                                           'item.brand'))
                                   ->left_outer_join('barcode',
                                                     array('barcode.item', '=',
                                                           'item.id'))
                                   ->where_raw($v->where_clause())
                                   ->where_gte('item.active',
                                               $v->force_all ? 0 : 1)
                                   ->where_not_equal('item.deleted', 1)
                                   ->group_by('item.id')
                                   ->order_by_asc('item.code')
                                   ->find_many();

    return $items;
  }

  public function searchProducts($terms) {
    // This should rank products for which we stock items higher
    $q= "SELECT id, WEIGHT() weight
{'fraction_non_alphanumeric': 0.14878566375854752, 'fraction_numerical': 0.0014147606696533837, 'mean_word_length': 1.4351320321469576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236122', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1139, 'n_words': 210}