public function testDelete() { update_option('fields', array('field1' => 1, 'field2' => 1)); register_post_type('post'); $post = (object) array('post_type' => 'post', 'ID' => 1, 'field1' => 'value1', 'field2' => 'value2'); Indexer::addOrUpdate($post); $this->index->refresh(); $this->assertEquals(1, $this->index->count(new \Elastica\Query(array('query' => array('bool' => array('must' => array(array('match' => array('_id' => 1)), array('match' => array('field1' => 'value1')), array('match' => array('field2' => 'value2'))))))))); Indexer::delete($post); $this->index->refresh(); $this->assertEquals(0, $this->index->count()); }
function save_post($post_id) { if (is_object($post_id)) { $post = $post_id; } else { $post = get_post($post_id); } if ($post == null || !in_array($post->post_type, Config::types())) { return; } if ($post->post_status == 'publish') { Indexer::addOrUpdate($post); } else { Indexer::delete($post); } }
public function testSearchRangeLadderDown() { update_option('numeric', array('field1' => 1)); update_option('fields', array('field1' => 1)); update_option('field1_range', '-30,-20,-10'); register_post_type('post'); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 1, 'field1' => 5)); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 2, 'field1' => 15)); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 3, 'field1' => 17)); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 4, 'field1' => 23)); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 5, 'field1' => 25)); Indexer::addOrUpdate((object) array('post_type' => 'post', 'post_date' => '10/24/1988 00:00:00 CST', 'ID' => 6, 'field1' => 27)); $this->index->refresh(); $results = $this->searcher->search(null, 0, 10, array('field1' => '-30')); $this->assertEquals(6, $results['total']); $this->assertEquals(0, count(array_diff(array(4, 5, 1, 6, 2, 3), $results['ids']))); $this->assertEquals(array('field1' => array('-10' => 1, '-20' => 3, '-30' => 6)), $results['facets']); $results = $this->searcher->search(null, 0, 10, array('field1' => '-20')); $this->assertEquals(3, $results['total']); $this->assertEquals(0, count(array_diff(array(1, 2, 3), $results['ids']))); $this->assertEquals(array('field1' => array('-10' => 1, '-20' => 3, '-30' => 3)), $results['facets']); $results = $this->searcher->search(null, 0, 10, array('field1' => '-10')); $this->assertEquals(1, $results['total']); $this->assertEquals(0, count(array_diff(array(1), $results['ids']))); $this->assertEquals(array('field1' => array('-10' => 1, '-20' => 1, '-30' => 1)), $results['facets']); }