1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Search_Elastic_BulkIndexingTest extends PHPUnit_Framework_TestCase 9{ 10 function testBasicBulk() 11 { 12 $parts = []; 13 $bulk = new Search_Elastic_BulkOperation( 14 10, 15 function ($data) use (& $parts) { 16 $parts[] = $data; 17 }, 18 '_doc' 19 ); 20 21 $bulk->index('test', 'foo', 1, ['a' => 1]); 22 $bulk->index('test', 'foo', 2, ['a' => 2]); 23 $bulk->index('test', 'foo', 3, ['a' => 3]); 24 $bulk->unindex('test', 'bar', 4); 25 $bulk->flush(); 26 27 $this->assertCount(1, $parts); 28 29 $this->assertContains(json_encode(['a' => 3]) . "\n", $parts[0]); 30 $this->assertContains(json_encode(['index' => ['_index' => 'test', '_id' => 'foo-2', '_type' => '_doc']]) . "\n", $parts[0]); 31 $this->assertContains(json_encode(['delete' => ['_index' => 'test', '_id' => 'bar-4', '_type' => '_doc']]) . "\n", $parts[0]); 32 } 33 34 function testDoubleFlushHasNoImpact() 35 { 36 $parts = []; 37 $bulk = new Search_Elastic_BulkOperation( 38 10, 39 function ($data) use (& $parts) { 40 $parts[] = $data; 41 }, 42 '_doc' 43 ); 44 45 $bulk->index('test', 'foo', 1, ['a' => 1]); 46 $bulk->index('test', 'foo', 2, ['a' => 2]); 47 $bulk->index('test', 'foo', 3, ['a' => 3]); 48 $bulk->unindex('test', 'bar', 4); 49 $bulk->flush(); 50 $bulk->flush(); 51 52 $this->assertCount(1, $parts); 53 } 54 55 function testAutomaticFlushWhenLimitReached() 56 { 57 $parts = []; 58 $bulk = new Search_Elastic_BulkOperation( 59 10, 60 function ($data) use (& $parts) { 61 $parts[] = $data; 62 }, 63 '_doc' 64 ); 65 66 foreach (range(1, 15) as $i) { 67 $bulk->index('test', 'foo', $i, ['a' => $i]); 68 } 69 70 $bulk->flush(); 71 72 $this->assertCount(2, $parts); 73 } 74 75 function testFlushOnLimit() 76 { 77 $parts = []; 78 $bulk = new Search_Elastic_BulkOperation( 79 15, 80 function ($data) use (& $parts) { 81 $parts[] = $data; 82 }, 83 '_doc' 84 ); 85 86 foreach (range(1, 45) as $i) { 87 $bulk->index('test', 'foo', $i, ['a' => $i]); 88 } 89 90 $this->assertCount(3, $parts); 91 92 $bulk->flush(); // Does nothing 93 94 $this->assertCount(3, $parts); 95 } 96} 97