Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
Elasticsearch DSL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public
Elasticsearch DSL
Commits
40002a9a
Commit
40002a9a
authored
8 years ago
by
tchiotludo
Committed by
Simonas Šerlinskas
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
InnerHit new implementation
parent
bf444e92
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/InnerHit/NestedInnerHit.php
+10
-93
10 additions, 93 deletions
src/InnerHit/NestedInnerHit.php
tests/InnerHit/NestedInnerHitTest.php
+29
-26
29 additions, 26 deletions
tests/InnerHit/NestedInnerHitTest.php
tests/InnerHit/ParentInnerHitTest.php
+6
-1
6 additions, 1 deletion
tests/InnerHit/ParentInnerHitTest.php
with
45 additions
and
120 deletions
src/InnerHit/NestedInnerHit.php
+
10
−
93
View file @
40002a9a
...
...
@@ -11,10 +11,10 @@
namespace
ONGR\ElasticsearchDSL\InnerHit
;
use
ONGR\ElasticsearchDSL\BuilderBag
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\NameAwareTrait
;
use
ONGR\ElasticsearchDSL\ParametersTrait
;
use
ONGR\ElasticsearchDSL\Search
;
/**
* Represents Elasticsearch top level nested inner hits.
...
...
@@ -36,19 +36,14 @@ class NestedInnerHit implements BuilderInterface
*/
private
$query
;
/**
* @var BuilderBag
*/
private
$innerHits
;
/**
* Inner hits container init.
*
* @param string
$name
* @param string
$path
* @param
BuilderInterface
$query
* @param string $name
* @param string $path
* @param
Search
$query
*/
public
function
__construct
(
$name
,
$path
,
BuilderInterface
$query
=
null
)
public
function
__construct
(
$name
,
$path
,
Search
$query
=
null
)
{
$this
->
setName
(
$name
);
$this
->
setPath
(
$path
);
...
...
@@ -65,14 +60,10 @@ class NestedInnerHit implements BuilderInterface
/**
* @param string $path
*
* @return $this
*/
public
function
setPath
(
$path
)
{
$this
->
path
=
$path
;
return
$this
;
}
/**
...
...
@@ -84,15 +75,11 @@ class NestedInnerHit implements BuilderInterface
}
/**
* @param BuilderInterface $query
*
* @return $this
* @param Search $query
*/
public
function
setQuery
(
BuilderInterface
$query
)
public
function
setQuery
(
Search
$query
=
null
)
{
$this
->
query
=
$query
;
return
$this
;
}
/**
...
...
@@ -103,70 +90,16 @@ class NestedInnerHit implements BuilderInterface
return
'nested'
;
}
/**
* Adds a sub-innerHit.
*
* @param NestedInnerHit $innerHit
*
* @return $this
*/
public
function
addInnerHit
(
NestedInnerHit
$innerHit
)
{
if
(
!
$this
->
innerHits
)
{
$this
->
innerHits
=
new
BuilderBag
();
}
$this
->
innerHits
->
add
(
$innerHit
);
return
$this
;
}
/**
* Returns all sub inner hits.
*
* @return BuilderInterface[]
*/
public
function
getInnerHits
()
{
if
(
$this
->
innerHits
)
{
return
$this
->
innerHits
->
all
();
}
else
{
return
[];
}
}
/**
* Returns sub inner hit.
* @param string $name inner hit name to return.
*
* @return NestedInnerHit|null
*/
public
function
getInnerHit
(
$name
)
{
if
(
$this
->
innerHits
&&
$this
->
innerHits
->
has
(
$name
))
{
return
$this
->
innerHits
->
get
(
$name
);
}
else
{
return
null
;
}
}
/**
* {@inheritdoc}
*/
public
function
toArray
()
{
$out
=
array_filter
(
[
'query'
=>
$this
->
getQuery
()
?
$this
->
getQuery
()
->
toArray
()
:
null
,
'inner_hits'
=>
$this
->
collectNestedInnerHits
(),
]
);
$out
=
$this
->
processArray
(
$out
);
$out
=
$this
->
getQuery
()
?
$this
->
getQuery
()
->
toArray
()
:
new
\stdClass
();
$out
=
[
$this
->
getPathType
()
=>
[
$this
->
getPath
()
=>
$out
?
$out
:
new
\stdClass
()
,
$this
->
getPath
()
=>
$out
,
],
];
...
...
@@ -174,7 +107,7 @@ class NestedInnerHit implements BuilderInterface
}
/**
* Returns 'path' for neted and 'type' for parent inner hits
* Returns 'path' for ne
s
ted and 'type' for parent inner hits
*
* @return null|string
*/
...
...
@@ -192,20 +125,4 @@ class NestedInnerHit implements BuilderInterface
}
return
$type
;
}
/**
* Process all nested inner hits.
*
* @return array
*/
private
function
collectNestedInnerHits
()
{
$result
=
[];
/** @var NestedInnerHit $innerHit */
foreach
(
$this
->
getInnerHits
()
as
$innerHit
)
{
$result
[
$innerHit
->
getName
()]
=
$innerHit
->
toArray
();
}
return
$result
;
}
}
This diff is collapsed.
Click to expand it.
tests/InnerHit/NestedInnerHitTest.php
+
29
−
26
View file @
40002a9a
...
...
@@ -5,7 +5,7 @@ namespace ONGR\ElasticsearchDSL\Tests\InnerHit;
use
ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit
;
use
ONGR\ElasticsearchDSL\Query\MatchQuery
;
use
ONGR\ElasticsearchDSL\Query\NestedQuery
;
use
ONGR\ElasticsearchDSL\
Query\TermQuery
;
use
ONGR\ElasticsearchDSL\
Search
;
class
NestedInnerHitTest
extends
\PHPUnit_Framework_TestCase
{
...
...
@@ -20,11 +20,28 @@ class NestedInnerHitTest extends \PHPUnit_Framework_TestCase
$matchQuery
=
new
MatchQuery
(
'foo.bar.aux'
,
'foo'
);
$nestedQuery
=
new
NestedQuery
(
'foo.bar'
,
$matchQuery
);
$innerHit
=
new
NestedInnerHit
(
'acme'
,
'foo'
,
$nestedQuery
);
$nestedInnerHit1
=
new
NestedInnerHit
(
'aux'
,
'foo.bar.aux'
,
$matchQuery
);
$nestedInnerHit2
=
new
NestedInnerHit
(
'lux'
,
'foo.bar.aux'
,
$matchQuery
);
$innerHit
->
addInnerHit
(
$nestedInnerHit1
);
$innerHit
->
addInnerHit
(
$nestedInnerHit2
);
$searchQuery
=
new
Search
();
$searchQuery
->
addQuery
(
$nestedQuery
);
$matchSearch
=
new
Search
();
$matchSearch
->
addQuery
(
$matchQuery
);
$innerHit
=
new
NestedInnerHit
(
'acme'
,
'foo'
,
$searchQuery
);
$emptyInnerHit
=
new
NestedInnerHit
(
'acme'
,
'foo'
);
$nestedInnerHit1
=
new
NestedInnerHit
(
'aux'
,
'foo.bar.aux'
,
$matchSearch
);
$nestedInnerHit2
=
new
NestedInnerHit
(
'lux'
,
'foo.bar.aux'
,
$matchSearch
);
$searchQuery
->
addInnerHit
(
$nestedInnerHit1
);
$searchQuery
->
addInnerHit
(
$nestedInnerHit2
);
$out
[]
=
[
$emptyInnerHit
,
[
'path'
=>
[
'foo'
=>
new
\stdClass
(),
],
],
];
$out
[]
=
[
$nestedInnerHit1
,
...
...
@@ -88,30 +105,16 @@ class NestedInnerHitTest extends \PHPUnit_Framework_TestCase
public
function
testGettersAndSetters
()
{
$query
=
new
MatchQuery
(
'acme'
,
'test'
);
$hit
=
new
NestedInnerHit
(
'test'
,
'acme'
,
new
TermQuery
(
'foo'
,
'bar'
));
$search
=
new
Search
();
$search
->
addQuery
(
$query
);
$hit
=
new
NestedInnerHit
(
'test'
,
'acme'
,
new
Search
());
$hit
->
setName
(
'foo'
);
$hit
->
setPath
(
'bar'
);
$hit
->
setQuery
(
$
query
);
$hit
->
setQuery
(
$
search
);
$this
->
assertEquals
(
'foo'
,
$hit
->
getName
());
$this
->
assertEquals
(
'bar'
,
$hit
->
getPath
());
$this
->
assertEquals
(
$query
,
$hit
->
getQuery
());
}
/**
* Tests getInnerHit() method
*/
public
function
testGetInnerHit
()
{
$query
=
new
MatchQuery
(
'acme'
,
'test'
);
$hit
=
new
NestedInnerHit
(
'test'
,
'acme'
,
$query
);
$nestedInnerHit1
=
new
NestedInnerHit
(
'foo'
,
'acme.foo'
,
$query
);
$nestedInnerHit2
=
new
NestedInnerHit
(
'bar'
,
'acme.bar'
,
$query
);
$hit
->
addInnerHit
(
$nestedInnerHit1
);
$hit
->
addInnerHit
(
$nestedInnerHit2
);
$this
->
assertEquals
(
$nestedInnerHit1
,
$hit
->
getInnerHit
(
'foo'
));
$this
->
assertEquals
(
$nestedInnerHit2
,
$hit
->
getInnerHit
(
'bar'
));
$this
->
assertNull
(
$hit
->
getInnerHit
(
'non_existing_hit'
));
$this
->
assertEquals
(
$search
,
$hit
->
getQuery
());
}
}
This diff is collapsed.
Click to expand it.
tests/InnerHit/ParentInnerHitTest.php
+
6
−
1
View file @
40002a9a
...
...
@@ -4,13 +4,18 @@ namespace ONGR\ElasticsearchDSL\Tests\InnerHit;
use
ONGR\ElasticsearchDSL\InnerHit\ParentInnerHit
;
use
ONGR\ElasticsearchDSL\Query\TermQuery
;
use
ONGR\ElasticsearchDSL\Search
;
class
ParentInnerHitTest
extends
\PHPUnit_Framework_TestCase
{
public
function
testToArray
()
{
$query
=
new
TermQuery
(
'foo'
,
'bar'
);
$hit
=
new
ParentInnerHit
(
'test'
,
'acme'
,
$query
);
$search
=
new
Search
();
$search
->
addQuery
(
$query
);
$hit
=
new
ParentInnerHit
(
'test'
,
'acme'
,
$search
);
$expected
=
[
'type'
=>
[
'acme'
=>
[
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment