Skip to content

Commit 3724311

Browse files
Add fields to stored hits (#73)
1 parent facd011 commit 3724311

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

elasticsearch_django/models.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,18 @@ def has_aggs(self) -> bool:
630630

631631
@property
632632
def has_highlights(self) -> bool:
633-
"""Return True if the query includes aggs."""
633+
"""Return True if the query includes highlights."""
634634
if not self.query:
635635
raise ValueError("Missing query attribute.")
636636
return "highlight" in self.query
637637

638+
@property
639+
def has_fields(self) -> bool:
640+
"""Return True if the query includes explicit fields."""
641+
if not self.query:
642+
raise ValueError("Missing query attribute.")
643+
return "fields" in self.query
644+
638645
def search_rank_annotation(self, pk_field_name: str = "pk") -> Case | None:
639646
"""Return SQL CASE statement used to annotate results with rank."""
640647
if not self.hits:
@@ -761,6 +768,8 @@ def _hit(hit: dict) -> dict:
761768
}
762769
if highlight := hit.get("highlight"):
763770
retval["highlight"] = highlight
771+
if fields := hit.get("fields"):
772+
retval["fields"] = fields
764773
return retval
765774

766775
return [_hit(h) for h in self.raw_hits]

tests/test_models.py

+52
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
SearchDocumentManagerMixin,
1717
SearchDocumentMixin,
1818
SearchQuery,
19+
SearchResponseParser,
1920
)
2021

2122
from .models import (
@@ -412,6 +413,57 @@ def test_from_search_results(self) -> None:
412413
assert obj.search_score == 3.0
413414

414415

416+
@pytest.mark.django_db
417+
class SearchResponseParserTests:
418+
def test_parse(self) -> None:
419+
response = mock.MagicMock(spec=ObjectApiResponse)
420+
response.body = {
421+
"took": 31,
422+
"timed_out": False,
423+
"_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0},
424+
"hits": {
425+
"total": {"value": 190, "relation": "eq"},
426+
"max_score": 7.563781,
427+
"hits": [
428+
{
429+
"_index": "foo",
430+
"_id": "123",
431+
"_score": 7.563781,
432+
"_source": {"country": "GB", "city": "London", "name": "Fred"},
433+
"fields": {"country": ["gb"], "city": ["london"]},
434+
"highlight": {"country": ["<em>gb</em>"]},
435+
}
436+
],
437+
},
438+
"aggregations": {
439+
"countries": {
440+
"doc_count_error_upper_bound": 0,
441+
"sum_other_doc_count": 0,
442+
"buckets": [{"key": "gb", "doc_count": 100}],
443+
}
444+
},
445+
}
446+
parser = SearchResponseParser(response)
447+
assert parser.total_hits == 190
448+
assert parser.total_hits_relation == "eq"
449+
assert parser.hits == [
450+
{
451+
"id": "123",
452+
"index": "foo",
453+
"score": 7.563781,
454+
"fields": {"city": ["london"], "country": ["gb"]},
455+
"highlight": {"country": ["<em>gb</em>"]},
456+
}
457+
]
458+
assert parser.aggregations == {
459+
"countries": {
460+
"doc_count_error_upper_bound": 0,
461+
"sum_other_doc_count": 0,
462+
"buckets": [{"key": "gb", "doc_count": 100}],
463+
}
464+
}
465+
466+
415467
@pytest.mark.django_db
416468
class ExecuteFunctionTests:
417469
raw_hits = [

0 commit comments

Comments
 (0)