<commit_before><?php

namespace Application\Bundle\FrontBundle\SphinxSearch;

use Application\Bundle\FrontBundle\SphinxSearch\SphinxFields;
use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Helper;
use Foolz\SphinxQL\Connection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Session\Session;

class SphinxSearch
{

    private $conn;
    private $indexName = 'records';
    private $recordId;
    private $entityManager = null;
    private $recordTypeId;

    public function __construct(EntityManager $entityManager, $recordId = null, $recordTypeId = null)
    {
        $this->entityManager = $entityManager;
        $this->recordId = $recordId;
        $this->recordTypeId = $recordTypeId;

        $this->conn = new Connection();
        $this->conn->setParams(array('host' => 'localhost', 'port' => '9306'));
        $this->conn->silenceConnectionWarning(true);
    }

    public function insert()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->insert()->into($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function update()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->update($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function replace()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->replace()->into($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function select($offset = 0, $limit = 100, $sortColumn = 'title', $sortOrder = 'asc', $criteria = null)
    {
        $sq = SphinxQL::create($this->conn)
                ->select()
                ->from($this->indexName)
//        if ($criteria) {
//            $this->whereClause($criteria, $sq);
//        }
       ->match('s_media_type', 'Audio', true)
       ->orderBy($sortColumn, $sortOrder)
                ->limit($offset, $limit)
                ->enqueue(Helper::create($this->conn)->showMeta());
        return $sq->executeBatch();
    }

    public function facetSelect($facetColumn)
    {
        $sq = SphinxQL::create($this->conn)
                ->select($facetColumn, SphinxQL::expr('count(*) AS total'))
                ->from($this->indexName)
                ->groupBy($facetColumn)
                ->orderBy($facetColumn, 'asc');


        return $sq->execute();
    }

    public function whereClause($criteria, $sq)
    {
        if ($criteria['mediaType']) {
//            $sq->where('media_type', 'IN', $criteria['mediaType']);
            foreach($criteria['mediaType'] as $key => $value) {
                $sq->match('s_media_type', $value, true);
            }
        }
    }

}<commit_msg>facets<commit_after><?php

namespace Application\Bundle\FrontBundle\SphinxSearch;

use Application\Bundle\FrontBundle\SphinxSearch\SphinxFields;
use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Helper;
use Foolz\SphinxQL\Connection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Session\Session;

class SphinxSearch
{

    private $conn;
    private $indexName = 'records';
    private $recordId;
    private $entityManager = null;
    private $recordTypeId;

    public function __construct(EntityManager $entityManager, $recordId = null, $recordTypeId = null)
    {
        $this->entityManager = $entityManager;
        $this->recordId = $recordId;
        $this->recordTypeId = $recordTypeId;

        $this->conn = new Connection();
        $this->conn->setParams(array('host' => 'localhost', 'port' => '9306'));
        $this->conn->silenceConnectionWarning(true);
    }

    public function insert()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->insert()->into($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function update()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->update($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function replace()
    {
        $sphinxFields = new SphinxFields();
        $data = $sphinxFields->prepareFields($this->entityManager, $this->recordId, $this->recordTypeId);
        $sq = SphinxQL::create($this->conn)->replace()->into($this->indexName);
        $sq->set($data);
        return $sq->execute();
    }

    public function select($offset = 0, $limit = 100, $sortColumn = 'title', $sortOrder = 'asc', $criteria = null)
    {
//        $sq = SphinxQL::create($this->conn)
//                ->select()
//                ->from($this->indexName)
//                ->match('s_media_type', 'Audio', true)
//                ->orderBy($sortColumn, $sortOrder)
//                ->limit($offset, $limit)
//                ->enqueue(Helper::create($this->conn)->showMeta());
        $sq = SphinxQL::create($this->conn);
                $sq->select()
                ->from($this->indexName);
        if ($criteria) {
            $this->whereClause($criteria, $sq);
        }
        $sq->orderBy($sortColumn, $sortOrder)
                ->limit($offset, $limit)
                ->enqueue(Helper::create($this->conn)->showMeta());
        return $sq->executeBatch();
    }

    public function facetSelect($facetColumn)
    {
        $sq = SphinxQL::create($this->conn)
                ->select($facetColumn, SphinxQL::expr('count(*) AS total'))
                ->from($this->indexName)
                ->groupBy($facetColumn)
                ->orderBy($facetColumn, 'asc');


        return $sq->execute();
    }

    public function whereClause($criteria, $sq)
    {
        if ($criteria['mediaType']) {
//            $sq->where('media_type', 'IN', $criteria['mediaType']);
            foreach ($criteria['mediaType'] as $key => $value) {
                $sq->match('s_media_type', $value, true);
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.16938712657838004, 'fraction_numerical': 0.0024638127502309825, 'mean_word_length': 2.5667215815485998, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532099', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1955, 'n_words': 324}