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_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(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_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_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_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})
def test_search(self # type: Base ): cluster = self.gen_cluster(**self.make_connargs()) yield from (cluster.on_connect() or asyncio.sleep(0.01)) try: it = cluster.search_query( "beer-search", SEARCH.TermQuery("category"), facets={'fred': SEARCH.TermFacet('category', 10)}) yield from it.future data = list(it) self.assertIsInstance(it, AsyncSearchResult) self.assertEqual(10, len(data)) except SearchException as e: if isinstance(e.inner_cause, NotSupportedException) and self.is_mock: raise SkipTest("Not supported")
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 testBatchedSearch(self # type: Base ): if self.is_mock: raise SkipTest("No analytics on mock") cb = self.make_connection() d = cb.search_query("beer-search", SEARCH.TermQuery("category"), facets={'fred': SEARCH.TermFacet('category', 10)}) def verify(o): logging.error("Called back") self.assertIsInstance(o, BatchedSearchResult) rows = [r for r in o] self.assertEqual(10, len(rows)) logging.error("End of callback") result = d.addCallback(verify) logging.error("ready to return") return result
# tag::search_result[] result = cluster.search_query("travel-sample-index", search.PrefixQuery("swim"), SearchOptions(fields=["description"])) for row in result.rows(): print("Score: {}".format(row.score)) print("Document Id: {}".format(row.id)) # print fields included in query: print(row.fields) # end::search_result[] # tag::limit_and_skip[] result = cluster.search_query("travel-sample-index", search.TermQuery("downtown"), SearchOptions(limit=4, skip=3)) # end::limit_and_skip[] # tag::scan_consistency[] result = cluster.search_query( "travel-sample-index", search.TermQuery("downtown"), SearchOptions(scan_consistency=SearchScanConsistency.NOT_BOUNDED)) # end::scan_consistency[] # tag::ryow[] new_airline = { "callsign": None, "country": "United States", "iata": "TX", "icao": "TX99",