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 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 phraseSearch(self, field, words): "Search for a phrase (given as a list of words) in C{field}." q = PhraseQuery() for word in words: t = Term(field, word) q.add(t) return q
def phraseSearch(qStr, field='body'): """Find all docs containing the phrase C{qStr}.""" parts = qStr.split() q = PhraseQuery() for p in parts: t = Term(field, p) q.add(t) return q
def delete(self, **kw): "Delete the first document containing the specified term. See also L{deleteAll}." # Not very efficient for bulk deletes # Use deleteAll for bulk deletes self._setupSearcher() if len(kw) != 1: raise RuntimeError, 'one and only one field for the moment' field, value = kw.items()[0] t = Term(field, value) self.searcher.reader.deleteTerm(t)
def deleteAll(self, **kw): "Remove all documents containing this field and value." self.close() reader = indexsearcher.open(self.name) if len(kw) != 1: raise RuntimeError, 'one and only one field for the moment' field, values = kw.items()[0] for value in values: t = Term(field, value) reader.deleteTerm(t) # commit the deletes reader.close()
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 titleSearch(qStr): t = Term('title', qStr) q = TermQuery(t) return q
def termSearch(qStr): t = Term('text', qStr) q = TermQuery(t) return q
def termSearch(qStr): t = Term('body', qStr) q = TermQuery(t) return q
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
def termSearch(self, field, term): "Search for a single C{term} in a C{field}." t = Term(field, term) q = TermQuery(t) return q