<commit_before><?php

namespace G4\DataMapper\Engine\Elasticsearch;

use G4\DataMapper\Common\Selection\Comparison;
use G4\DataMapper\Common\SelectionFactoryInterface;
use G4\DataMapper\Common\Selection\Sort;
use G4\DataMapper\Common\IdentityInterface;

class ElasticsearchSelectionFactory implements SelectionFactoryInterface
{
    /**
     * @var IdentityInterface
     */
    private $identity;

    public function __construct(ElasticsearchIdentityInterface $identity)
    {
        $this->identity = $identity;
    }

    public function fieldNames()
    {
        $fieldNames = $this->identity->getFieldNames();

        return count($fieldNames) === 0
            ? []
            : $fieldNames;
    }

    public function group()
    {
        return ['group_by_' . $this->identity->getGrouping() => [ 'terms' => [ 'field' => $this->identity->getGrouping()]]];
    }

    public function limit()
    {
        return (int) $this->identity->getLimit();
    }

    public function offset()
    {
        return (int) $this->identity->getOffset();
    }

    public function sort()
    {
        $rawSorting = $this->identity->getSorting();

        if (empty($rawSorting)) {
            return [];
        }

        $sorting = [];

        foreach ($rawSorting as $oneSort) {
            if ($oneSort instanceof Sort) {
                $sorting[] = $oneSort->getSort($this->makeSortingFormatter());
            }
        }

        $sorting = array_merge((new ElasticsearchGeodistSort($this->identity))->sort(), $sorting);

        return $sorting;
    }

    public function where()
    {
        if ($this->identity->isVoid()) {
            return ['bool' => ['must' => ['match_all' => []]]];
        }

        $comparisons = [];

        foreach ($this->identity->getComparisons() as $oneComparison) {
            if ($oneComparison instanceof Comparison && !$oneComparison->getValue()->isEmpty()) {

                $comparisons[]= $oneComparison->getComparison($this->makeComparisonFormatter());
            }
        }

        if($this->identity->hasRawQuery()) {
            $comparisons[]= $this->identity->getRawQuery();
        }

        if (empty($comparisons)) {
            $comparisons =  ['match_all' => []];
        }

        return ['bool' => ['must' => $comparisons, 'filter' => (new ElasticsearchGeodistFormatter($this->identity))->format()]];
    }

    private function makeComparisonFormatter()
    {
        return new ElasticsearchComparisonFormatter();
    }

    private function makeSortingFormatter()
    {
        return new ElasticsearchSortingFormatter();
    }
}
<commit_msg>000 - Modified where method to accept 'not equal' comparisons.<commit_after><?php

namespace G4\DataMapper\Engine\Elasticsearch;

use G4\DataMapper\Common\Selection\Comparison;
use G4\DataMapper\Common\SelectionFactoryInterface;
use G4\DataMapper\Common\Selection\Sort;
use G4\DataMapper\Common\IdentityInterface;

class ElasticsearchSelectionFactory implements SelectionFactoryInterface
{
    /**
     * @var IdentityInterface
     */
    private $identity;

    public function __construct(ElasticsearchIdentityInterface $identity)
    {
        $this->identity = $identity;
    }

    public function fieldNames()
    {
        $fieldNames = $this->identity->getFieldNames();

        return count($fieldNames) === 0
            ? []
            : $fieldNames;
    }

    public function group()
    {
        return ['group_by_' . $this->identity->getGrouping() => [ 'terms' => [ 'field' => $this->identity->getGrouping()]]];
    }

    public function limit()
    {
        return (int) $this->identity->getLimit();
    }

    public function offset()
    {
        return (int) $this->identity->getOffset();
    }

    public function sort()
    {
        $rawSorting = $this->identity->getSorting();

        if (empty($rawSorting)) {
            return [];
        }

        $sorting = [];

        foreach ($rawSorting as $oneSort) {
            if ($oneSort instanceof Sort) {
                $sorting[] = $oneSort->getSort($this->makeSortingFormatter());
            }
        }

        $sorting = array_merge((new ElasticsearchGeodistSort($this->identity))->sort(), $sorting);

        return $sorting;
    }

    //TODO:Vladan:It is too complicated.
    public function where()
    {
        if ($this->identity->isVoid()) {
            return ['bool' => ['must' => ['match_all' => []]]];
        }

        $comparisons = [];

        foreach ($this->identity->getComparisons() as $oneComparison) {
            if ($oneComparison instanceof Comparison && !$oneComparison->getValue()->isEmpty()) {
                if (preg_match("/^-/", $oneComparison->getName())) {
                    $comparisons['must_not'][]= $oneComparison->getComparison($this->makeComparisonFormatter());
                } else {
                    $comparisons['must'][]= $oneComparison->getComparison($this->makeComparisonFormatter());
                }
            }
        }

        if($this->identity->hasRawQuery()) {
            $comparisons['must'][]= $this->identity->getRawQuery();
        }

        if (empty($comparisons)) {
            $comparisons =  ['must' => ['match_all' => []]];
        }

        $comparisons['filter'] = (new ElasticsearchGeodistFormatter($this->identity))->format();

        return ['bool' => $comparisons];
    }

    private function makeComparisonFormatter()
    {
        return new ElasticsearchComparisonFormatter();
    }

    private function makeSortingFormatter()
    {
        return new ElasticsearchSortingFormatter();
    }
}

{'fraction_non_alphanumeric': 0.1484263233190272, 'fraction_numerical': 0.002682403433476395, 'mean_word_length': 2.4166157605375687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '2558051', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1516, 'n_words': 272}