| 81 | |
| 82 | |
| 83 | class SearchQueryResult(QueryResult): |
| 84 | FACETS = { |
| 85 | "collection_id": CollectionFacet, |
| 86 | "match_collection_id": CollectionFacet, |
| 87 | "languages": LanguageFacet, |
| 88 | "language": LanguageFacet, |
| 89 | "countries": CountryFacet, |
| 90 | "country": CountryFacet, |
| 91 | "category": CategoryFacet, |
| 92 | "schema": SchemaFacet, |
| 93 | "schemata": SchemaFacet, |
| 94 | "event": EventFacet, |
| 95 | "entity": EntityFacet, |
| 96 | } |
| 97 | |
| 98 | def __init__(self, request, query): |
| 99 | super(SearchQueryResult, self).__init__(request, parser=query.parser) |
| 100 | self.query = query |
| 101 | result = query.search() |
| 102 | hits = result.get("hits", {}) |
| 103 | total = hits.get("total", {}) |
| 104 | self.total = total.get("value") |
| 105 | self.total_type = total.get("relation") |
| 106 | self.aggregations = result.get("aggregations") |
| 107 | for doc in hits.get("hits", []): |
| 108 | # log.info("Res: %s", pformat(doc)) |
| 109 | doc = unpack_result(doc) |
| 110 | if doc is not None: |
| 111 | self.results.append(doc) |
| 112 | |
| 113 | def get_facets(self): |
| 114 | facets = {} |
| 115 | for name in self.parser.facet_names: |
| 116 | facet_type = self.parser.get_facet_type(name) |
| 117 | |
| 118 | if facet_type is None: |
| 119 | facet_type = name |
| 120 | |
| 121 | facet_cls = self.FACETS.get(facet_type, Facet) |
| 122 | facets[name] = facet_cls(name, self.aggregations, self.parser) |
| 123 | return facets |
| 124 | |
| 125 | def to_dict(self, serializer=None): |
| 126 | data = super(SearchQueryResult, self).to_dict(serializer=serializer) |
| 127 | data["facets"] = self.get_facets() |
| 128 | data["query_text"] = self.query.to_text() |
| 129 | return data |