Example #1
0
    def test_cluster_search(self  # type: ClusterTestCase
                            ):
        if self.is_mock:
            raise SkipTest("FTS not supported by mock")
        most_common_term_max = 10
        initial=time.time()
        x = self.cluster.search_query("beer-search", FT.TermQuery("category"),
                                      facets={'fred': FT.TermFacet('category', most_common_term_max)})
        first_entry = x.hits()[0]
        self.assertEquals("brasserie_de_brunehaut-mont_st_aubert", first_entry.get('id'))
        min_hits = 6
        metadata = x.metadata()
        duration=time.time()-initial
        self.assertIsInstance(metadata, IMetaData)
        self.assertIsInstance(metadata.error_count(), int)
        self.assertIsInstance(metadata.max_score(), float)
        self.assertIsInstance(metadata.success_count(), int)
        took=metadata.took()
        self.assertIsInstance(took, Seconds)
        self.assertAlmostEqual(took.value, duration, delta=0.1)
        self.assertGreater(took.value, 0)
        self.assertIsInstance(metadata.total_hits(), int)
        self.assertGreaterEqual(metadata.success_count(), min_hits)
        self.assertGreaterEqual(metadata.total_hits(), min_hits)
        self.assertGreaterEqual(len(x.hits()), min_hits)
        fred_facet = x.facets()['fred']
        self.assertIsInstance(fred_facet, ISearchResult.Facet)
        self.assertEqual(len(fred_facet['terms']), most_common_term_max)

        self.assertRaises(couchbase.exceptions.SearchException, self.cluster.search_query, "beer-search",
                          FT.TermQuery("category"),
                          facets={'fred': None})
Example #2
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)
Example #3
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'))