Repository: internetarchive/openlibrary

Issue:
Add autocomplete to subject + people + places
I would like to have list of prompts when typing info for books. Preferably, with number of usage of this certain subject.

Example from Russian fan fiction site ficbook.net (search for users).
![image](https://user-images.githubusercontent.com/58372007/149519467-511df2d7-25b2-4343-86e7-7ffac01eca9f.png)




Discussion:
I would like to work on this issue
This actually, as I discussed in #6074 was also discussed at the same meeting - we all talked about which keywords should be pulled out of books.

We thought about how the most common words can be pulled out, but we realize that those would not be representative of the book itself, as you'd get issues like having the word 'the' or 'a', prepositions, etc. popping up. Then we thought about having the unique words come up, but those might just be for one sentence and doesn't represent the book at all.

Then maybe the most common unique word can be used, but that might just be referenced without it being about the book at all.

I actually suggested having the keywords from google books be brought into the Open Library. I actually feel like I already wrote this out somewhere, but I can't find it

@tuminzee maybe what I suggested will help you out.

Google books used to show all the frequent, unique words in a book - but they don't anymore, so it's not possible to port them over. Maybe that's why we didn't go through with that.
this is related to #4027 , because the issue is keywords aren't the same as what the book's about itself.
I think I found the reason for this problem here: #584
Sorry @BrittanyBunk it took me some time to reply to this, but I really like the early references which you have tagged here
@tuminzee if you still would like to work on this issue, would you like to propose a small technical plan + design and bring to an upcoming community call for discussion? 
@mekarpeles yes I will do that :)
sorry @mekarpeles I wouldn't be able to finish it right now. 
I will un-assign myself from the task

Resolving patch:
diff --git a/openlibrary/plugins/upstream/addbook.py b/openlibrary/plugins/upstream/addbook.py
--- a/openlibrary/plugins/upstream/addbook.py
+++ b/openlibrary/plugins/upstream/addbook.py
@@ -1113,6 +1113,33 @@ def GET(self):
         return to_json(docs)
 
 
+class subjects_autocomplete(delegate.page):
+    path = "/subjects_autocomplete"
+    # can't use /subjects/_autocomplete because the subjects endpoint = /subjects/[^/]+
+
+    def GET(self):
+        i = web.input(q="", type="", limit=5)
+        i.limit = safeint(i.limit, 5)
+
+        solr = get_solr()
+        prefix_q = solr.escape(i.q).strip()
+        solr_q = f'name:({prefix_q}*)'
+        fq = f'type:subject AND subject_type:{i.type}' if i.type else 'type:subject'
+
+        params = {
+            'fl': 'key,name,subject_type,work_count',
+            'q_op': 'AND',
+            'fq': fq,
+            'sort': 'work_count desc',
+            'rows': i.limit,
+        }
+
+        data = solr.select(solr_q, **params)
+        docs = [{'key': d['key'], 'name': d['name']} for d in data['docs']]
+
+        return to_json(docs)
+
+
 class work_identifiers(delegate.view):
     # TODO: (cclauss) Fix typing in infogami.utils.delegate and remove type: ignore
     suffix = "identifiers"  # type: ignore[assignment]


Test patch: