Ejemplo n.º 1
0
    def fts(self, term, index_name, fuzziness):
        if not index_name:
            index_name = 'default'

        try:
            query = fts.TermQuery(
                term,
                fuzziness=fuzziness) if fuzziness else fts.TermQuery(term)
            results = self.bucket.search(index_name,
                                         query,
                                         limit=10,
                                         highlight_style='html')
            response = []
            for result in results:
                response.append({
                    'id': result['id'],
                    'hit': result['locations']
                })
            return response
        except Exception as error:
            print('Error performing FTS. Error: {0}'.format(error))
            raise
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test_fuzzy(self):
        q = cbft.TermQuery('someterm', field='field', boost=1.5,
                           prefix_length=23, fuzziness=12)
        p = cbft.Params(explain=True)

        exp_json = {
            'query': {
                'term': 'someterm',
                'boost': 1.5,
                'fuzziness':  12,
                'prefix_length': 23,
                'field': 'field'
            },
            'indexName': 'someIndex',
            'explain': True
        }

        self.assertEqual(exp_json, cbft.make_search_body('someIndex', q, p))