<commit_before><?php

namespace Ojs\SiteBundle\Controller;

use Elastica\Aggregation;
use Elastica\Query;
use Elastica\ResultSet;
use Ojs\CoreBundle\Controller\OjsController as Controller;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;

class SearchController extends Controller
{
    /**
     * search page index controller
     *
     * @param Request $request
     * @param int $page
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction(Request $request, $page = 1)
    {
        $searchManager = $this->get('ojs_search_manager');

        $getRoles = $request->query->get('role_filters');
        $getSubjects = $request->query->get('subject_filters');
        $getJournals = $request->query->get('journal_filters');
        $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : [];
        $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
        $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : [];

        $queryType = $request->query->has('type') ? $request->get('type') : 'basic';

        $query = filter_var($request->get('q'), FILTER_SANITIZE_STRING);

        $section = filter_var($request->get('section'), FILTER_SANITIZE_STRING);

        $searcher = $this->get('fos_elastica.index.search');
        $searchQuery = new Query('_all');
        $boolQuery = new Query\Bool();
        $match = new Query\Match();
        $match->setField('status', 3);
        $boolQuery->addShould($match);

        $match = new Query\Match();
        $match->setField('published', true);
        $boolQuery->addShould($match);

        //set query according to query type
        if ($queryType == 'basic') {

            $fieldQuery = new Query\MultiMatch();
            $fieldQuery->setFields(['_all']);
            $fieldQuery->setQuery($query);
            $boolQuery->addMust($fieldQuery);
        } elseif ($queryType == 'advanced') {

            $parseQuery = $searchManager->parseSearchQuery($query);
            foreach ($parseQuery as $searchTerm) {
                $condition = $searchTerm['condition'];
                $advancedFieldQuery = new Query\MultiMatch();
                $advancedFieldQuery->setFields(
                    [$searchTerm['searchField']]
                );
                $advancedFieldQuery->setQuery($searchTerm['searchText']);
                if ($condition == 'AND') {
                    $boolQuery->addMust($advancedFieldQuery);
                } elseif ($condition == 'OR') {
                    $boolQuery->addShould($advancedFieldQuery);
                } elseif ($condition == 'NOT') {
                    $boolQuery->addMustNot($advancedFieldQuery);
                }
            }
        } elseif ($queryType == 'tag') {

            $matchQuery = new Query\Match();
            $matchQuery->setField('tags', $query);
            $boolQuery->addMust($matchQuery);
        }

        //set aggregations if requested
        if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters)) {

            foreach ($roleFilters as $role) {
                $match = new Query\Match();
                $match->setField('user.userJournalRoles.role.name', $role);
                $boolQuery->addMust($match);
            }

            foreach ($subjectFilters as $subject) {
                $match = new Query\Match();
                $match->setField('subjects', $subject);
                $boolQuery->addMust($match);
            }

            foreach ($journalFilters as $journal) {
                $match = new Query\Match();
                $match->setField('user.userJournalRoles.journal.title', $journal);
                $boolQuery->addMust($match);
            }
        }
        //set our boolean query
        $searchQuery->setQuery($boolQuery);
        //get all result
        $searchQuery->setSize(1000);

        //get role aggregation
        $roleAgg = new Aggregation\Terms('roles');
        $roleAgg->setField('userJournalRoles.role.name');
        $roleAgg->setOrder('_term', 'asc');
        $roleAgg->setSize(0);
        $searchQuery->addAggregation($roleAgg);

        //get subject aggregation
        $subjectAgg = new Aggregation\Terms('subjects');
        $subjectAgg->setField('subjects');
        $subjectAgg->setOrder('_term', 'asc');
        $subjectAgg->setSize(0);
        $searchQuery->addAggregation($subjectAgg);

        //get journal aggregation
        $journalAgg = new Aggregation\Terms('journals');
        $journalAgg->setField('journal.raw');
        $journalAgg->setOrder('_term', 'asc');
        $journalAgg->setSize(0);
        $searchQuery->addAggregation($journalAgg);
        /**
         * @var ResultSet $resultData
         */
        $resultData = $searcher->search($searchQuery);

        $roles = $resultData->getAggregation('roles')['buckets'];
        $subjects = $resultData->getAggregation('subjects')['buckets'];
        $journals = $resultData->getAggregation('journals')['buckets'];

        $match = new Query\Match();
        $match->setField('articles.journal.id', 2);
        $boolQuery->addMust($match);

        $match = new Query\Match();
        $match->setField('user.journalUsers.journal.id', 2);
        $boolQuery->addMust($match);


        if ($resultData->count() > 0) {
            /**
             * manipulate result data for easily use on template
             */
            $results = $searchManager->buildResultsObject($resultData, $section);
            /**
             * if search section is not defined or empty redirect to first result section
             */
            if (empty($section)) {
                $section = array_keys($results)[0];
                $redirectParams = array_merge($request->query->all(), ['section' => $section]);

                return $this->redirectToRoute('ojs_search_index', $redirectParams);
            } else {
                /**
                 * if section result is empty redirect to first that have result section
                 */
                if (!isset($results[$section])) {
                    foreach ($results as $resultKey => $result) {
                        if ($result['total_item'] > 0) {

                            $redirectParams = array_merge($request->query->all(), ['section' => $resultKey]);<commit_msg>Remove debug code<commit_after><?php

namespace Ojs\SiteBundle\Controller;

use Elastica\Aggregation;
use Elastica\Query;
use Elastica\ResultSet;
use Ojs\CoreBundle\Controller\OjsController as Controller;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;

class SearchController extends Controller
{
    /**
     * search page index controller
     *
     * @param Request $request
     * @param int $page
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction(Request $request, $page = 1)
    {
        $searchManager = $this->get('ojs_search_manager');

        $getRoles = $request->query->get('role_filters');
        $getSubjects = $request->query->get('subject_filters');
        $getJournals = $request->query->get('journal_filters');
        $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : [];
        $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
        $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : [];

        $queryType = $request->query->has('type') ? $request->get('type') : 'basic';

        $query = filter_var($request->get('q'), FILTER_SANITIZE_STRING);

        $section = filter_var($request->get('section'), FILTER_SANITIZE_STRING);

        $searcher = $this->get('fos_elastica.index.search');
        $searchQuery = new Query('_all');
        $boolQuery = new Query\Bool();
        $match = new Query\Match();
        $match->setField('status', 3);
        $boolQuery->addShould($match);

        $match = new Query\Match();
        $match->setField('published', true);
        $boolQuery->addShould($match);

        //set query according to query type
        if ($queryType == 'basic') {

            $fieldQuery = new Query\MultiMatch();
            $fieldQuery->setFields(['_all']);
            $fieldQuery->setQuery($query);
            $boolQuery->addMust($fieldQuery);
        } elseif ($queryType == 'advanced') {

            $parseQuery = $searchManager->parseSearchQuery($query);
            foreach ($parseQuery as $searchTerm) {
                $condition = $searchTerm['condition'];
                $advancedFieldQuery = new Query\MultiMatch();
                $advancedFieldQuery->setFields(
                    [$searchTerm['searchField']]
                );
                $advancedFieldQuery->setQuery($searchTerm['searchText']);
                if ($condition == 'AND') {
                    $boolQuery->addMust($advancedFieldQuery);
                } elseif ($condition == 'OR') {
                    $boolQuery->addShould($advancedFieldQuery);
                } elseif ($condition == 'NOT') {
                    $boolQuery->addMustNot($advancedFieldQuery);
                }
            }
        } elseif ($queryType == 'tag') {

            $matchQuery = new Query\Match();
            $matchQuery->setField('tags', $query);
            $boolQuery->addMust($matchQuery);
        }

        //set aggregations if requested
        if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters)) {

            foreach ($roleFilters as $role) {
                $match = new Query\Match();
                $match->setField('user.userJournalRoles.role.name', $role);
                $boolQuery->addMust($match);
            }

            foreach ($subjectFilters as $subject) {
                $match = new Query\Match();
                $match->setField('subjects', $subject);
                $boolQuery->addMust($match);
            }

            foreach ($journalFilters as $journal) {
                $match = new Query\Match();
                $match->setField('user.userJournalRoles.journal.title', $journal);
                $boolQuery->addMust($match);
            }
        }
        //set our boolean query
        $searchQuery->setQuery($boolQuery);
        //get all result
        $searchQuery->setSize(1000);

        //get role aggregation
        $roleAgg = new Aggregation\Terms('roles');
        $roleAgg->setField('userJournalRoles.role.name');
        $roleAgg->setOrder('_term', 'asc');
        $roleAgg->setSize(0);
        $searchQuery->addAggregation($roleAgg);

        //get subject aggregation
        $subjectAgg = new Aggregation\Terms('subjects');
        $subjectAgg->setField('subjects');
        $subjectAgg->setOrder('_term', 'asc');
        $subjectAgg->setSize(0);
        $searchQuery->addAggregation($subjectAgg);

        //get journal aggregation
        $journalAgg = new Aggregation\Terms('journals');
        $journalAgg->setField('journal.raw');
        $journalAgg->setOrder('_term', 'asc');
        $journalAgg->setSize(0);
        $searchQuery->addAggregation($journalAgg);
        /**
         * @var ResultSet $resultData
         */
        $resultData = $searcher->search($searchQuery);

        $roles = $resultData->getAggregation('roles')['buckets'];
        $subjects = $resultData->getAggregation('subjects')['buckets'];
        $journals = $resultData->getAggregation('journals')['buckets'];
        
        if ($resultData->count() > 0) {
            /**
             * manipulate result data for easily use on template
             */
            $results = $searchManager->buildResultsObject($resultData, $section);
            /**
             * if search section is not defined or empty redirect to first result section
             */
            if (empty($section)) {
                $section = array_keys($results)[0];
                $redirectParams = array_merge($request->query->all(), ['section' => $section]);

                return $this->redirectToRoute('ojs_search_index', $redirectParams);
            } else {
                /**
                 * if section result is empty redirect to first that have result section
                 */
                if (!isset($results[$section])) {
                    foreach ($results as $resultKey => $result) {
                        if ($result['total_item'] > 0) {

                            $redirectParams = array_merge($request->query->all(), ['section' => $resultKey]);
{'fraction_non_alphanumeric': 0.14868967721316714, 'fraction_numerical': 0.0020773410035155003, 'mean_word_length': 2.195557824865969, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, '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': '6608016', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3291, 'n_words': 613}