def _base_query(phrase): q = BooleanQuery() p1 = _phrase_query("author", phrase) p2 = _phrase_query("title", phrase) #p2.setBoost(2) q.add(p2, required = False, prohibited = False) q.add(p1, required = False, prohibited = False) return q
def boolSearch(ands=[], ors=[], nots=[]): """Build a simple boolean query. each word in B{ands} is equiv to +word each word in B{ors} is equiv to word each word in B{nots} is equiv to -word e.g. boolSearch(['spam'], ['eggs'], ['parrot', 'cheese']) is equiv to +spam eggs -parrot -cheese in Google/Lucene syntax""" q = BooleanQuery() for a in ands: t = Term('text', a) tq = TermQuery(t) q.add(tq, True, False) for a in ors: t = Term('text', a) tq = TermQuery(t) q.add(tq, False, False) for a in nots: t = Term('text', a) tq = TermQuery(t) q.add(tq, False, True) return q
def _formats_query(main_query, formats): if formats is None: return main_query q = BooleanQuery() fq = BooleanQuery() for f in formats: t = Term("formats", f.lower()) tq = TermQuery(t) fq.add(tq, required = False, prohibited = False) q.add(fq, required = True, prohibited = False) q.add(main_query, required = True, prohibited = False) return q
def boolSearch(self, field, ands=[], ors=[], nots=[]): """Build a simple boolean query. Each word in C{ands} is equiv to +word Each word in C{ors} is equiv to word Each word in C{nots} is equiv to -word E.g. C{boolSearch(['spam'], ['eggs'], ['parrot', 'cheese'])} is equiv to C{+spam eggs -parrot -cheese} in Google/Lucene syntax. """ q = BooleanQuery() for a in ands: t = Term(field, a) tq = TermQuery(t) q.add(tq, True, False) for a in ors: t = Term(field, a) tq = TermQuery(t) q.add(tq, False, False) for a in nots: t = Term(field, a) tq = TermQuery(t) q.add(tq, False, True) return q
def find(self, qStr): """Perform a search in any field in this index. If the search string is enclosed in double quotes, a phrase search will be run; otherwise, the search will be for documents containing all words specified.""" self._setupSearcher() fields = self.searcher.fieldNames() if not fields: return [] all = [self.parse(field, qStr) for field in fields] if len(all) is 1: # simple case return self.searcher.search(all[0]) q = BooleanQuery() for query in all: # OR all of the field queries q.add(query, False, False) hits = self.searcher.search(q) return hits
def _phrase_query(field, phrase): q = BooleanQuery() for p in phrase.split(): t = Term(field, p.lower()) q.add(TermQuery(t), required = True, prohibited = False) return q