Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
yii2bootstrap
Commits
d442f056
Commit
d442f056
authored
Oct 07, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
elasticsearch find by simple condition
parent
955bf7da
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
657 additions
and
410 deletions
+657
-410
ActiveQuery.php
framework/yii/elasticsearch/ActiveQuery.php
+83
-400
ActiveRecord.php
framework/yii/elasticsearch/ActiveRecord.php
+1
-1
Command.php
framework/yii/elasticsearch/Command.php
+120
-0
Connection.php
framework/yii/elasticsearch/Connection.php
+17
-0
Query.php
framework/yii/elasticsearch/Query.php
+409
-0
QueryBuilder.php
framework/yii/elasticsearch/QueryBuilder.php
+7
-2
Customer.php
tests/unit/data/ar/elasticsearch/Customer.php
+1
-1
ActiveRecordTest.php
tests/unit/framework/elasticsearch/ActiveRecordTest.php
+19
-6
No files found.
framework/yii/elasticsearch/ActiveQuery.php
View file @
d442f056
This diff is collapsed.
Click to expand it.
framework/yii/elasticsearch/ActiveRecord.php
View file @
d442f056
...
@@ -325,7 +325,7 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
...
@@ -325,7 +325,7 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
$key
=
reset
(
$this
->
primaryKey
());
$key
=
reset
(
$this
->
primaryKey
());
$pk
=
$this
->
getAttribute
(
$key
);
$pk
=
$this
->
getAttribute
(
$key
);
unset
(
$values
[
$key
]);
//
unset($values[$key]);
// save attributes
// save attributes
if
(
$pk
===
null
)
{
if
(
$pk
===
null
)
{
...
...
framework/yii/elasticsearch/Command.php
0 → 100644
View file @
d442f056
<?php
/**
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace
yii\elasticsearch
;
use
yii\base\Component
;
use
yii\helpers\Json
;
class
Command
extends
Component
{
/**
* @var Connection
*/
public
$db
;
public
$api
=
'_search'
;
/**
* @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html#search-multi-index
*/
public
$index
;
/**
* @var string|array the types to execute the query on. Defaults to null meaning all types
*/
public
$type
;
/**
* @var array|string array or json
*/
public
$query
;
private
function
createUrl
(
$endPoint
=
null
)
{
if
(
$endPoint
===
null
)
{
$endPoint
=
$this
->
api
;
}
if
(
$this
->
index
===
null
&&
$this
->
type
===
null
)
{
return
'/'
.
$endPoint
;
}
$index
=
$this
->
index
;
if
(
$index
===
null
)
{
$index
=
'_all'
;
}
elseif
(
is_array
(
$index
))
{
$index
=
implode
(
','
,
$index
);
}
$type
=
$this
->
type
;
if
(
is_array
(
$type
))
{
$type
=
implode
(
','
,
$type
);
}
return
'/'
.
$index
.
'/'
.
(
empty
(
$type
)
?
''
:
$type
.
'/'
)
.
$endPoint
;
}
public
function
queryAll
()
{
$query
=
$this
->
query
;
if
(
empty
(
$query
))
{
$query
=
'{}'
;
}
if
(
is_array
(
$query
))
{
$query
=
Json
::
encode
(
$query
);
}
$http
=
$this
->
db
->
http
();
$response
=
$http
->
post
(
$this
->
createUrl
(),
null
,
$query
)
->
send
();
$data
=
Json
::
decode
(
$response
->
getBody
(
true
));
// TODO store query meta data for later use
$docs
=
array
();
switch
(
$this
->
api
)
{
default
:
case
'_search'
:
if
(
isset
(
$data
[
'hits'
][
'hits'
]))
{
$docs
=
$data
[
'hits'
][
'hits'
];
}
break
;
case
'_mget'
:
if
(
isset
(
$data
[
'docs'
]))
{
$docs
=
$data
[
'docs'
];
}
break
;
}
$rows
=
array
();
foreach
(
$docs
as
$doc
)
{
// TODO maybe return type info
if
(
isset
(
$doc
[
'exists'
])
&&
!
$doc
[
'exists'
])
{
continue
;
}
$row
=
$doc
[
'_source'
];
$row
[
'id'
]
=
$doc
[
'_id'
];
$rows
[]
=
$row
;
}
return
$rows
;
}
public
function
queryOne
()
{
// TODO set limit
$rows
=
$this
->
queryAll
();
return
reset
(
$rows
);
}
public
function
queryCount
()
{
//http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html
$query
=
$this
->
query
;
if
(
empty
(
$query
))
{
$query
=
''
;
}
if
(
is_array
(
$query
))
{
$query
=
Json
::
encode
(
$query
);
}
$http
=
$this
->
db
->
http
();
$response
=
$http
->
post
(
$this
->
createUrl
(
'_count'
),
null
,
$query
)
->
send
();
$data
=
Json
::
decode
(
$response
->
getBody
(
true
));
// TODO store query meta data for later use
return
$data
[
'count'
];
}
}
\ No newline at end of file
framework/yii/elasticsearch/Connection.php
View file @
d442f056
...
@@ -53,6 +53,23 @@ class Connection extends Component
...
@@ -53,6 +53,23 @@ class Connection extends Component
}
}
}
}
/**
* Creates a command for execution.
* @param string $query the SQL statement to be executed
* @return Command the DB command
*/
public
function
createCommand
(
$query
=
null
,
$index
=
null
,
$type
=
null
)
{
$this
->
open
();
$command
=
new
Command
(
array
(
'db'
=>
$this
,
'query'
=>
$query
,
'index'
=>
$index
,
'type'
=>
$type
,
));
return
$command
;
}
/**
/**
* Closes the connection when this component is being serialized.
* Closes the connection when this component is being serialized.
* @return array
* @return array
...
...
framework/yii/elasticsearch/Query.php
View file @
d442f056
This diff is collapsed.
Click to expand it.
framework/yii/elasticsearch/QueryBuilder.php
View file @
d442f056
...
@@ -46,7 +46,7 @@ class QueryBuilder extends \yii\base\Object
...
@@ -46,7 +46,7 @@ class QueryBuilder extends \yii\base\Object
{
{
$searchQuery
=
array
();
$searchQuery
=
array
();
$this
->
buildSelect
(
$searchQuery
,
$query
->
select
);
$this
->
buildSelect
(
$searchQuery
,
$query
->
select
);
// $this->buildFrom(
&
$searchQuery, $query->from);
// $this->buildFrom($searchQuery, $query->from);
$this
->
buildCondition
(
$searchQuery
,
$query
->
where
);
$this
->
buildCondition
(
$searchQuery
,
$query
->
where
);
$this
->
buildOrderBy
(
$searchQuery
,
$query
->
orderBy
);
$this
->
buildOrderBy
(
$searchQuery
,
$query
->
orderBy
);
$this
->
buildLimit
(
$searchQuery
,
$query
->
limit
,
$query
->
offset
);
$this
->
buildLimit
(
$searchQuery
,
$query
->
limit
,
$query
->
offset
);
...
@@ -209,7 +209,12 @@ class QueryBuilder extends \yii\base\Object
...
@@ -209,7 +209,12 @@ class QueryBuilder extends \yii\base\Object
private
function
buildHashCondition
(
&
$query
,
$condition
)
private
function
buildHashCondition
(
&
$query
,
$condition
)
{
{
$query
[
'query'
][
'term'
]
=
$condition
;
foreach
(
$condition
as
$attribute
=>
$value
)
{
// ['query']['filteredQuery']
$query
[
'filter'
][
'bool'
][
'must'
][]
=
array
(
'term'
=>
array
(
$attribute
=>
$value
),
);
}
return
;
// TODO more
return
;
// TODO more
$parts
=
array
();
$parts
=
array
();
foreach
(
$condition
as
$column
=>
$value
)
{
foreach
(
$condition
as
$column
=>
$value
)
{
...
...
tests/unit/data/ar/elasticsearch/Customer.php
View file @
d442f056
...
@@ -35,6 +35,6 @@ class Customer extends ActiveRecord
...
@@ -35,6 +35,6 @@ class Customer extends ActiveRecord
public
static
function
active
(
$query
)
public
static
function
active
(
$query
)
{
{
$query
->
andWhere
(
'status=1'
);
$query
->
andWhere
(
array
(
'status'
=>
1
)
);
}
}
}
}
tests/unit/framework/elasticsearch/ActiveRecordTest.php
View file @
d442f056
...
@@ -4,7 +4,8 @@ namespace yiiunit\framework\elasticsearch;
...
@@ -4,7 +4,8 @@ namespace yiiunit\framework\elasticsearch;
use
yii\db\Query
;
use
yii\db\Query
;
use
yii\elasticsearch\Connection
;
use
yii\elasticsearch\Connection
;
use
yii\redis\ActiveQuery
;
use
yii\elasticsearch\ActiveQuery
;
use
yii\helpers\Json
;
use
yiiunit\data\ar\elasticsearch\ActiveRecord
;
use
yiiunit\data\ar\elasticsearch\ActiveRecord
;
use
yiiunit\data\ar\elasticsearch\Customer
;
use
yiiunit\data\ar\elasticsearch\Customer
;
use
yiiunit\data\ar\elasticsearch\OrderItem
;
use
yiiunit\data\ar\elasticsearch\OrderItem
;
...
@@ -80,6 +81,17 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -80,6 +81,17 @@ class ActiveRecordTest extends ElasticSearchTestCase
// $orderItem = new OrderItem();
// $orderItem = new OrderItem();
// $orderItem->setAttributes(array('order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0), false);
// $orderItem->setAttributes(array('order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0), false);
// $orderItem->save(false);
// $orderItem->save(false);
for
(
$n
=
0
;
$n
<
20
;
$n
++
)
{
$r
=
$db
->
http
()
->
post
(
'_count'
)
->
send
();
$c
=
Json
::
decode
(
$r
->
getBody
(
true
));
if
(
$c
[
'count'
]
!=
11
)
{
usleep
(
100000
);
}
else
{
return
;
}
}
throw
new
\Exception
(
'Unable to initialize elasticsearch data.'
);
}
}
public
function
testFind
()
public
function
testFind
()
...
@@ -124,13 +136,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
...
@@ -124,13 +136,14 @@ class ActiveRecordTest extends ElasticSearchTestCase
// find count, sum, average, min, max, scalar
// find count, sum, average, min, max, scalar
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
count
());
$this
->
assertEquals
(
6
,
Customer
::
find
()
->
sum
(
'id'
));
//
$this->assertEquals(6, Customer::find()->sum('id'));
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
average
(
'id'
));
//
$this->assertEquals(2, Customer::find()->average('id'));
$this
->
assertEquals
(
1
,
Customer
::
find
()
->
min
(
'id'
));
//
$this->assertEquals(1, Customer::find()->min('id'));
$this
->
assertEquals
(
3
,
Customer
::
find
()
->
max
(
'id'
));
//
$this->assertEquals(3, Customer::find()->max('id'));
// scope
// scope
$this
->
assertEquals
(
2
,
Customer
::
find
()
->
active
()
->
count
());
$this
->
assertEquals
(
2
,
count
(
Customer
::
find
()
->
active
()
->
all
()));
// $this->assertEquals(2, Customer::find()->active()->count());
// asArray
// asArray
$customer
=
Customer
::
find
()
->
where
(
array
(
'id'
=>
2
))
->
asArray
()
->
one
();
$customer
=
Customer
::
find
()
->
where
(
array
(
'id'
=>
2
))
->
asArray
()
->
one
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment