Skip to content

Commit 71a15e6

Browse files
committed
Merge remote-tracking branch 'origin/dev'
2 parents 9e0e39a + ecf8945 commit 71a15e6

File tree

10 files changed

+932
-1974
lines changed

10 files changed

+932
-1974
lines changed

README.md

Lines changed: 75 additions & 1697 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
{
2-
"name": "pdphilip/elasticsearch",
3-
"description": "An Elasticsearch implementation of Laravel's Eloquent ORM",
4-
"keywords": [
5-
"laravel",
6-
"eloquent",
7-
"elasticsearch",
8-
"elastic",
9-
"database",
10-
"model"
11-
],
12-
"homepage": "https://github.com/pdphilip/laravel-elasticsearch",
13-
"authors": [
14-
{
15-
"name": "David Philip",
16-
"email": "pd.philip@gmail.com",
17-
"homepage": "https://github.com/pdphilip"
2+
"name": "pdphilip/elasticsearch",
3+
"description": "An Elasticsearch implementation of Laravel's Eloquent ORM",
4+
"keywords": [
5+
"laravel",
6+
"eloquent",
7+
"elasticsearch",
8+
"elastic",
9+
"database",
10+
"model"
11+
],
12+
"homepage": "https://github.com/pdphilip/laravel-elasticsearch",
13+
"authors": [
14+
{
15+
"name": "David Philip",
16+
"email": "pd.philip@gmail.com",
17+
"homepage": "https://github.com/pdphilip"
18+
}
19+
],
20+
"license": "MIT",
21+
"require": {
22+
"php": "^8.2",
23+
"illuminate/support": "^11.0",
24+
"illuminate/container": "^11.0",
25+
"illuminate/database": "^11.0",
26+
"illuminate/events": "^11.0",
27+
"elasticsearch/elasticsearch": "^8.12"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^10.3",
31+
"orchestra/testbench": "^8.0",
32+
"mockery/mockery": "^1.4.4",
33+
"doctrine/coding-standard": "12.0.x-dev"
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"PDPhilip\\Elasticsearch\\Tests\\": "tests/"
38+
}
39+
},
40+
"autoload": {
41+
"psr-4": {
42+
"PDPhilip\\Elasticsearch\\": "src/"
43+
}
44+
},
45+
"extra": {
46+
"laravel": {
47+
"providers": [
48+
"PDPhilip\\Elasticsearch\\ElasticServiceProvider"
49+
]
50+
}
1851
}
19-
],
20-
"license": "MIT",
21-
"require": {
22-
"php": "^8.1",
23-
"illuminate/support": "^10.0",
24-
"illuminate/container": "^10.0",
25-
"illuminate/database": "^10.0",
26-
"illuminate/events": "^10.0",
27-
"elasticsearch/elasticsearch": "^8.10"
28-
},
29-
"require-dev": {
30-
"phpunit/phpunit": "^10.3",
31-
"orchestra/testbench": "^8.0",
32-
"mockery/mockery": "^1.4.4",
33-
"doctrine/coding-standard": "12.0.x-dev"
34-
},
35-
"autoload-dev": {
36-
"psr-4": {
37-
"PDPhilip\\Elasticsearch\\Tests\\": "tests/"
38-
}
39-
},
40-
"autoload": {
41-
"psr-4": {
42-
"PDPhilip\\Elasticsearch\\": "src/"
43-
}
44-
},
45-
"extra": {
46-
"laravel": {
47-
"providers": [
48-
"PDPhilip\\Elasticsearch\\ElasticServiceProvider"
49-
]
50-
}
51-
}
52-
}
52+
}

src/DSL/Bridge.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public function processIndicesDsl($method, $params): Results
9393
*/
9494
public function processFind($wheres, $options, $columns): Results
9595
{
96-
9796
$params = $this->buildParams($this->index, $wheres, $options, $columns);
9897

9998
return $this->_returnSearch($params, __FUNCTION__);
@@ -104,9 +103,9 @@ public function processFind($wheres, $options, $columns): Results
104103
*/
105104
public function processSearch($searchParams, $searchOptions, $wheres, $opts, $fields, $cols)
106105
{
107-
108106
$params = $this->buildSearchParams($this->index, $searchParams, $searchOptions, $wheres, $opts, $fields, $cols);
109107

108+
110109
return $this->_returnSearch($params, __FUNCTION__);
111110

112111
}
@@ -117,6 +116,7 @@ protected function _returnSearch($params, $source)
117116
$params['size'] = $this->maxSize;
118117
}
119118
try {
119+
120120
$process = $this->client->search($params);
121121

122122
return $this->_sanitizeSearchResponse($process, $params, $this->_queryTag($source));
@@ -141,6 +141,13 @@ public function processDistinct($wheres, $options, $columns, $includeDocCount =
141141
unset($options['skip']);
142142
unset($options['limit']);
143143

144+
if ($sort) {
145+
$sortField = key($sort);
146+
$sortDir = $sort[$sortField]['order'] ?? 'asc';
147+
$sort = [$sortField => $sortDir];
148+
}
149+
150+
144151
$params = $this->buildParams($this->index, $wheres, $options);
145152
$params['body']['aggs'] = $this->createNestedAggs($columns, $sort);
146153

@@ -649,6 +656,25 @@ private function _matrixAggregate($wheres, $options, $columns): Results
649656

650657
}
651658

659+
public function parseRequiredKeywordMapping($field)
660+
{
661+
$mappings = $this->processIndexMappings($this->index);
662+
$map = reset($mappings);
663+
if (!empty($map['mappings']['properties'][$field])) {
664+
$fieldMap = $map['mappings']['properties'][$field];
665+
if (!empty($fieldMap['type']) && $fieldMap['type'] === 'keyword') {
666+
//primary Map is field. Use as is
667+
return $field;
668+
}
669+
if (!empty($fieldMap['fields']['keyword'])) {
670+
return $field.'.keyword';
671+
}
672+
}
673+
674+
return false;
675+
676+
}
677+
652678
//----------------------------------------------------------------------
653679
// Distinct Aggregates
654680
//----------------------------------------------------------------------
@@ -805,6 +831,7 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
805831
$meta['timed_out'] = $response['timed_out'];
806832
$meta['total'] = $response['hits']['total']['value'] ?? 0;
807833
$meta['max_score'] = $response['hits']['max_score'] ?? 0;
834+
$meta['sorts'] = [];
808835
$data = [];
809836
if (!empty($response['hits']['hits'])) {
810837
foreach ($response['hits']['hits'] as $hit) {
@@ -816,13 +843,55 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
816843
$datum[$key] = $value;
817844
}
818845
}
819-
$data[] = $datum;
846+
if (!empty($hit['inner_hits'])) {
847+
foreach ($hit['inner_hits'] as $innerKey => $innerHit) {
848+
$datum[$innerKey] = $this->_filterInnerHits($innerHit);
849+
}
850+
}
851+
852+
//------------------------ later, maybe ------------------------------
853+
// if (!empty($hit['sort'])) {
854+
// $datum['_meta']['sort'] = $this->_parseSort($hit['sort'], $params['body']['sort'] ?? []);
855+
// }
856+
//----------------------------------------------------------------------
857+
858+
859+
{
860+
$data[] = $datum;
861+
}
820862
}
821863
}
822864

823865
return $this->_return($data, $meta, $params, $queryTag);
824866
}
825867

868+
private function _filterInnerHits($innerHit)
869+
{
870+
$hits = [];
871+
foreach ($innerHit['hits']['hits'] as $inner) {
872+
$innerDatum = [];
873+
if (!empty($inner['_source'])) {
874+
foreach ($inner['_source'] as $innerSourceKey => $innerSourceValue) {
875+
$innerDatum[$innerSourceKey] = $innerSourceValue;
876+
}
877+
}
878+
$hits[] = $innerDatum;
879+
}
880+
881+
return $hits;
882+
}
883+
884+
885+
private function _parseSort($sort, $sortParams)
886+
{
887+
$sortValues = [];
888+
foreach ($sort as $key => $value) {
889+
$sortValues[array_key_first($sortParams[$key])] = $value;
890+
}
891+
892+
return $sortValues;
893+
}
894+
826895
private function _sanitizeDistinctResponse($response, $columns, $includeDocCount)
827896
{
828897
$keys = [];
@@ -837,17 +906,18 @@ private function _sanitizeDistinctResponse($response, $columns, $includeDocCount
837906
private function processBuckets($columns, $keys, $response, $index, $includeDocCount, $currentData = [])
838907
{
839908
$data = [];
840-
841909
if (!empty($response[$keys[$index]]['buckets'])) {
842910
foreach ($response[$keys[$index]]['buckets'] as $res) {
911+
843912
$datum = $currentData;
844-
//clear keyword from column name
913+
845914
$col = $columns[$index];
846915
if (str_contains($col, '.keyword')) {
847916
$col = str_replace('.keyword', '', $col);
848917
}
849918

850919
$datum[$col] = $res['key'];
920+
851921
if ($includeDocCount) {
852922
$datum[$col.'_count'] = $res['doc_count'];
853923
}

0 commit comments

Comments
 (0)