+ # qrys = [self.make_phrase(self.get_tokens(searched, field=fld), field=fld, fuzzy=fuzzy) for fld in fields_to_search]
+
+ # books = []
+ # for q in qrys:
+ # top = self.searcher.search(q,
+ # self.chain_filters([only_in, self.term_filter(Term('is_book', 'true'))]),
+ # max_results)
+ # for found in top.scoreDocs:
+ # books.append(SearchResult(self, found, how_found="search_perfect_book"))
+ # return books
+
+ # def search_book(self, searched, max_results=20, fuzzy=False, hint=None):
+ # fields_to_search = ['tags', 'authors', 'title']
+
+ # only_in = None
+ # if hint:
+ # if not hint.should_search_for_book():
+ # return []
+ # fields_to_search = hint.just_search_in(fields_to_search)
+ # only_in = hint.book_filter()
+
+ # tokens = self.get_tokens(searched, field='SIMPLE')
+
+ # q = BooleanQuery()
+
+ # for fld in fields_to_search:
+ # q.add(BooleanClause(self.make_term_query(tokens, field=fld,
+ # fuzzy=fuzzy), BooleanClause.Occur.SHOULD))
+
+ # books = []
+ # top = self.searcher.search(q,
+ # self.chain_filters([only_in, self.term_filter(Term('is_book', 'true'))]),
+ # max_results)
+ # for found in top.scoreDocs:
+ # books.append(SearchResult(self, found, how_found="search_book"))
+
+ # return books
+
+ # def search_perfect_parts(self, searched, max_results=20, fuzzy=False, hint=None):
+ # """
+ # Search for book parts which contains a phrase perfectly matching (with a slop of 2, default for make_phrase())
+ # some part/fragment of the book.
+ # """
+ # qrys = [self.make_phrase(self.get_tokens(searched), field=fld, fuzzy=fuzzy) for fld in ['text']]
+
+ # flt = None
+ # if hint:
+ # flt = hint.part_filter()
+
+ # books = []
+ # for q in qrys:
+ # top = self.searcher.search(q,
+ # self.chain_filters([self.term_filter(Term('is_book', 'true'), inverse=True),
+ # flt]),
+ # max_results)
+ # for found in top.scoreDocs:
+ # books.append(SearchResult(self, found, snippets=self.get_snippets(found, q), how_found='search_perfect_parts'))
+
+ # return books
+
+ def search_everywhere(self, searched, query_terms=None):
+ """
+ Tries to use search terms to match different fields of book (or its parts).
+ E.g. one word can be an author survey, another be a part of the title, and the rest
+ are some words from third chapter.
+ """
+ books = []
+ # content only query : themes x content
+ q = self.make_term_query(searched, 'text')
+ q_themes = self.make_term_query(searched, 'themes_pl')
+
+ query = self.index.query(q).query(q_themes).field_limit(score=True, all_fields=True)
+ res = query.execute()
+
+ for found in res:
+ books.append(SearchResult(found, how_found='search_everywhere_themesXcontent', query_terms=query_terms))
+
+ # query themes/content x author/title/tags
+ in_content = self.index.Q()
+ in_meta = self.index.Q()
+
+ for fld in ['themes_pl', 'text']:
+ in_content |= self.make_term_query(searched, field=fld)
+
+ for fld in ['tags', 'authors', 'title']:
+ in_meta |= self.make_term_query(searched, field=fld)
+
+ q = in_content & in_meta
+ res = self.index.query(q).field_limit(score=True, all_fields=True).execute()
+
+ for found in res:
+ books.append(SearchResult(found, how_found='search_everywhere', query_terms=query_terms))
+
+ return books
+
+ def get_snippets(self, searchresult, query, field='text', num=1):
+ """
+ Returns a snippet for found scoreDoc.
+ """
+ maxnum = len(searchresult)
+ if num is None or num < 0 or num > maxnum:
+ num = maxnum
+ book_id = searchresult.book_id
+ revision = searchresult.snippet_revision()
+ snippets = Snippets(book_id, revision=revision)
+ snips = [None] * maxnum
+ try:
+ snippets.open()
+ idx = 0
+ while idx < maxnum and num > 0:
+ position, length = searchresult.snippet_pos(idx)
+ if position is None or length is None:
+ continue
+ text = snippets.get((int(position),
+ int(length)))
+ snip = self.index.highlight(text=text, field=field, q=query)
+ snips[idx] = snip
+ if snip:
+ num -= 1
+ idx += 1
+
+ except IOError, e:
+ log.error("Cannot open snippet file for book id = %d [rev=%d], %s" % (book_id, revision, e))
+ return []
+ finally:
+ snippets.close()
+
+ # remove verse end markers..
+ snips = map(lambda s: s and s.replace("/\n", "\n"), snips)
+
+ searchresult.snippets = snips
+
+ return snips
+
+ def hint_tags(self, query, pdcounter=True, prefix=True):