def test_facets(self): s = SearchOptions() f = search.NumericFacet('numfield') p = s._gen_params() 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 = search.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 = search.TermFacet('termfield') self.assertEqual({'field': 'termfield'}, f.encodable) f.limit = 10 self.assertEqual({'field': 'termfield', 'size': 10}, f.encodable)
def search_products(self, product, fuzziness): """ Lab 2: Search operation (FTS) 1. FTS: term query w/ fuzziness use "basic-search" as index name for searchQuery 2. K/V getMulti() using FTS results """ try: result = self.__cluster.search_query( 'basic-search', TermQuery(term=product, fuzziness=fuzziness) if fuzziness else TermQuery(term=product), SearchOptions(limit=100)) doc_ids = list(map(lambda r: r.id, result)) # TODO: 2020.11.16 - get_multi() doesn't work, LCB_ERR_UNSUPPORTED_OPERATION (214) thrown # product_docs = self.__cluster.get_multi(doc_ids) # TODO: This is slow, just looping over the ids and fetch on each on # need to work in an async approach products = list( map(lambda id: self.get_object_by_key(id)['result'], doc_ids)) return {'products': products, 'error': None} except Exception as ex: output_message(ex, 'repository.py:search_products() - error:') return { 'products': None, 'error': { 'message': repr(ex), 'stackTrace': traceback.format_exc() } }
def get_stations_for_region_by_geosearch(self, region_name): """Using a geosearh return all the stations within the defined region Args: region_name (string): the name of the region. Returns: list: the list of stations within this region """ try: result = self.cluster.query( """SELECT geo.bottom_right.lat as br_lat, geo.bottom_right.lon as br_lon, geo.top_left.lat as tl_lat, geo.top_left.lon as tl_lon FROM mdata WHERE type='MD' and docType='region' and subset='COMMON' and version='V01' and name=$region""", region=region_name, read_only=True, ) _boundingbox = list(result)[0] _domain_stations = [] _result1 = self.cluster.search_query( "station_geo", GeoBoundingBoxQuery( top_left=(_boundingbox["tl_lon"], _boundingbox["tl_lat"]), bottom_right=(_boundingbox["br_lon"], _boundingbox["br_lat"]), field="geo", ), SearchOptions(fields=["name"], limit=10000), ) for elem in list(_result1): _domain_stations.append(elem.fields["name"]) _domain_stations.sort() return _domain_stations except Exception as _e: # pylint: disable=broad-except logging.error( "%s: Exception with builder: error: %s", self.__class__.__name__, str(_e), ) return []
def test_params(self): self.assertEqual({}, SearchOptions().as_encodable('ix')) self.assertEqual({'size': 10}, SearchOptions(limit=10).as_encodable('ix')) self.assertEqual({'from': 100}, SearchOptions(skip=100).as_encodable('ix')) self.assertEqual({'explain': True}, SearchOptions(explain=True).as_encodable('ix')) self.assertEqual( {'highlight': { 'style': 'html' }}, SearchOptions( highlight_style=search.HighlightStyle.Html).as_encodable('ix')) self.assertEqual( {'highlight': { 'style': 'ansi', 'fields': ['foo', 'bar', 'baz'] }}, SearchOptions(highlight_style=search.HighlightStyle.Ansi, highlight_fields=['foo', 'bar', 'baz']).as_encodable('ix')) self.assertEqual( {'fields': ['foo', 'bar', 'baz']}, SearchOptions(fields=['foo', 'bar', 'baz']).as_encodable('ix')) self.assertEqual( {'sort': ['f1', 'f2', '-_score']}, SearchOptions(sort=['f1', 'f2', '-_score']).as_encodable('ix')) self.assertEqual( {'sort': ['f1', 'f2', '-_score']}, SearchOptions(sort=['f1', 'f2', '-_score']).as_encodable('ix')) p = SearchOptions( facets={ 'term': search.TermFacet('somefield', limit=10), 'dr': search.DateFacet('datefield').add_range( 'name', 'start', 'end'), 'nr': search.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'))
def do_search(dest): search_params = SearchOptions.gen_search_params_cls( index, query, *options, **kwargs) return search_params.itercls(search_params.body, dest, **search_params.iterargs)
for row in result.rows(): print("Found row: {}".format(row)) print("Reported total rows: {}".format( result.metadata().metrics.total_rows)) except CouchbaseException as ex: import traceback traceback.print_exc() # end::search_basic_example[] # 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[]