def test_cluster_search(self): options = search.SearchOptions(fields=["*"], limit=10, sort=["-_score"], scan_consistency=SearchScanConsistency.NOT_BOUNDED.value) initial = datetime.datetime.now() x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10)) # type: SearchResult SearchResultTest._check_search_result(self, initial, 6, x)
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 = search.SearchOptions(consistent_with=ms) got = params._gen_search_params('ix', search.MatchNoneQuery()).body exp = { 'indexName': ixname, 'query': { 'match_none': None }, 'ctl': { 'consistency': { 'level': 'at_plus', 'vectors': { ixname: { '{0}/{1}'.format(vb, uuid): seq } } } } } self.assertEqual(exp, got)
def test_cluster_search(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") most_common_term_max = 10 initial = datetime.datetime.now() x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)( "beer-search", search.TermQuery("category"), search.SearchOptions( facets={ 'fred': search.TermFacet('category', most_common_term_max) })) # type: SearchResult r = x.rows() print(r) first_entry = x.rows()[0] self.assertEqual("brasserie_de_brunehaut-mont_st_aubert", first_entry.id) SearchResultTest._check_search_result(self, initial, 6, x) fred_facet = x.facets()['fred'] self.assertIsInstance(fred_facet, search.SearchFacetResult) self.assertRaises(couchbase.exceptions.SearchException, self.cluster.search_query, "beer-search", search.TermQuery("category"), facets={'fred': None})
def test_cluster_search_fields(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") test_fields = ['category','name'] initial = datetime.datetime.now() #verify fields works w/in kwargs x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), fields=test_fields) # type: SearchResult first_entry = x.rows()[0] self.assertNotEqual(first_entry.fields, {}) res = list(map(lambda f: f in test_fields,first_entry.fields.keys())) self.assertTrue(all(res)) SearchResultTest._check_search_result(self, initial, 6, x) #verify fields works w/in SearchOptions x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(fields=test_fields)) # type: SearchResult first_entry = x.rows()[0] self.assertNotEqual(first_entry.fields, {}) res = list(map(lambda f: f in test_fields,first_entry.fields.keys())) self.assertTrue(all(res))
def test_locations(self): initial = datetime.datetime.now() x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)( "beer-search", search.MatchQuery("Budweiser", prefix_length=0, fuzziness=0), search.SearchOptions(fields=["*"], limit=10, sort=["-_score"])) # type: SearchResult SearchResultTest._check_search_result(self, initial, 6, x)
def test_cluster_search_date_facets(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") facet_name = 'updated' facet = search.DateFacet('updated') facet.add_range('early', end='2010-12-01T00:00:00Z') facet.add_range('mid', start='2010-12-01T00:00:00Z', end='2011-01-01T00:00:00Z') facet.add_range('late', start='2011-01-01T00:00:00Z') x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(facets={ facet_name:facet })) # type: SearchResult x.rows() result_facet = x.facets()[facet_name] self.assertIsInstance(result_facet, search.SearchFacetResult) self.assertEqual(facet_name, result_facet.name) self.assertEqual(facet.field, result_facet.field) # if a limit is not provided, only the top-level facet results are provided self.assertEqual(0, len(result_facet.date_ranges)) # try again but verify the limit is applied (i.e. limit < len(date_ranges)) facet.limit = 2 x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(facets={ facet_name:facet })) # type: SearchResult x.rows() result_facet = x.facets()[facet_name] self.assertIsInstance(result_facet, search.SearchFacetResult) self.assertEqual(facet_name, result_facet.name) self.assertEqual(facet.field, result_facet.field) self.assertEqual(facet.limit, len(result_facet.date_ranges)) self.assertRaises(couchbase.exceptions.SearchException, self.cluster.search_query, "beer-search-index", search.TermQuery("north"), facets={'abv': search.DateFacet('abv', 10)})
def test_cluster_search_numeric_facets(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") facet_name = 'abv' facet = search.NumericFacet('abv') facet.add_range('low', max=7) facet.add_range('med', min=7, max=10) facet.add_range('high', min=10) x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(facets={ facet_name:facet })) # type: SearchResult x.rows() result_facet = x.facets()[facet_name] self.assertIsInstance(result_facet, search.SearchFacetResult) self.assertEqual(facet_name, result_facet.name) self.assertEqual(facet.field, result_facet.field) # if a limit is not provided, only the top-level facet results are provided self.assertEqual(0, len(result_facet.numeric_ranges)) # try again but verify the limit is applied (i.e. limit < len(numeric_ranges)) facet.limit = 2 x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(facets={ facet_name:facet })) # type: SearchResult x.rows() result_facet = x.facets()[facet_name] self.assertIsInstance(result_facet, search.SearchFacetResult) self.assertEqual(facet_name, result_facet.name) self.assertEqual(facet.field, result_facet.field) self.assertGreaterEqual(facet.limit, len(result_facet.numeric_ranges)) self.assertEqual(facet.limit, len(result_facet.numeric_ranges)) self.assertRaises(couchbase.exceptions.SearchException, self.cluster.search_query, "beer-search-index", search.TermQuery("north"), facets={'abv': search.NumericFacet('abv', 10)})
def test_cluster_search_disable_scoring(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") if float(self.cluster_version[0:3]) < 6.5: raise SkipTest("Disable scoring not available on server version < 6.5") #verify disable scoring works w/in SearchOptions x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10, disable_scoring=True) ) # type: SearchResult rows = x.rows() res = list(map(lambda r: r.score == 0, rows)) self.assertTrue(all(res)) # verify disable scoring works w/in kwargs x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10), disable_scoring=True) # type: SearchResult rows = x.rows() res = list(map(lambda r: r.score == 0, rows)) self.assertTrue(all(res)) x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10, disable_scoring=False) ) # type: SearchResult rows = x.rows() res = list(map(lambda r: r.score != 0, rows)) self.assertTrue(all(res)) x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10)) # type: SearchResult rows = x.rows() res = list(map(lambda r: r.score != 0, rows)) self.assertTrue(all(res))
def test_string_query(self): exp_json = { 'query': { 'query': 'q*ry', 'boost': 2.0, }, 'explain': True, 'size': 10, 'indexName': 'ix' } q = search.QueryStringQuery('q*ry', boost=2.0) p = search.SearchOptions(limit=10, explain=True) self.assertEqual(exp_json, p._gen_search_params('ix', q).body)
def test_cluster_search_highlight(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") initial = datetime.datetime.now() #verify locations/fragments works w/in SearchOptions x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(highlight_style=search.HighlightStyle.Html, limit=10)) # type: SearchResult rows = x.rows() self.assertGreaterEqual(10, len(rows)) locations = rows[0].locations fragments = rows[0].fragments self.assertIsInstance(fragments, dict) res = list(map(lambda l: isinstance(l, search.SearchRowLocation), locations.get_all())) self.assertTrue(all(res)) self.assertIsInstance(locations, search.SearchRowLocations) SearchResultTest._check_search_result(self, initial, 6, x) initial = datetime.datetime.now() #verify locations/fragments works w/in kwargs x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(limit=10), highlight_style='html') # type: SearchResult rows = x.rows() self.assertGreaterEqual(10, len(rows)) locations = rows[0].locations fragments = rows[0].fragments self.assertIsInstance(fragments, dict) res = list(map(lambda l: isinstance(l, search.SearchRowLocation), locations.get_all())) self.assertTrue(all(res)) self.assertIsInstance(locations, search.SearchRowLocations) SearchResultTest._check_search_result(self, initial, 6, x)
def test_cluster_search_scan_consistency(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") initial = datetime.datetime.now() #verify scan consistency works w/in SearchOptions x = self.try_n_times_decorator(self.cluster.search_query, 2, 1)("beer-search-index", search.TermQuery("north"), search.SearchOptions(scan_consistency=search.SearchScanConsistency.NOT_BOUNDED)) # type: SearchResult rows = x.rows() self.assertGreaterEqual(10, len(rows)) SearchResultTest._check_search_result(self, initial, 6, x) initial = datetime.datetime.now() #verify scan consistency works w/in SearchOptions x = self.try_n_times_decorator(self.cluster.search_query, 2, 1)("beer-search-index", search.TermQuery("north"), search.SearchOptions(scan_consistency=search.SearchScanConsistency.AT_PLUS)) # type: SearchResult rows = x.rows() self.assertGreaterEqual(10, len(rows)) SearchResultTest._check_search_result(self, initial, 6, x)
def test_match_phrase(self): exp_json = { 'query': { 'match_phrase': 'salty beers', 'analyzer': 'analyzer', 'boost': 1.5, 'field': 'field' }, 'size': 10, 'indexName': 'ix' } p = search.SearchOptions(limit=10) q = search.MatchPhraseQuery('salty beers', boost=1.5, analyzer='analyzer', field='field') self.assertEqual(exp_json, p._gen_search_params('ix', q).body)
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 = search.MatchQuery('salty beers', boost=1.5, analyzer='analyzer', field='field', fuzziness=1234, prefix_length=4) p = search.SearchOptions(limit=10) self.assertEqual(exp_json, p._gen_search_params('ix', q).body)
def test_fuzzy(self): q = search.TermQuery('someterm', field='field', boost=1.5, prefix_length=23, fuzziness=12) p = search.SearchOptions(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, p._gen_search_params('someIndex', q).body)
def test_cluster_search_term_facets(self # type: SearchTest ): if self.is_mock: raise SkipTest("F.T.S. not supported by mock") facet_name = 'beers' facet = search.TermFacet('category', 10) x = self.try_n_times_decorator(self.cluster.search_query, 10, 10)("beer-search-index", search.TermQuery("north"), search.SearchOptions(facets={ facet_name:facet })) # type: SearchResult x.rows() result_facet = x.facets()[facet_name] self.assertIsInstance(result_facet, search.SearchFacetResult) self.assertEqual(facet_name, result_facet.name) self.assertEqual(facet.field, result_facet.field) self.assertGreaterEqual(facet.limit, len(result_facet.terms)) self.assertRaises(couchbase.exceptions.SearchException, self.cluster.search_query, "beer-search-index", search.TermQuery("north"), facets={'beers': None})