def findall(self, description, location):
        """Find hotels using full text search"""
        # Requires FTS index called 'hotels'
        # TODO auto create index if missing
        qp = FT.ConjunctionQuery(FT.TermQuery(term='hotel', field='type'))
        if location != '*':
            qp.conjuncts.append(
                FT.DisjunctionQuery(
                    FT.MatchPhraseQuery(location, field='country'),
                    FT.MatchPhraseQuery(location, field='city'),
                    FT.MatchPhraseQuery(location, field='state'),
                    FT.MatchPhraseQuery(location, field='address')))

        if description != '*':
            qp.conjuncts.append(
                FT.DisjunctionQuery(
                    FT.MatchPhraseQuery(description, field='description'),
                    FT.MatchPhraseQuery(description, field='name')))

        q = db.search('hotels', qp, limit=100)
        results = []
        for row in q:
            subdoc = db.retrieve_in(row['id'], 'country', 'city', 'state',
                                    'address', 'name', 'description', 'title',
                                    'phone', 'free_internet', 'pets_ok',
                                    'free_parking', 'email', 'free_breakfast')

            # Get the fields from the document, if they exist
            addr = ', '.join(x for x in (subdoc.get(y)[1]
                                         for y in ('address', 'city', 'state',
                                                   'country')) if x)
            subresults = {
                'name': subdoc['name'],
                'description': subdoc['description'],
                'address': addr,
                'title': subdoc['title'],
                'phone': subdoc['phone'],
                'free_internet': subdoc['free_internet'],
                'pets_ok': subdoc['pets_ok'],
                'free_parking': subdoc['free_parking'],
                'email': subdoc['email'],
                'free_breakfast': subdoc['free_breakfast'],
                'id': row['id']
            }
            results.append(subresults)

        response = {'data': results}
        return jsonify(response)
Beispiel #2
0
    def test_disjunction_query(self):
        dq = cbft.DisjunctionQuery()
        self.assertEqual(1, dq.min)
        self.assertRaises(cbft.NoChildrenError, getattr, dq, 'encodable')

        dq.disjuncts.append(cbft.PrefixQuery('somePrefix'))
        self.assertEqual({'min': 1, 'disjuncts': [{'prefix': 'somePrefix'}]},
                         dq.encodable)
        self.assertRaises(ValueError, setattr, dq, 'min', 0)
        dq.min = 2
        self.assertRaises(cbft.NoChildrenError, getattr, dq, 'encodable')