Beispiel #1
0
    def test_match_phrase(self):
        exp_json = {
            'query': {
                'match_phrase': 'salty beers',
                'analyzer': 'analyzer',
                'boost': 1.5,
                'field': 'field'
            },
            'size': 10,
            'indexName': 'ix'
        }

        p = cbft.Params(limit=10)
        q = cbft.MatchPhraseQuery('salty beers', boost=1.5, analyzer='analyzer',
                                  field='field')
        self.assertEqual(exp_json, cbft.make_search_body('ix', q, p))
    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)