20
20
21
21
use Couchbase \BooleanSearchQuery ;
22
22
use Couchbase \ClusterInterface ;
23
+ use Couchbase \CollectionInterface ;
23
24
use Couchbase \ConjunctionSearchQuery ;
24
25
use Couchbase \DateRangeSearchFacet ;
25
26
use Couchbase \DateRangeSearchQuery ;
26
27
use Couchbase \DisjunctionSearchQuery ;
27
28
use Couchbase \DocIdSearchQuery ;
29
+ use Couchbase \DurabilityLevel ;
28
30
use Couchbase \Exception \FeatureNotAvailableException ;
29
31
use Couchbase \Exception \IndexNotFoundException ;
30
32
use Couchbase \GeoBoundingBoxSearchQuery ;
52
54
use Couchbase \TermRangeSearchQuery ;
53
55
use Couchbase \TermSearchFacet ;
54
56
use Couchbase \TermSearchQuery ;
57
+ use Couchbase \UpsertOptions ;
55
58
use Couchbase \VectorQuery ;
56
59
use Couchbase \VectorQueryCombination ;
57
60
use Couchbase \VectorSearch ;
63
66
class SearchTest extends Helpers \CouchbaseTestCase
64
67
{
65
68
private ClusterInterface $ cluster ;
69
+ private CollectionInterface $ collection ;
66
70
private SearchIndexManager $ indexManager ;
67
71
72
+ /**
73
+ * @return number of the documents in dataset
74
+ */
75
+ public function loadDataset (): int
76
+ {
77
+ $ dataset = json_decode (file_get_contents (__DIR__ . "/beer-data.json " ), true );
78
+
79
+ $ options = UpsertOptions::build ()->durabilityLevel (DurabilityLevel::MAJORITY_AND_PERSIST_TO_ACTIVE );
80
+ foreach ($ dataset as $ id => $ document ) {
81
+ $ this ->collection ->upsert ($ id , $ document , $ options );
82
+ }
83
+
84
+ return count ($ dataset );
85
+ }
86
+
87
+ public function createSearchIndex (int $ datasetSize ): void
88
+ {
89
+ fprintf (STDERR , "Create 'beer-search' to index %d docs \n" , $ datasetSize );
90
+ $ indexDump = json_decode (file_get_contents (__DIR__ . "/beer-search.json " ), true );
91
+ $ index = SearchIndex::build ("beer-search " , self ::env ()->bucketName ());
92
+ $ index ->setParams ($ indexDump ["params " ]);
93
+ $ this ->indexManager ->upsertIndex ($ index );
94
+
95
+ $ start = time ();
96
+ while (true ) {
97
+ try {
98
+ $ indexedDocuments = $ this ->indexManager ->getIndexedDocumentsCount ("beer-search " );
99
+ fprintf (STDERR , "%ds, Indexing 'beer-search': %d docs \n" , time () - $ start , $ indexedDocuments );
100
+ if ($ indexedDocuments >= $ datasetSize ) {
101
+ break ;
102
+ }
103
+ sleep (5 );
104
+ } catch (\Couchbase \Exception \IndexNotReadyException $ ex ) {
105
+ }
106
+ }
107
+ }
108
+
68
109
public function setUp (): void
69
110
{
70
111
parent ::setUp ();
71
112
72
113
$ this ->cluster = $ this ->connectCluster ();
114
+ $ this ->collection = $ this ->openBucket (self ::env ()->bucketName ())->defaultCollection ();
73
115
74
116
if (self ::env ()->useCouchbase ()) {
75
117
$ this ->indexManager = $ this ->cluster ->searchIndexes ();
76
118
try {
77
119
$ this ->indexManager ->getIndex ("beer-search " );
78
120
} catch (IndexNotFoundException $ ex ) {
79
- $ indexDump = json_decode (file_get_contents (__DIR__ . "/beer-search.json " ), true );
80
- $ index = SearchIndex::build ("beer-search " , "beer-sample " );
81
- $ index ->setParams ($ indexDump ["params " ]);
82
- $ this ->indexManager ->upsertIndex ($ index );
83
- }
84
- while (true ) {
85
- try {
86
- $ indexedDocuments = $ this ->indexManager ->getIndexedDocumentsCount ("beer-search " );
87
- fprintf (STDERR , "Indexing 'beer-search': %d docs \n" , $ indexedDocuments );
88
- if ($ indexedDocuments > 7000 ) {
89
- break ;
90
- }
91
- sleep (3 );
92
- } catch (\Couchbase \Exception \IndexNotReadyException $ ex ) {
93
- }
121
+ $ this ->createSearchIndex ($ this ->loadDataset ());
94
122
}
95
123
}
96
124
}
@@ -159,6 +187,7 @@ public function testSearchWithNoHits()
159
187
$ this ->assertEquals (0 , $ result ->metaData ()->totalHits ());
160
188
}
161
189
190
+
162
191
public function testSearchWithConsistency ()
163
192
{
164
193
$ this ->skipIfCaves ();
@@ -173,8 +202,7 @@ public function testSearchWithConsistency()
173
202
$ this ->assertEmpty ($ result ->rows ());
174
203
$ this ->assertEquals (0 , $ result ->metaData ()->totalHits ());
175
204
176
- $ collection = $ this ->cluster ->bucket ('beer-sample ' )->defaultCollection ();
177
- $ result = $ collection ->upsert ($ id , ["type " => "beer " , "name " => $ id ]);
205
+ $ result = $ this ->collection ->upsert ($ id , ["type " => "beer " , "name " => $ id ]);
178
206
$ mutationState = new MutationState ();
179
207
$ mutationState ->add ($ result );
180
208
@@ -358,7 +386,7 @@ public function testCompoundSearchQueries()
358
386
$ disjunctionQuery = new DisjunctionSearchQuery ([$ nameQuery , $ descriptionQuery ]);
359
387
$ options = SearchOptions::build ()->fields (["type " , "name " , "description " ]);
360
388
$ result = $ this ->cluster ->searchQuery ("beer-search " , $ disjunctionQuery , $ options );
361
- $ this ->assertGreaterThan (1000 , $ result ->metaData ()->totalHits ());
389
+ $ this ->assertGreaterThan (20 , $ result ->metaData ()->totalHits ());
362
390
$ this ->assertNotEmpty ($ result ->rows ());
363
391
$ this ->assertMatchesRegularExpression ('/green/i ' , $ result ->rows ()[0 ]['fields ' ]['name ' ]);
364
392
$ this ->assertDoesNotMatchRegularExpression ('/hop/i ' , $ result ->rows ()[0 ]['fields ' ]['name ' ]);
@@ -434,18 +462,18 @@ public function testSearchWithFacets()
434
462
$ this ->assertNotNull ($ result ->facets ()['foo ' ]);
435
463
$ this ->assertEquals ('name ' , $ result ->facets ()['foo ' ]->field ());
436
464
$ this ->assertEquals ('ale ' , $ result ->facets ()['foo ' ]->terms ()[0 ]->term ());
437
- $ this ->assertGreaterThan (1000 , $ result ->facets ()['foo ' ]->terms ()[0 ]->count ());
465
+ $ this ->assertGreaterThan (10 , $ result ->facets ()['foo ' ]->terms ()[0 ]->count ());
438
466
439
467
$ this ->assertNotNull ($ result ->facets ()['bar ' ]);
440
468
$ this ->assertEquals ('updated ' , $ result ->facets ()['bar ' ]->field ());
441
469
$ this ->assertEquals ('old ' , $ result ->facets ()['bar ' ]->dateRanges ()[0 ]->name ());
442
- $ this ->assertGreaterThan (5000 , $ result ->facets ()['bar ' ]->dateRanges ()[0 ]->count ());
470
+ $ this ->assertGreaterThan (30 , $ result ->facets ()['bar ' ]->dateRanges ()[0 ]->count ());
443
471
444
472
$ this ->assertNotNull ($ result ->facets ()['baz ' ]);
445
473
$ this ->assertEquals ('abv ' , $ result ->facets ()['baz ' ]->field ());
446
474
$ this ->assertEquals ('light ' , $ result ->facets ()['baz ' ]->numericRanges ()[0 ]->name ());
447
475
$ this ->assertGreaterThan (0 , $ result ->facets ()['baz ' ]->numericRanges ()[0 ]->max ());
448
- $ this ->assertGreaterThan (100 , $ result ->facets ()['baz ' ]->numericRanges ()[0 ]->count ());
476
+ $ this ->assertGreaterThan (15 , $ result ->facets ()['baz ' ]->numericRanges ()[0 ]->count ());
449
477
}
450
478
451
479
public function testNullInNumericRangeFacet ()
0 commit comments