Save best-guess search results on import

This commit is contained in:
Mouse Reeve 2021-08-10 13:54:52 -07:00
parent ebabbf475a
commit fa396d4bc8
3 changed files with 41 additions and 5 deletions

View file

@ -76,9 +76,10 @@ def import_data(source, job_id):
item.save() item.save()
continue continue
if item.book: if item.book or item.book_guess:
item.save() item.save()
if item.book:
# shelves book and handles reviews # shelves book and handles reviews
handle_imported_book( handle_imported_book(
source, job.user, item, job.include_reviews, job.privacy source, job.user, item, job.include_reviews, job.privacy

View file

@ -0,0 +1,25 @@
# Generated by Django 3.2.4 on 2021-08-10 20:49
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0082_auto_20210806_2324"),
]
operations = [
migrations.AddField(
model_name="importitem",
name="book_guess",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="book_guess",
to="bookwyrm.book",
),
),
]

View file

@ -71,7 +71,13 @@ class ImportItem(models.Model):
index = models.IntegerField() index = models.IntegerField()
data = models.JSONField() data = models.JSONField()
book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True)
book_guess = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) book_guess = models.ForeignKey(
Book,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="book_guess",
)
fail_reason = models.TextField(null=True) fail_reason = models.TextField(null=True)
def resolve(self): def resolve(self):
@ -81,7 +87,11 @@ class ImportItem(models.Model):
else: else:
# don't fall back on title/author search is isbn is present. # don't fall back on title/author search is isbn is present.
# you're too likely to mismatch # you're too likely to mismatch
self.book = self.get_book_from_title_author() book, confidence = self.get_book_from_title_author()
if confidence > 0.999:
self.book = book
else:
self.book_guess = book
def get_book_from_isbn(self): def get_book_from_isbn(self):
"""search by isbn""" """search by isbn"""
@ -97,12 +107,12 @@ class ImportItem(models.Model):
"""search by title and author""" """search by title and author"""
search_term = construct_search_term(self.title, self.author) search_term = construct_search_term(self.title, self.author)
search_result = connector_manager.first_search_result( search_result = connector_manager.first_search_result(
search_term, min_confidence=0.999 search_term, min_confidence=0.1
) )
if search_result: if search_result:
# raises ConnectorException # raises ConnectorException
return search_result.connector.get_or_create_book(search_result.key) return search_result.connector.get_or_create_book(search_result.key)
return None return None, 0
@property @property
def title(self): def title(self):