forked from mirrors/bookwyrm
Adds fulltext search of postgres
This commit is contained in:
parent
6e218a85de
commit
e3525b13f5
2 changed files with 61 additions and 5 deletions
|
@ -31,8 +31,21 @@ def load_more_data(book_id):
|
|||
|
||||
def search(query):
|
||||
''' try an external datasource for books '''
|
||||
self = self_connector()
|
||||
results = self.search(query)
|
||||
if len(results) >= 10:
|
||||
return results
|
||||
|
||||
connector = get_connector()
|
||||
return connector.search(query)
|
||||
external_results = connector.search(query)
|
||||
dedupe_slug = lambda r: '%s %s %s' % (r.title, r.author, r.year)
|
||||
result_index = [dedupe_slug(r) for r in results]
|
||||
for result in external_results:
|
||||
if dedupe_slug(result) in result_index:
|
||||
continue
|
||||
results.append(result)
|
||||
|
||||
return results
|
||||
|
||||
def update_book(book):
|
||||
''' re-sync with the original data source '''
|
||||
|
@ -40,12 +53,24 @@ def update_book(book):
|
|||
connector.update_book(book)
|
||||
|
||||
|
||||
def get_connector(book=None):
|
||||
def self_connector():
|
||||
''' load the connector for the local database '''
|
||||
return get_connector(self=True)
|
||||
|
||||
|
||||
def get_connector(book=None, self=False):
|
||||
''' pick a book data connector '''
|
||||
if book and book.connector:
|
||||
connector_info = book.connector
|
||||
elif self:
|
||||
connector_info = models.Connector.objects.filter(
|
||||
connector_file='self_connector'
|
||||
).first()
|
||||
else:
|
||||
connector_info = models.Connector.objects.first()
|
||||
# only select from external connectors
|
||||
connector_info = models.Connector.objects.exclude(
|
||||
connector_file='self_connector'
|
||||
).first()
|
||||
|
||||
connector = importlib.import_module(
|
||||
'fedireads.connectors.%s' % connector_info.connector_file
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
''' using a fedireads instance as a source of book data '''
|
||||
from django.contrib.postgres.search import SearchVector
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from fedireads import models
|
||||
from .abstract_connector import AbstractConnector
|
||||
from .abstract_connector import AbstractConnector, SearchResult
|
||||
|
||||
|
||||
class Connector(AbstractConnector):
|
||||
|
@ -14,7 +15,33 @@ class Connector(AbstractConnector):
|
|||
def search(self, query):
|
||||
''' right now you can't search fedireads sorry, but when
|
||||
that gets implemented it will totally rule '''
|
||||
return []
|
||||
results = models.Edition.objects.annotate(
|
||||
search=SearchVector('title', weight='A') +\
|
||||
SearchVector('subtitle', weight='B') +\
|
||||
SearchVector('author_text', weight='A') +\
|
||||
SearchVector('isbn_13', weight='A') +\
|
||||
SearchVector('isbn_10', weight='A') +\
|
||||
SearchVector('openlibrary_key', weight='B') +\
|
||||
SearchVector('goodreads_key', weight='B') +\
|
||||
SearchVector('source_url', weight='B') +\
|
||||
SearchVector('asin', weight='B') +\
|
||||
SearchVector('oclc_number', weight='B') +\
|
||||
SearchVector('description', weight='C') +\
|
||||
SearchVector('series', weight='C')
|
||||
).filter(search=query)
|
||||
|
||||
search_results = []
|
||||
for book in results[:10]:
|
||||
search_results.append(
|
||||
SearchResult(
|
||||
book.title,
|
||||
book.fedireads_key,
|
||||
book.author_text,
|
||||
book.published_date.year if book.published_date else None,
|
||||
None
|
||||
)
|
||||
)
|
||||
return search_results
|
||||
|
||||
|
||||
def get_or_create_book(self, fedireads_key):
|
||||
|
@ -38,3 +65,7 @@ class Connector(AbstractConnector):
|
|||
|
||||
def update_book(self, book_obj):
|
||||
pass
|
||||
|
||||
|
||||
def expand_book_data(self, book):
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue