public function testMatchDoc() { $client = new 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 Percolator($index); $percolatorName = 'percotest'; $query = new Term(array('name' => 'ruflin')); $response = $percolator->registerQuery($percolatorName, $query); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); $doc1 = new Document(); $doc1->set('name', 'ruflin'); $doc2 = new Document(); $doc2->set('name', 'nicolas'); $index = new Index($index->getClient(), '_percolator'); $index->optimize(); $index->refresh(); $matches1 = $percolator->matchDoc($doc1); $this->assertTrue(in_array($percolatorName, $matches1)); $this->assertEquals(1, count($matches1)); $matches2 = $percolator->matchDoc($doc2); $this->assertEmpty($matches2); }
/** * Test case for using filtered percolator queries based on the Elasticsearch documentation examples. */ public function testRegisterAndUnregisterPercolator() { // step one: register create index and setup the percolator query from the ES documentation. $index = $this->_createIndex(); $percolator = new Percolator($index); $baseQuery = new Term(array('field1' => 'value1')); $fields = array('color' => 'blue'); $response = $percolator->registerQuery('kuku', $baseQuery, $fields); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); // refreshing is required in order to ensure the query is really ready for execution. $index->refresh(); // step two: match a document which should match the kuku query when filtered on the blue color $doc = new Document(); $doc->set('field1', 'value1'); $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue'))); $this->assertCount(1, $matches, 'No or too much registered query matched.'); $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.'); // step three: validate that using a different color, no registered query matches. $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green'))); $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.'); // unregister percolator query $response = $percolator->unregisterQuery('kuku'); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); // refreshing is required in order to ensure the query is really ready for execution. $index->refresh(); $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue'))); $this->assertCount(0, $matches, 'Percolator query did not get deleted.'); $index->delete(); }
/** * @group functional */ public function testPercolateWithAdditionalRequestBodyOptions() { $index = $this->_createIndex(); $percolator = new Percolator($index); $query = new Term(array('name' => 'foo')); $response = $percolator->registerQuery('percotest', $query, array('field1' => array('tag1', 'tag2'))); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); $query = new Term(array('name' => 'foo')); $response = $percolator->registerQuery('percotest1', $query, array('field1' => array('tag2'))); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); $doc1 = new Document(); $doc1->set('name', 'foo'); $index->refresh(); $options = array('track_scores' => true, 'sort' => array('_score' => 'desc'), 'size' => 1); $matches = $percolator->matchDoc($doc1, new Term(array('field1' => 'tag2')), 'type', $options); $this->assertCount(1, $matches); $this->assertEquals('percotest1', $matches[0]['_id']); $this->assertArrayHasKey('_score', $matches[0]); }