|
20 | 20 | SearchIn,
|
21 | 21 | SearchMoreLikeThis,
|
22 | 22 | SearchPhrase,
|
| 23 | + SearchQueryString, |
23 | 24 | SearchRange,
|
24 | 25 | SearchRegex,
|
25 | 26 | SearchScoreOption,
|
@@ -359,6 +360,68 @@ def test_str_returns_expected_format(self):
|
359 | 360 | )
|
360 | 361 |
|
361 | 362 |
|
| 363 | +@skipUnlessDBFeature("supports_atlas_search") |
| 364 | +class SearchQueryStringTests(SearchUtilsMixin): |
| 365 | + @classmethod |
| 366 | + def setUpClass(cls): |
| 367 | + cls.create_search_index( |
| 368 | + Article, |
| 369 | + "query_string_index", |
| 370 | + { |
| 371 | + "mappings": { |
| 372 | + "dynamic": False, |
| 373 | + "fields": { |
| 374 | + "headline": {"type": "string"}, |
| 375 | + "body": {"type": "string"}, |
| 376 | + }, |
| 377 | + } |
| 378 | + }, |
| 379 | + ) |
| 380 | + |
| 381 | + def setUp(self): |
| 382 | + self.mars_mission = Article.objects.create( |
| 383 | + headline="space exploration", |
| 384 | + body="NASA launches a new mission to Mars", |
| 385 | + number=1, |
| 386 | + ) |
| 387 | + self.exoplanet = Article.objects.create( |
| 388 | + headline="space exploration", |
| 389 | + body="Astronomers discover exoplanets orbiting distant stars", |
| 390 | + number=2, |
| 391 | + ) |
| 392 | + Article.objects.create( |
| 393 | + headline="other news", |
| 394 | + body="Local team wins championship", |
| 395 | + number=3, |
| 396 | + ) |
| 397 | + |
| 398 | + def test_search_query_string_basic(self): |
| 399 | + qs = Article.objects.annotate( |
| 400 | + score=SearchQueryString(path="headline", query="space AND body:Mars") |
| 401 | + ) |
| 402 | + self.assertCountEqual(qs, [self.mars_mission]) |
| 403 | + |
| 404 | + def test_search_query_string_with_score(self): |
| 405 | + constant_score = SearchScoreOption({"constant": {"value": 10}}) |
| 406 | + qs = Article.objects.annotate( |
| 407 | + score=SearchQueryString( |
| 408 | + path="headline", |
| 409 | + query="space AND body:exoplanets", |
| 410 | + score=constant_score, |
| 411 | + ) |
| 412 | + ) |
| 413 | + self.assertCountEqual(qs, [self.exoplanet]) |
| 414 | + self.assertAlmostEqual(qs.first().score, 10.0, places=2) |
| 415 | + |
| 416 | + def test_str_returns_expected_format(self): |
| 417 | + score = SearchScoreOption({"constant": {"value": 10}}) |
| 418 | + se = SearchQueryString(path="body", query="space AND body:exoplanets", score=score) |
| 419 | + self.assertEqual( |
| 420 | + str(se), |
| 421 | + f"SearchQueryString(path='body', query='space AND body:exoplanets, score={score})>", |
| 422 | + ) |
| 423 | + |
| 424 | + |
362 | 425 | class SearchRangeTests(SearchUtilsMixin):
|
363 | 426 | @classmethod
|
364 | 427 | def setUpClass(cls):
|
|
0 commit comments