Merge pull request #169 from mouse-reeve/self-connector-test

Sort by rank in local db full text search
This commit is contained in:
Mouse Reeve 2020-05-14 10:32:43 -07:00 committed by GitHub
commit c0fb3beb54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 14 deletions

View file

@ -1,5 +1,5 @@
''' using a fedireads instance as a source of book data '''
from django.contrib.postgres.search import SearchVector
from django.contrib.postgres.search import SearchRank, SearchVector
from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult
@ -14,20 +14,26 @@ class Connector(AbstractConnector):
def search(self, query):
''' right now you can't search fedireads sorry, but when
that gets implemented it will totally rule '''
vector = 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('asin', weight='B') +\
SearchVector('oclc_number', weight='B') +\
SearchVector('remote_id', weight='B') +\
SearchVector('description', weight='C') +\
SearchVector('series', weight='C')
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('asin', weight='B') +\
SearchVector('oclc_number', weight='B') +\
SearchVector('remote_id', weight='B') +\
SearchVector('description', weight='C') +\
SearchVector('series', weight='C')
).filter(search=query)
search=vector
).annotate(
rank=SearchRank(vector, query)
).filter(
rank__gt=0
).order_by('-rank')
results = results.filter(default=True) or results
search_results = []

View file

@ -0,0 +1,75 @@
''' testing book data connectors '''
import datetime
from django.test import TestCase
from fedireads import models
from fedireads.connectors.self_connector import Connector
from fedireads.settings import DOMAIN
class SelfConnector(TestCase):
def setUp(self):
models.Connector.objects.create(
identifier=DOMAIN,
name='Local',
local=True,
connector_file='self_connector',
base_url='https://%s' % DOMAIN,
books_url='https://%s/book' % DOMAIN,
covers_url='https://%s/images/covers' % DOMAIN,
search_url='https://%s/search?q=' % DOMAIN,
priority=1,
)
self.connector = Connector(DOMAIN)
self.work = models.Work.objects.create(
title='Example Work',
)
self.edition = models.Edition.objects.create(
title='Edition of Example Work',
author_text='Anonymous',
published_date=datetime.datetime(1980, 5, 10),
parent_work=self.work,
)
models.Edition.objects.create(
title='Another Edition',
parent_work=self.work,
series='Anonymous'
)
models.Edition.objects.create(
title='More Editions',
subtitle='The Anonymous Edition',
parent_work=self.work,
)
models.Edition.objects.create(
title='An Edition',
author_text='Fish',
parent_work=self.work
)
def test_format_search_result(self):
result = self.connector.format_search_result(self.edition)
self.assertEqual(result.title, 'Edition of Example Work')
self.assertEqual(result.key, self.edition.absolute_id)
self.assertEqual(result.author, 'Anonymous')
self.assertEqual(result.year, 1980)
def test_search_rank(self):
results = self.connector.search('Anonymous')
self.assertEqual(len(results), 3)
self.assertEqual(results[0].title, 'Edition of Example Work')
self.assertEqual(results[1].title, 'More Editions')
self.assertEqual(results[2].title, 'Another Edition')
def test_search_default_filter(self):
self.edition.default = True
self.edition.save()
results = self.connector.search('Anonymous')
self.assertEqual(len(results), 1)
self.assertEqual(results[0].title, 'Edition of Example Work')
results = self.connector.search('Fish')
self.assertEqual(len(results), 1)
self.assertEqual(results[0].title, 'An Edition')