def search(self, index, query, **kwargs):
        """
        Perform full-text searches

        .. versionadded:: 2.0.9

        .. warning::

            The full-text search API is experimental and subject to change

        :param str index: Name of the index to query
        :param couchbase_core.fulltext.SearchQuery query: Query to issue
        :param couchbase_core.fulltext.Params params: Additional query options
        :return: An iterator over query hits

        .. note:: You can avoid instantiating an explicit `Params` object
            and instead pass the parameters directly to the `search` method.

        .. code-block:: python

            it = cb.search('name', ft.MatchQuery('nosql'), limit=10)
            for hit in it:
                print(hit)

        """
        itercls = kwargs.pop('itercls', _FTS.SearchRequest)
        iterargs = itercls.mk_kwargs(kwargs)
        params = kwargs.pop('params', _FTS.Params(**kwargs))
        body = _FTS.make_search_body(index, query, params)
        return itercls(body, self, **iterargs)
Exemple #2
0
    def test_consistency(self):
        uuid = str('10000')
        vb = 42
        seq = 101
        ixname = 'ix'

        mutinfo = (vb, uuid, seq, 'dummy-bucket-name')
        ms = MutationState()
        ms._add_scanvec(mutinfo)

        params = cbft.Params()
        params.consistent_with(ms)
        got = cbft.make_search_body('ix', cbft.MatchNoneQuery(), params)
        exp = {
            'indexName': ixname,
            'query': {
                'match_none': None
            },
            'ctl': {
                'consistency': {
                    'level': 'at_plus',
                    'vectors': {
                        ixname: {
                            '{0}/{1}'.format(vb, uuid): seq
                        }
                    }
                }
            }
        }
        self.assertEqual(exp, got)
Exemple #3
0
    def test_facets(self):
        p = cbft.Params()
        f = cbft.NumericFacet('numfield')
        self.assertRaises(ValueError, p.facets.__setitem__, 'facetName', f)
        self.assertRaises(TypeError, f.add_range, 'range1')
        p.facets['facetName'] = f.add_range('range1', min=123, max=321)
        self.assertTrue('facetName' in p.facets)

        f = cbft.DateFacet('datefield')
        f.add_range('r1', start='2012', end='2013')
        f.add_range('r2', start='2014')
        f.add_range('r3', end='2015')
        exp = {
            'field':
            'datefield',
            'date_ranges': [{
                'name': 'r1',
                'start': '2012',
                'end': '2013'
            }, {
                'name': 'r2',
                'start': '2014'
            }, {
                'name': 'r3',
                'end': '2015'
            }]
        }
        self.assertEqual(exp, f.encodable)

        f = cbft.TermFacet('termfield')
        self.assertEqual({'field': 'termfield'}, f.encodable)
        f.limit = 10
        self.assertEqual({'field': 'termfield', 'size': 10}, f.encodable)
Exemple #4
0
 def test_string_query(self):
     exp_json = {
         'query': {
             'query': 'q*ry',
             'boost': 2.0,
         },
         'explain': True,
         'size': 10,
         'indexName': 'ix'
     }
     q = cbft.QueryStringQuery('q*ry', boost=2.0)
     p = cbft.Params(limit=10, explain=True)
     self.assertEqual(exp_json, cbft.make_search_body('ix', q, p))
Exemple #5
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))
Exemple #6
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))
Exemple #7
0
    def test_match_query(self):
        exp_json = {
            'query': {
                'match': 'salty beers',
                'analyzer': 'analyzer',
                'boost': 1.5,
                'field': 'field',
                'fuzziness': 1234,
                'prefix_length': 4
            },
            'size': 10,
            'indexName': 'ix'
        }

        q = cbft.MatchQuery('salty beers',
                            boost=1.5,
                            analyzer='analyzer',
                            field='field',
                            fuzziness=1234,
                            prefix_length=4)
        p = cbft.Params(limit=10)
        self.assertEqual(exp_json, cbft.make_search_body('ix', q, p))
Exemple #8
0
    def test_params(self):
        self.assertEqual({}, cbft.Params().as_encodable('ix'))
        self.assertEqual({'size': 10},
                         cbft.Params(limit=10).as_encodable('ix'))
        self.assertEqual({'from': 100},
                         cbft.Params(skip=100).as_encodable('ix'))

        self.assertEqual({'explain': True},
                         cbft.Params(explain=True).as_encodable('ix'))

        self.assertEqual(
            {'highlight': {
                'style': 'html'
            }},
            cbft.Params(highlight_style='html').as_encodable('ix'))

        self.assertEqual(
            {'highlight': {
                'style': 'ansi',
                'fields': ['foo', 'bar', 'baz']
            }},
            cbft.Params(highlight_style='ansi',
                        highlight_fields=['foo', 'bar',
                                          'baz']).as_encodable('ix'))

        self.assertEqual(
            {'fields': ['foo', 'bar', 'baz']},
            cbft.Params(fields=['foo', 'bar', 'baz']).as_encodable('ix'))

        self.assertEqual(
            {'sort': ['f1', 'f2', '-_score']},
            cbft.Params(sort=['f1', 'f2', '-_score']).as_encodable('ix'))

        self.assertEqual({'sort': ['f1', 'f2', '-_score']},
                         cbft.Params(sort=cbft.SortString(
                             'f1', 'f2', '-_score')).as_encodable('ix'))

        p = cbft.Params(
            facets={
                'term': cbft.TermFacet('somefield', limit=10),
                'dr': cbft.DateFacet('datefield').add_range(
                    'name', 'start', 'end'),
                'nr': cbft.NumericFacet('numfield').add_range(
                    'name2', 0.0, 99.99)
            })
        exp = {
            'facets': {
                'term': {
                    'field': 'somefield',
                    'size': 10
                },
                'dr': {
                    'field':
                    'datefield',
                    'date_ranges': [{
                        'name': 'name',
                        'start': 'start',
                        'end': 'end'
                    }]
                },
                'nr': {
                    'field': 'numfield',
                    'numeric_ranges': [{
                        'name': 'name2',
                        'min': 0.0,
                        'max': 99.99
                    }]
                },
            }
        }
        self.assertEqual(exp, p.as_encodable('ix'))