Ejemplo n.º 1
0
 public function testGeoPoint()
 {
     $client = new Elastica_Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     // Set mapping
     $type->setMapping(array('point' => array('type' => 'geo_point')));
     // Add doc 1
     $doc1 = new Elastica_Document(1, array('name' => 'ruflin'));
     $doc1->addGeoPoint('point', 17, 19);
     $type->addDocument($doc1);
     // Add doc 2
     $doc2 = new Elastica_Document(2, array('name' => 'ruflin'));
     $doc2->addGeoPoint('point', 30, 40);
     $type->addDocument($doc2);
     $index->optimize();
     $index->refresh();
     // Only one point should be in radius
     $query = new Elastica_Query();
     $geoFilter = new Elastica_Filter_GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km');
     $query = new Elastica_Query(new Elastica_Query_MatchAll());
     $query->setFilter($geoFilter);
     $this->assertEquals(1, $type->search($query)->count());
     // Both points should be inside
     $query = new Elastica_Query();
     $geoFilter = new Elastica_Filter_GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km');
     $query = new Elastica_Query(new Elastica_Query_MatchAll());
     $query->setFilter($geoFilter);
     $index->refresh();
     $this->assertEquals(2, $type->search($query)->count());
 }
Ejemplo n.º 2
0
	public function testMatchDoc() {

		$client = new Elastica_Client(array('persistent' => false));
		$index = $client->getIndex('elastica_test');
		$index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true);

		$percolator = new Elastica_Percolator($index);
		
		$percolatorName = 'percotest';
		
		$query = new Elastica_Query_Term(array('name' => 'ruflin'));
		$response = $percolator->registerQuery($percolatorName, $query);

		$this->assertTrue($response->isOk());
		$this->assertFalse($response->hasError());

		$doc1 = new Elastica_Document();
		$doc1->add('name', 'ruflin');

		$doc2 = new Elastica_Document();
		$doc2->add('name', 'nicolas');

		$index = new Elastica_Index($index->getClient(), '_percolator');
		$index->refresh();
		
		$matches1 = $percolator->matchDoc($doc1);
		
		$this->assertTrue(in_array($percolatorName, $matches1));
		$this->assertEquals(1, count($matches1));
		
		$matches2 = $percolator->matchDoc($doc2);
		$this->assertEmpty($matches2);
	}
Ejemplo n.º 3
0
 public function testGeoPoint()
 {
     $client = new Elastica_Client();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     // Set mapping
     $type->setMapping(array('point' => array('type' => 'geo_point')));
     // Add doc 1
     $doc1 = new Elastica_Document(1, array('name' => 'ruflin'));
     $doc1->addGeoPoint('point', 17, 19);
     $type->addDocument($doc1);
     // Add doc 2
     $doc2 = new Elastica_Document(2, array('name' => 'ruflin'));
     $doc2->addGeoPoint('point', 30, 40);
     $type->addDocument($doc2);
     $index->refresh();
     // Only one point should be in polygon
     $query = new Elastica_Query();
     $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16));
     $geoFilter = new Elastica_Filter_GeoPolygon('point', compact('points'));
     $query = new Elastica_Query(new Elastica_Query_MatchAll());
     $query->setFilter($geoFilter);
     $this->assertEquals(1, $type->search($query)->count());
     // Both points should be inside
     $query = new Elastica_Query();
     $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16));
     $geoFilter = new Elastica_Filter_GeoPolygon('point', compact('points'));
     $query = new Elastica_Query(new Elastica_Query_MatchAll());
     $query->setFilter($geoFilter);
     $this->assertEquals(2, $type->search($query)->count());
 }
Ejemplo n.º 4
0
 /**
  * Match a document to percolator queries
  *
  * @param  Elastica_Document                             $doc
  * @param  string|Elastica_Query|Elastica_Query_Abstract $query Not implemented yet
  * @return Elastica_Response
  */
 public function matchDoc(Elastica_Document $doc, $query = null)
 {
     $path = $this->_index->getName() . '/type/_percolate';
     $data = array('doc' => $doc->getData());
     $response = $this->getIndex()->getClient()->request($path, Elastica_Request::GET, $data);
     $data = $response->getData();
     return $data['matches'];
 }
Ejemplo n.º 5
0
 public function testToArray()
 {
     $id = 17;
     $data = array('hello' => 'world');
     $type = 'testtype';
     $index = 'textindex';
     $doc = new Elastica_Document($id, $data, $type, $index);
     $result = array('_index' => $index, '_type' => $type, '_id' => $id, '_source' => $data);
     $this->assertEquals($result, $doc->toArray());
 }
Ejemplo n.º 6
0
 public function testMatchDoc()
 {
     $index = $this->_createIndex();
     $percolator = new Elastica_Percolator($index);
     $percolatorName = 'percotest';
     $query = new Elastica_Query_Term(array('name' => 'ruflin'));
     $response = $percolator->registerQuery($percolatorName, $query);
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $doc1 = new Elastica_Document();
     $doc1->add('name', 'ruflin');
     $doc2 = new Elastica_Document();
     $doc2->add('name', 'nicolas');
     $index = new Elastica_Index($index->getClient(), '_percolator');
     $index->refresh();
     $matches1 = $percolator->matchDoc($doc1);
     $this->assertTrue(in_array($percolatorName, $matches1));
     $this->assertEquals(1, count($matches1));
     $matches2 = $percolator->matchDoc($doc2);
     $this->assertEmpty($matches2);
 }
Ejemplo n.º 7
0
	public function testAddDocumentVersion() {
		$client = new Elastica_Client();
		$index = $client->getIndex('test');
		$index->create(array(), true);
		$type = new Elastica_Type($index, 'test');

		$doc1 = new Elastica_Document(1);
		$doc1->add('title', 'Hello world');

		$return = $type->addDocument($doc1);
		$data = $return->getData();
		$this->assertEquals(1, $data['_version']);

		$return = $type->addDocument($doc1);
		$data = $return->getData();
		$this->assertEquals(2, $data['_version']);
	}
Ejemplo n.º 8
0
 public function testSetData()
 {
     $doc = new Elastica_Document();
     $returnValue = $doc->setData(array('data'));
     $this->assertInstanceOf('Elastica_Document', $returnValue);
 }
Ejemplo n.º 9
0
 /**
  * More like this query based on the given object
  *
  * The id in the given object has to be set
  *
  * @param Elastica_Document      $doc  Document to query for similar objects
  * @param array                  $params OPTIONAL Additional arguments for the query
  * @param Elastica_Query         $query OPTIONAL Query to filter the moreLikeThis results
  * @return Elastica_ResultSet    ResultSet with all results inside
  * @link http://www.elasticsearch.org/guide/reference/api/more-like-this.html
  */
 public function moreLikeThis(Elastica_Document $doc, $params = array(), $query = array())
 {
     $path = $doc->getId() . '/_mlt';
     $query = Elastica_Query::create($query);
     $response = $this->request($path, Elastica_Request::GET, $query->toArray(), $params);
     return new Elastica_ResultSet($response);
 }
Ejemplo n.º 10
0
 /**
  * More like this query based on the given object
  *
  * The id in the given object has to be set
  *
  * @param EalsticSearch_Document $doc  Document to query for similar objects
  * @param array                  $args OPTIONAL Additional arguments for the query
  * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/more_like_this/
  */
 public function moreLikeThis(Elastica_Document $doc, $args = array())
 {
     // TODO: Not tested yet
     $path = $doc->getId() . '/_mlt';
     return $this->request($path, Elastica_Request::GET, $args);
 }
Ejemplo n.º 11
0
 /**
  * Index a associative array.
  *
  * @param \Elastica_Type $type
  * @param int $id ID of document
  * @param array $data Array with data of document
  */
 protected function index(\Elastica_Type $type, $id, $data, $parent = false)
 {
     // create document
     $document = new \Elastica_Document($id, $data);
     if ($parent) {
         $document->setParent($parent);
     }
     // save to index
     $type->addDocument($document);
 }
Ejemplo n.º 12
0
 public function testAddWordxFile()
 {
     $indexMapping = array('file' => array('type' => 'attachment'), 'text' => array('type' => 'string', 'store' => 'no'));
     $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0));
     $client = new Elastica_Client();
     $index = new Elastica_Index($client, 'content');
     $type = new Elastica_Type($index, 'content');
     $index->create($indexParams, true);
     $type->setMapping($indexMapping);
     $doc1 = new Elastica_Document(1);
     $doc1->addFile('file', BASE_PATH . '/data/test.docx');
     $doc1->add('text', 'basel world');
     $type->addDocument($doc1);
     $doc2 = new Elastica_Document(2);
     $doc2->add('text', 'running in basel');
     $type->addDocument($doc2);
     $index->optimize();
     $resultSet = $type->search('xodoa');
     $this->assertEquals(1, $resultSet->count());
     $resultSet = $type->search('basel');
     $this->assertEquals(2, $resultSet->count());
     $resultSet = $type->search('ruflin');
     $this->assertEquals(0, $resultSet->count());
 }