|
15 | 15 | package searcher |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
| 19 | + "os" |
18 | 20 | "testing" |
19 | 21 |
|
20 | 22 | "github.com/blevesearch/bleve/v2/search" |
@@ -223,3 +225,135 @@ func TestDisjunctionSearchTooMany(t *testing.T) { |
223 | 225 | t.Fatal(err) |
224 | 226 | } |
225 | 227 | } |
| 228 | + |
| 229 | +func TestUnadornedDisjunctionAdvance(t *testing.T) { |
| 230 | + dir, _ := os.MkdirTemp("", "scorchTwoDoc") |
| 231 | + defer func() { |
| 232 | + _ = os.RemoveAll(dir) |
| 233 | + }() |
| 234 | + twoDocIndex := initTwoDocScorch(dir) |
| 235 | + twoDocIndexReader, err := twoDocIndex.Reader() |
| 236 | + if err != nil { |
| 237 | + t.Fatal(err) |
| 238 | + } |
| 239 | + defer func() { |
| 240 | + err := twoDocIndexReader.Close() |
| 241 | + if err != nil { |
| 242 | + t.Fatal(err) |
| 243 | + } |
| 244 | + }() |
| 245 | + getNewOptimizedCompositeSearcher := func(t *testing.T) search.Searcher { |
| 246 | + optimizedCompositeSearcherOptions := search.SearcherOptions{Explain: false, IncludeTermVectors: false, Score: "none"} |
| 247 | + martyTermSearcher, err := NewTermSearcher(context.Background(), twoDocIndexReader, "marty", "name", 1.0, optimizedCompositeSearcherOptions) |
| 248 | + if err != nil { |
| 249 | + t.Fatal(err) |
| 250 | + } |
| 251 | + dustinTermSearcher, err := NewTermSearcher(context.Background(), twoDocIndexReader, "dustin", "name", 1.0, optimizedCompositeSearcherOptions) |
| 252 | + if err != nil { |
| 253 | + t.Fatal(err) |
| 254 | + } |
| 255 | + steveTermSearcher, err := NewTermSearcher(context.Background(), twoDocIndexReader, "steve", "name", 1.0, optimizedCompositeSearcherOptions) |
| 256 | + if err != nil { |
| 257 | + t.Fatal(err) |
| 258 | + } |
| 259 | + martyOrDustinOrSteveSearcher, err := NewDisjunctionSearcher(context.Background(), twoDocIndexReader, []search.Searcher{martyTermSearcher, dustinTermSearcher, steveTermSearcher}, 0, optimizedCompositeSearcherOptions) |
| 260 | + if err != nil { |
| 261 | + t.Fatal(err) |
| 262 | + } |
| 263 | + return martyOrDustinOrSteveSearcher |
| 264 | + } |
| 265 | + martyOrDustinOrSteveSearcher := getNewOptimizedCompositeSearcher(t) |
| 266 | + ctx := &search.SearchContext{ |
| 267 | + DocumentMatchPool: search.NewDocumentMatchPool(martyOrDustinOrSteveSearcher.DocumentMatchPoolSize(), 0), |
| 268 | + } |
| 269 | + // get the correct order using only next calls |
| 270 | + dm, err := martyOrDustinOrSteveSearcher.Next(ctx) |
| 271 | + if err != nil { |
| 272 | + t.Fatal(err) |
| 273 | + } |
| 274 | + expectedDocIDs := []index.IndexInternalID{} |
| 275 | + for dm != nil && err == nil { |
| 276 | + expectedDocIDs = append(expectedDocIDs, dm.IndexInternalID) |
| 277 | + dm, err = martyOrDustinOrSteveSearcher.Next(ctx) |
| 278 | + } |
| 279 | + if err != nil { |
| 280 | + t.Fatal(err) |
| 281 | + } |
| 282 | + if len(expectedDocIDs) != 3 { |
| 283 | + t.Fatalf("expected 3 results, got %d", len(expectedDocIDs)) |
| 284 | + } |
| 285 | + // Test 1 - Advance in reverse direction after getting the correct order using only next calls |
| 286 | + // Next(->) - Next(->) - Next(->) - Advance(<-) - Advance(<-) |
| 287 | + for i := len(expectedDocIDs) - 1; i >= 0; i-- { |
| 288 | + xID := expectedDocIDs[i] |
| 289 | + dm, err = martyOrDustinOrSteveSearcher.Advance(ctx, xID) |
| 290 | + if err != nil { |
| 291 | + t.Fatal(err) |
| 292 | + } |
| 293 | + if dm == nil { |
| 294 | + t.Fatalf("expected to find %v", xID) |
| 295 | + } |
| 296 | + if !dm.IndexInternalID.Equals(xID) { |
| 297 | + t.Fatalf("expected %v, got %v", xID, dm.IndexInternalID) |
| 298 | + } |
| 299 | + } |
| 300 | + // Test 2 - Advance in forward direction after getting the correct order using only next calls |
| 301 | + // Next(->) - Next(->) - Next(->) - Advance(ResetTo0) - Advance(->) - Advance(->) |
| 302 | + martyOrDustinOrSteveSearcher = getNewOptimizedCompositeSearcher(t) |
| 303 | + for i := 0; i < len(expectedDocIDs); i++ { |
| 304 | + xID := expectedDocIDs[i] |
| 305 | + dm, err = martyOrDustinOrSteveSearcher.Advance(ctx, xID) |
| 306 | + if err != nil { |
| 307 | + t.Fatal(err) |
| 308 | + } |
| 309 | + if dm == nil { |
| 310 | + t.Fatalf("expected to find %v", xID) |
| 311 | + } |
| 312 | + if !dm.IndexInternalID.Equals(xID) { |
| 313 | + t.Fatalf("expected %v, got %v", xID, dm.IndexInternalID) |
| 314 | + } |
| 315 | + } |
| 316 | + // Test 3 - Alternate Next and Advance calls |
| 317 | + // Next(->) -> Next(->) -> Advance(<-) -> Next(->) -> Next(->) -> Advance(<-) -> Advance(<-) -> Next(->) |
| 318 | + martyOrDustinOrSteveSearcher = getNewOptimizedCompositeSearcher(t) |
| 319 | + goNext := func(expectedDocID index.IndexInternalID) { |
| 320 | + dm, err = martyOrDustinOrSteveSearcher.Next(ctx) |
| 321 | + if err != nil { |
| 322 | + t.Fatal(err) |
| 323 | + } |
| 324 | + if dm == nil { |
| 325 | + t.Fatal("expected a document, got nil") |
| 326 | + } |
| 327 | + if !dm.IndexInternalID.Equals(expectedDocID) { |
| 328 | + t.Fatalf("expected %v, got %v", expectedDocID, dm.IndexInternalID) |
| 329 | + } |
| 330 | + } |
| 331 | + goBack := func(goTo index.IndexInternalID) { |
| 332 | + dm, err = martyOrDustinOrSteveSearcher.Advance(ctx, goTo) |
| 333 | + if err != nil { |
| 334 | + t.Fatal(err) |
| 335 | + } |
| 336 | + if dm == nil { |
| 337 | + t.Fatalf("expected to find %v", goTo) |
| 338 | + } |
| 339 | + if !dm.IndexInternalID.Equals(goTo) { |
| 340 | + t.Fatalf("expected %v, got %v", goTo, dm.IndexInternalID) |
| 341 | + } |
| 342 | + } |
| 343 | + // Next (->) |
| 344 | + goNext(expectedDocIDs[0]) |
| 345 | + // Next (->) |
| 346 | + goNext(expectedDocIDs[1]) |
| 347 | + // Advance (<-) |
| 348 | + goBack(expectedDocIDs[0]) |
| 349 | + // Next (->) |
| 350 | + goNext(expectedDocIDs[1]) |
| 351 | + // Next (->) |
| 352 | + goNext(expectedDocIDs[2]) |
| 353 | + // Advance (<-) |
| 354 | + goBack(expectedDocIDs[1]) |
| 355 | + // Advance (<-) |
| 356 | + goBack(expectedDocIDs[0]) |
| 357 | + // Next (->) |
| 358 | + goNext(expectedDocIDs[1]) |
| 359 | +} |
0 commit comments