Passed
Pull Request — main (#2)
by Dante
12:34
created

ElasticSearchAdapter::buildElasticSearchQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace BEdita\ElasticSearch\Adapter;
3
4
use BEdita\Core\Search\BaseAdapter;
0 ignored issues
show
Bug introduced by
The type BEdita\Core\Search\BaseAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Cake\Database\Expression\ComparisonExpression;
6
use Cake\ORM\Query;
7
use Cake\Datasource\EntityInterface;
8
9
class ElasticSearchAdapter extends BaseAdapter
10
{
11
    /**
12
     * @inheritDoc
13
     */
14
    public function search(Query $query, string $text, array $options = [], array $config = []): Query
15
    {
16
        return $query;
17
    }
18
19
    /**
20
     * {@inheritDoc}
21
     *
22
     * @codeCoverageIgnore
23
     */
24
    public function indexResource(EntityInterface $entity, string $operation): void
25
    {
26
    }
27
28
    /**
29
     * Build elastic search query
30
     */
31
    protected function buildElasticSearchQuery(array $options): array
32
    {
33
        return ['todo'];
34
    }
35
36
    /**
37
     * Build query and return it
38
     *
39
     * @param Query $query The query
40
     * @param array $options The options
41
     * @return Query
42
     */
43
    protected function buildQuery(Query $query, array $options): Query
44
    {
45
        $results = $this->buildElasticSearchQuery($options);
46
47
        if (count($results) === 0) {
48
            // Nothing found. No results should be returned. Add a contradiction to the `WHERE` clause.
49
            return $query->where(new ComparisonExpression(1, 1, 'integer', '<>'));
50
        }
51
52
        // TODO: implement this
53
        return $query;
54
55
        // // Prepare temporary table with `id` and `score` from ElasticSearch results.
56
        // $tempTable = sprintf('elasticsearch_%s', sha1($this->compiledQuery));
57
        // $tempTable = $this->createTempTable($tempTable);
58
        // $insertQuery = $tempTable->query()->insert(['id', 'score']);
59
        // foreach ($results as $row) {
60
        //     $insertQuery = $insertQuery->values($row);
61
        // }
62
        // $insertQuery->execute();
63
64
        // // Add a join with the temporary table to filter by ID and sort by relevance score.
65
        // return $query
66
        //     ->innerJoin(
67
        //         $tempTable->getTable(),
68
        //         new ComparisonExpression(
69
        //             new IdentifierExpression($tempTable->aliasField('id')),
70
        //             new IdentifierExpression($this->getTable()->aliasField('id')),
71
        //             'integer',
72
        //             '='
73
        //         )
74
        //     )
75
        //     ->orderDesc($tempTable->aliasField('score'));
76
    }
77
78
    // /**
79
    //  * Create a temporary table to store search results.
80
    //  *
81
    //  * @param string $table Temporary table name.
82
    //  * @return \Cake\ORM\Table|null
83
    //  */
84
    // protected function createTempTable($table)
85
    // {
86
    //     // $connection = $this->getTable()->getConnection();
87
    //     // $schema = (new TableSchema($table))
88
    //     //     ->setTemporary(true)
89
    //     //     ->addColumn('id', [
90
    //     //         'type' => TableSchema::TYPE_INTEGER,
91
    //     //         'length' => 11,
92
    //     //         'unsigned' => true,
93
    //     //         'null' => false,
94
    //     //     ])
95
    //     //     ->addColumn('score', [
96
    //     //         'type' => TableSchema::TYPE_FLOAT,
97
    //     //         'null' => false,
98
    //     //     ])
99
    //     //     ->addConstraint(
100
    //     //         'PRIMARY',
101
    //     //         [
102
    //     //             'type' => TableSchema::CONSTRAINT_PRIMARY,
103
    //     //             'columns' => ['id'],
104
    //     //         ]
105
    //     //     )
106
    //     //     ->addIndex(
107
    //     //         sprintf('%s_score_idx', str_replace('_', '', $table)),
108
    //     //         [
109
    //     //             'type' => TableSchema::INDEX_INDEX,
110
    //     //             'columns' => ['score'],
111
    //     //         ]
112
    //     //     );
113
114
    //     // try {
115
    //     //     // Execute SQL to create table. In MySQL the transaction is completely useless,
116
    //     //     // because `CREATE TABLE` implicitly implies a commit.
117
    //     //     $connection->transactional(function (Connection $connection) use ($schema) {
118
    //     //         foreach ($schema->createSql($connection) as $statement) {
119
    //     //             $connection->execute($statement);
120
    //     //         }
121
    //     //     });
122
    //     // } catch (\Exception $e) {
123
    //     //     $this->log($e, LogLevel::CRITICAL);
124
125
    //     //     return null;
126
    //     // }
127
128
    //     // $table = (new Table(compact('connection', 'table', 'schema')))
129
    //     //     ->setPrimaryKey('id')
130
    //     //     ->setDisplayField('score');
131
132
    //     // return $table;
133
    // }
134
}
135