View Single Post
Old 03-11-2024, 10:52 AM   #1
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Search for books with author notes and show one book per author

EDIT: See this post for an updated & generalized version of the following template search.

@Comfy.n asked me if there was a template search that for each author with a note would show one book by that author. It didn't matter which book. Here is that search.

Showing one book per author might not be possible if the author is a coauthor on several books. For example, consider these books where All of A, B, and C have notes:
  • Title 1, authors A & B
  • Title 2, authors C & B
Title 1 and Title 2 will both appear in the search results because there is no other way to show the authors A and C. This means that two books by author B will appear.

The search template gives priority to books with single authors. Extending the above example:
  • Title 1, authors A & B
  • Title 2, authors C & B
  • Title 3, author B
  • Title 4, author C
results in
  • Title 1 (only book with author A)
  • Title 3 (author B is sole author)
  • Title 4 (author C is sole author)
Title 3 is shown even though B is a coauthor of Title 1.

The template can easily be changed to check any field, for example series, tags, or some custom column.

The search respects virtual libraries.

Use the Advanced search / Template search to create the search.
Template value field: yes
Comparison type: Set/Not set
Template:
Spoiler:
Code:
python:
def evaluate(book, context):
	# Set this field to the lookup name of the desired field.
	field_name = 'authors'

	db = context.db.new_api
	# Check if we have already computed the necessary data
	book_ids = context.globals.get('book_ids', None)
	if book_ids is None:
		book_notes = {}
		all_notes = db.get_all_items_that_have_notes(field_name)
		for book_id in db.search(query=''): # restrict the search to the current VL
			item_values = db.field_for(field_name, book_id)
			if item_values is None:
				continue
			if isinstance(item_values, str):
				item_values = (item_values,)
			for item_val in item_values:
				item_id = db.get_item_id(field_name, item_val)
				if item_id in all_notes:
					if item_val not in book_notes or book_notes[item_val]['count'] > len(item_values):
						book_notes[item_val] = {'count': len(item_values), 'book_id': book_id}
		context.globals['book_ids'] = frozenset(bn['book_id'] for bn in book_notes.values())

	# Check if the current book is to be displayed
	book_ids = context.globals['book_ids']
	return '1' if book.id in book_ids else ''

Here is an image of the filled-in Advanced search dialog.
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.jpg
Views:	312
Size:	71.8 KB
ID:	206858  

Last edited by chaley; 03-13-2024 at 04:55 PM. Reason: Improve template performance
chaley is offline   Reply With Quote