示例#1
0
def test_requires():
    a = Term("f", u("a"))
    b = Term("f", u("b"))
    assert_equal(And([a, b]).requires(), set([a, b]))
    assert_equal(Or([a, b]).requires(), set())
    assert_equal(AndMaybe(a, b).requires(), set([a]))
    assert_equal(a.requires(), set([a]))
def test_requires():
    a = Term("f", u("a"))
    b = Term("f", u("b"))
    assert And([a, b]).requires() == set([a, b])
    assert Or([a, b]).requires() == set()
    assert AndMaybe(a, b).requires() == set([a])
    assert a.requires() == set([a])
示例#3
0
    def __init__(self, a, b):
        """
        :param a: the query to match.
        :param b: do not match any spans that overlap with spans from this
            query.
        """

        self.q = AndMaybe(a, b)
        self.a = a
        self.b = b
示例#4
0
 def search(self, query_string, limit=25, cwd=os.getcwd()):
     '''
     Searches the index given a query string.
     '''
     with self.ix.searcher() as searcher:
         # parse user query
         query = self.query_parser.parse(query_string)
         # improve the score of files in the same directory
         query = AndMaybe(query, Term('dirname', cwd))
         results = searcher.search(query, limit=limit)
         # return dict copies: results not valid outside of context
         return ([dict(
             basename=result['basename'],
             dirname=result['dirname'],
             path=result['path']
         ) for result in results], len(results))
示例#5
0
    def parse(self, input, normalize=True):
        reqs, opts, nots, phrase = self._sort(self._split(input))
        make_clause = self.make_clause
        make_filter_clause = self.make_filter_clause

        reqs = [make_clause(text) for text in reqs]
        opts = [make_clause(text) for text in opts]
        nots = [make_filter_clause(text) for text in nots]

        pctmatch = int((len(reqs) + len(opts)) * self.minpercent) - len(reqs)
        minmatch = max(pctmatch, self.minmatch - len(reqs), 0)

        q = Or(opts, minmatch=minmatch)
        if reqs: q = AndMaybe(And(reqs), q)
        if nots: q = AndNot(q, Or(nots))

        if normalize:
            q = q.normalize()
        return q
示例#6
0
def whoosh_search(user_id, query_terms):
    ret = {}
    user_artists_profile = get_user_artists_profile(user_id)
    # q = qp.parse(
    #     "(" + query + " ANDMAYBE ((" + ") OR (".join([
    #         (" artist_id:" + str(artist_id) + "^" + str(1 + artist_score)) for (
    #             artist_id, artist_score) in user_artists_profile]) + ")))")
    # see https://pythonhosted.org/Whoosh/api/query.html#whoosh.query.AndMaybe
    q = AndMaybe(
        And([Term('title', qt) for qt in query_terms.split(' ')]),
        Or([
            Term('artist_id', artist_id, boost=artist_score)
            for (artist_id, artist_score) in user_artists_profile
        ]))
    with ix.searcher() as searcher:
        results = searcher.search(q, limit=10)
        ret = {
            'items': [hit.fields() for hit in results],
            'runtime': results.runtime
        }
    return ret