|
28 | 28 | SearchWildcard,
|
29 | 29 | )
|
30 | 30 |
|
31 |
| -from .models import Article, Writer |
| 31 | +from .models import Article, Location, Writer |
| 32 | + |
| 33 | + |
| 34 | +def wait_until_index_ready(collection, index_name, timeout: float = 30, interval: float = 0.5): |
| 35 | + start = monotonic() |
| 36 | + while monotonic() - start < timeout: |
| 37 | + indexes = list(collection.list_search_indexes()) |
| 38 | + for idx in indexes: |
| 39 | + if idx["name"] == index_name and idx["status"] == "READY": |
| 40 | + return True |
| 41 | + sleep(interval) |
| 42 | + raise TimeoutError(f"Index {index_name} not ready after {timeout} seconds") |
32 | 43 |
|
33 | 44 |
|
34 | 45 | def _delayed_assertion(timeout: float = 120, interval: float = 0.5):
|
@@ -76,6 +87,7 @@ def create_search_index(cls, model, index_name, definition, type="search"):
|
76 | 87 | collection = cls._get_collection(model)
|
77 | 88 | idx = SearchIndexModel(definition=definition, name=index_name, type=type)
|
78 | 89 | collection.create_search_index(idx)
|
| 90 | + wait_until_index_ready(collection, index_name) |
79 | 91 |
|
80 | 92 | def drop_index():
|
81 | 93 | collection.drop_search_index(index_name)
|
@@ -479,10 +491,13 @@ def setUpClass(cls):
|
479 | 491 |
|
480 | 492 | def setUp(self):
|
481 | 493 | self.article = Article.objects.create(
|
482 |
| - headline="any", number=1, body="", location={"type": "Point", "coordinates": [40, 5]} |
| 494 | + headline="any", number=1, body="", location=Location(type="Point", coordinates=[40, 5]) |
483 | 495 | )
|
484 | 496 | Article.objects.create(
|
485 |
| - headline="any", number=2, body="", location={"type": "Point", "coordinates": [400, 50]} |
| 497 | + headline="any", |
| 498 | + number=2, |
| 499 | + body="", |
| 500 | + location=Location(type="Point", coordinates=[400, 50]), |
486 | 501 | )
|
487 | 502 |
|
488 | 503 | def test_search_geo_shape(self):
|
@@ -524,10 +539,13 @@ def setUpClass(cls):
|
524 | 539 |
|
525 | 540 | def setUp(self):
|
526 | 541 | self.article = Article.objects.create(
|
527 |
| - headline="geo", number=2, body="", location={"type": "Point", "coordinates": [40, 5]} |
| 542 | + headline="geo", number=2, body="", location=Location(type="Point", coordinates=[40, 5]) |
528 | 543 | )
|
529 | 544 | Article.objects.create(
|
530 |
| - headline="geo2", number=3, body="", location={"type": "Point", "coordinates": [-40, -5]} |
| 545 | + headline="geo2", |
| 546 | + number=3, |
| 547 | + body="", |
| 548 | + location=Location(type="Point", coordinates=[-40, -5]), |
531 | 549 | )
|
532 | 550 |
|
533 | 551 | def test_search_geo_within(self):
|
@@ -598,7 +616,6 @@ def test_search_more_like_this(self):
|
598 | 616 | {"headline": self.article1.headline, "body": self.article1.body},
|
599 | 617 | {"headline": self.article2.headline, "body": self.article2.body},
|
600 | 618 | ]
|
601 |
| - like_docs = [{"body": "NASA launches new satellite to explore the galaxy"}] |
602 | 619 | qs = Article.objects.annotate(score=SearchMoreLikeThis(documents=like_docs)).order_by(
|
603 | 620 | "score"
|
604 | 621 | )
|
|
0 commit comments