X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/442e7667f2d3ae96b873dacc1c7f980765ecc57a..cf287e8434ef1d5e84be9f6a3047f73eb0721285:/src/search/index.py diff --git a/src/search/index.py b/src/search/index.py index 4c278eab5..ab3286aee 100644 --- a/src/search/index.py +++ b/src/search/index.py @@ -20,6 +20,13 @@ from wolnelektury.utils import makedirs log = logging.getLogger('search') +if os.path.isfile(settings.SOLR_STOPWORDS): + stopwords = set( + line.decode('utf-8').strip() + for line in open(settings.SOLR_STOPWORDS) if not line.startswith('#')) +else: + stopwords = set() + class SolrIndex(object): def __init__(self, mode=None): @@ -566,10 +573,9 @@ class SearchResult(object): def merge(self, other): if self.book_id != other.book_id: - raise ValueError("this search result is or book %d; tried to merge with %d" % (self.book_id, other.book_id)) + raise ValueError("this search result is for book %d; tried to merge with %d" % (self.book_id, other.book_id)) self._hits += other._hits - if other.score > self.score: - self._score = other._score + self._score += max(other._score, 0) + 0.5 return self def get_book(self): @@ -731,14 +737,17 @@ class Search(SolrIndex): def search_words(self, words, fields, book=True): filters = [] for word in words: - word_filter = None - for field in fields: - q = self.index.Q(**{field: word}) - if word_filter is None: - word_filter = q - else: - word_filter |= q - filters.append(word_filter) + if word not in stopwords: + word_filter = None + for field in fields: + q = self.index.Q(**{field: word}) + if word_filter is None: + word_filter = q + else: + word_filter |= q + filters.append(word_filter) + if not filters: + return [] if book: query = self.index.query(is_book=True) else: