Skip to content

Commit

Permalink
Little fix and Test for image plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmoati committed Feb 23, 2015
1 parent 6debf36 commit 3c867e1
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ Dependencies
|[Elasticsearch thrift transport plugin](https://github.com/elasticsearch/elasticsearch-transport-thrift/tree/v2.4.1)|2.4.1|no|
|[Elasticsearch memcached transport plugin](https://github.com/elasticsearch/elasticsearch-transport-memcached/tree/v2.4.1)|2.4.1|no|
|[Elasticsearch geocluster facet plugin](https://github.com/zenobase/geocluster-facet/tree/0.0.12)|0.0.12|no|
|[Elasticsearch image plugin](https://github.com/SibaTokyo/elasticsearch-image/tree/1.4.0)|1.4.0|no|
1 change: 1 addition & 0 deletions ansible/es-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- ES_TRANSPORT_MEMCACHED_VER: "2.4.1"
- ES_TRANSPORT_THRIFT_VER: "2.4.1"
- ES_GEOCLUSTER_FACET_VER: "0.0.12"
- ES_IMAGE_PLUGIN_VER: "1.4.0"
- ES_PROJECT_ROOT: "{{ lookup('env', 'ES_PROJECT_ROOT') }}"
- ES_COMPOSER_NODEV: "{{ lookup('env', 'ES_COMPOSER_NODEV') }}"
roles:
Expand Down
5 changes: 5 additions & 0 deletions ansible/roles/elasticsearch/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
state=present
update_cache=no
- name: install image plugin
command: >
creates=/usr/share/elasticsearch/plugins/image
/usr/share/elasticsearch/bin/plugin --url https://github.com/SibaTokyo/elasticsearch-image/releases/download/{{ ES_IMAGE_PLUGIN_VER }}/elasticsearch-image-{{ ES_IMAGE_PLUGIN_VER }}.zip -install image
- name: install mapper-attachments plugin
command: >
creates=/usr/share/elasticsearch/plugins/mapper-attachments
Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/elasticsearch/templates/config-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index.number_of_replicas: 0
index.store.type: memory

# Required plugins
plugin.mandatory: mapper-attachments, geocluster-facet, transport-thrift, transport-memcached
plugin.mandatory: mapper-attachments, geocluster-facet, transport-thrift, transport-memcached, image

# For bulk tests
bulk.udp.enabled: true
Expand Down
30 changes: 20 additions & 10 deletions lib/Elastica/Query/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
* @package Elastica
* @author Jacques Moati <[email protected]>
* @link https://github.com/kzwang/elasticsearch-image
*
*
* To use this feature you have to call the following command in the
* elasticsearch directory:
* <code>
* ./bin/plugin --url https://github.com/SibaTokyo/elasticsearch-image/releases/download/1.4.0/elasticsearch-image-1.4.0.zip --install image
* </code>
* This installs the image plugin. More infos
* can be found here: {@link https://github.com/SibaTokyo/elasticsearch-image}
*
*/
class Image extends AbstractQuery
{
Expand All @@ -24,7 +34,7 @@ public function __construct(array $image = array())
* @param string $key
* @param string $value
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldParam($field, $key, $value)
{
Expand All @@ -45,7 +55,7 @@ public function setFieldParam($field, $key, $value)
* @param string $field
* @param float $boost
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldBoost($field, $boost = 1.0)
{
Expand All @@ -60,7 +70,7 @@ public function setFieldBoost($field, $boost = 1.0)
* @param string $field
* @param string $feature
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldFeature($field, $feature = "CEDD")
{
Expand All @@ -75,7 +85,7 @@ public function setFieldFeature($field, $feature = "CEDD")
* @param string $field
* @param string $hash
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldHash($field, $hash = "BIT_SAMPLING")
{
Expand All @@ -88,7 +98,7 @@ public function setFieldHash($field, $hash = "BIT_SAMPLING")
* @param string $field
* @param string $path File will be base64_encode
*
* @return Image
* @return $this
* @throws \Exception
*/
public function setFieldImage($field, $path)
Expand All @@ -106,7 +116,7 @@ public function setFieldImage($field, $path)
* @param string $field
* @param string $index
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldIndex($field, $index)
{
Expand All @@ -119,7 +129,7 @@ public function setFieldIndex($field, $index)
* @param string $field
* @param string $type
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldType($field, $type)
{
Expand All @@ -132,7 +142,7 @@ public function setFieldType($field, $type)
* @param string $field
* @param string $id
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldId($field, $id)
{
Expand All @@ -145,7 +155,7 @@ public function setFieldId($field, $id)
* @param string $field
* @param string $path
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setFieldPath($field, $path)
{
Expand All @@ -163,7 +173,7 @@ public function setFieldPath($field, $path)
* @param string $id
* @param string $path
*
* @return \Elastica\Query\Image
* @return $this
*/
public function setImageByReference($field, $index, $type, $id, $path = null)
{
Expand Down
93 changes: 92 additions & 1 deletion test/lib/Elastica/Test/Query/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Index;
use Elastica\Query\Image;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;
use Elastica\Type\Mapping;

class ImageTest extends BaseTest
{
Expand Down Expand Up @@ -36,10 +38,99 @@ public function testToArrayFromImage()
$query->setFieldHash($field, 'BIT_SAMPLING');
$query->setFieldBoost($field, 100);

$query->setFieldImage($field, __DIR__ . '/../../../../data/test.jpg');
$query->setFieldImage($field, BASE_PATH . '/data/test.jpg');

$jsonString = '{"image":{"image":{"feature":"CEDD","hash":"BIT_SAMPLING","boost":100,"image":"\/9j\/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP\/sABFEdWNreQABAAQAAAA8AAD\/4QN6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P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UfvQdylQD9AABrKPedG7y7o2RlBu1tujs6A+qIiqJPq8w+CFsICEk9tKoO2\/yy3qIiY+aNibV6M5t5f9+oD5\/lpvV\/6j+Wc\/f0B5X82u9Xi80fyzn7+gPJfm43qim3zR\/LOfv6AZP83+9ZODzP\/LO\/v6Ag3P8ANvvZm2+TC5y2xkktk0siPHNHQQ0wUm1N0xQuoqitARPyqafn3XfDbJ0dslhWRqRJmPInaijjJstipcGYzc2Jx4L1KAO2gFQEL035N4SqBmJ546KzzfR+byDkzZ8cMEwxwoQe\/vf8N+0oU5bWfqdjH9c\/V7NgvRfO3NZsMdvN8\/twx4cKA5j8BuP1K\/pKA+fgL9yv6OgF+An3J\/o6A8r\/APAOP1I7PQ6A8F\/j1x+o\/Z6FQHgv8deP1F7PQqA7rRnq\/wCal9S\/MfmrOubzRzfMc5gmObo\/a5sOrtoC\/wD73\/DftKAX97\/hv2lARP7p50\/4Of5j5eTJn9nHGhD\/2Q=="}}}';
$this->assertEquals($jsonString, json_encode($query->toArray()));
}

public function testFromReference()
{
$field = "image";

$client = $this->_getClient();
$index = $client->getIndex('test');
$index->create(array(), true);

$type = $index->getType('test');

$mapping = new Mapping($type, array(
$field => array(
"type" => "image",
"store" => false,
"include_in_all" => false,
"feature" => array(
"CEDD" => array(
"hash" => "BIT_SAMPLING",
),
)
)
)
);

$type->setMapping($mapping);

$doc = new Document(1, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);
$doc = new Document(2, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);
$doc = new Document(3, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);

$index->refresh();

$query = new Image();
$query->setFieldFeature($field, 'CEDD');
$query->setFieldHash($field, 'BIT_SAMPLING');
$query->setFieldBoost($field, 100);
$query->setImageByReference($field, $index->getName(), $type->getName(), 1);

$resultSet = $index->search($query);
$this->assertEquals(3, $resultSet->count());
}

public function testFromImage()
{
$field = "image";

$client = $this->_getClient();
$index = $client->getIndex('test');
$index->create(array(), true);

$type = $index->getType('test');

$mapping = new Mapping($type, array(
$field => array(
"type" => "image",
"store" => false,
"include_in_all" => false,
"feature" => array(
"CEDD" => array(
"hash" => "BIT_SAMPLING",
),
)
)
)
);

$type->setMapping($mapping);

$doc = new Document(1, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);
$doc = new Document(2, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);
$doc = new Document(3, array($field => base64_encode(file_get_contents(BASE_PATH . '/data/test.jpg'))));
$type->addDocument($doc);

$index->refresh();

$query = new Image();
$query->setFieldFeature($field, 'CEDD');
$query->setFieldHash($field, 'BIT_SAMPLING');
$query->setFieldBoost($field, 100);
$query->setFieldImage($field, BASE_PATH . '/data/test.jpg');

$resultSet = $index->search($query);
$this->assertEquals(3, $resultSet->count());
}
}

0 comments on commit 3c867e1

Please sign in to comment.