1
+ from typing import cast
2
+ import typing
1
3
import httpx
4
+ from openlibrary .solr .solr_types import SolrDocument
2
5
from openlibrary .solr .updater .abstract import AbstractSolrBuilder , AbstractSolrUpdater
3
6
from openlibrary .solr .utils import SolrUpdateRequest , get_solr_base_url
7
+ from openlibrary .solr .data_provider import WorkReadingLogSolrSummary
8
+ from openlibrary .core .ratings import WorkRatingsSummary , Ratings
9
+
10
+
11
+ SUBJECT_FACETS = ['subject_facet' , 'time_facet' , 'person_facet' , 'place_facet' ]
4
12
5
13
6
14
class AuthorSolrUpdater (AbstractSolrUpdater ):
@@ -9,24 +17,34 @@ class AuthorSolrUpdater(AbstractSolrUpdater):
9
17
10
18
async def update_key (self , author : dict ) -> tuple [SolrUpdateRequest , list [str ]]:
11
19
author_id = author ['key' ].split ("/" )[- 1 ]
12
- facet_fields = ['subject' , 'time' , 'person' , 'place' ]
13
- base_url = get_solr_base_url () + '/select'
14
-
20
+ base_url = get_solr_base_url () + '/query'
21
+
22
+ json : dict [str , typing .Any ] = {
23
+ "params" : {
24
+ "json.nl" : "arrarr" ,
25
+ "q" : "author_key:%s " % author_id ,
26
+ "fl" : "title, subtitle" ,
27
+ "sort" : "edition_count desc" ,
28
+ },
29
+ 'facet' : {
30
+ "ratings_count_1" : "sum(ratings_count_1)" ,
31
+ "ratings_count_2" : "sum(ratings_count_2)" ,
32
+ "ratings_count_3" : "sum(ratings_count_3)" ,
33
+ "ratings_count_4" : "sum(ratings_count_4)" ,
34
+ "ratings_count_5" : "sum(ratings_count_5)" ,
35
+ "readinglog_count" : "sum(readinglog_count)" ,
36
+ "want_to_read_count" : "sum(want_to_read_count)" ,
37
+ "currently_reading_count" : "sum(currently_reading_count)" ,
38
+ "already_read_count" : "sum(already_read_count)" ,
39
+ },
40
+ }
41
+ for field in SUBJECT_FACETS :
42
+ json ["facet" ][field ] = {
43
+ "type" : "terms" ,
44
+ "field" : field ,
45
+ }
15
46
async with httpx .AsyncClient () as client :
16
- response = await client .get (
17
- base_url ,
18
- params = [ # type: ignore[arg-type]
19
- ('wt' , 'json' ),
20
- ('json.nl' , 'arrarr' ),
21
- ('q' , 'author_key:%s' % author_id ),
22
- ('sort' , 'edition_count desc' ),
23
- ('rows' , 1 ),
24
- ('fl' , 'title,subtitle' ),
25
- ('facet' , 'true' ),
26
- ('facet.mincount' , 1 ),
27
- ]
28
- + [('facet.field' , '%s_facet' % field ) for field in facet_fields ],
29
- )
47
+ response = await client .post (base_url , json = json )
30
48
reply = response .json ()
31
49
32
50
doc = AuthorSolrBuilder (author , reply ).build ()
@@ -85,8 +103,38 @@ def work_count(self) -> int:
85
103
@property
86
104
def top_subjects (self ) -> list [str ]:
87
105
all_subjects = []
88
- for counts in self ._solr_reply ['facet_counts' ]['facet_fields' ].values ():
89
- for s , num in counts :
90
- all_subjects .append ((num , s ))
106
+ for field in SUBJECT_FACETS :
107
+ if facet := self ._solr_reply ['facets' ].get (field ):
108
+ for bucket in facet ['buckets' ]:
109
+ all_subjects .append ((bucket .count , bucket .val ))
91
110
all_subjects .sort (reverse = True )
92
- return [s for num , s in all_subjects [:10 ]]
111
+ return [top_facets for num , top_facets in all_subjects [:10 ]]
112
+
113
+ def build (self ) -> SolrDocument :
114
+ doc = cast (dict , super ().build ())
115
+ doc |= self .build_ratings ()
116
+ doc |= self .build_reading_log ()
117
+ return cast (SolrDocument , doc )
118
+
119
+ def build_ratings (self ) -> WorkRatingsSummary :
120
+ return Ratings .work_ratings_summary_from_counts (
121
+ [
122
+ self ._solr_reply ["facets" ].get (f"ratings_count_{ index } " , 0 )
123
+ for index in range (1 , 6 )
124
+ ]
125
+ )
126
+
127
+ def build_reading_log (self ) -> WorkReadingLogSolrSummary :
128
+ reading_log = {
129
+ "want_to_read_count" : self ._solr_reply ["facets" ].get (
130
+ "want_to_read_count" , 0.0
131
+ ),
132
+ "already_read_count" : self ._solr_reply ["facets" ].get (
133
+ "already_read_count" , 0.0
134
+ ),
135
+ "currently_reading_count" : self ._solr_reply ["facets" ].get (
136
+ "currently_reading_count" , 0.0
137
+ ),
138
+ "readinglog_count" : self ._solr_reply ["facets" ].get ("readinglog_count" , 0.0 ),
139
+ }
140
+ return cast (WorkReadingLogSolrSummary , reading_log )
0 commit comments