Search operations in Python are performed using the Cursor methods
find()orsearch(). Thefind()method locates an object in the database and returns an object instance orNoneif not found. Thesearch()method positions the cursor at the first matching object or returnsFalseif not found. Then the Cursor navigation methodsfirst(),last(),next(), andprev()can be used to scroll through the result set.The following code snippet demonstrates a find and search operation:
conn = db.connect() conn.startTransaction(exdb.Transaction.MCO_READ_WRITE) # Perform simple index search: locate Record by id cursor = conn.cursor() # Find record to update rec = cursor.find("Record", "by_i4", 2) ... cursor.close() # release cursor conn.commit() # commit changes conn.startTransaction() cursor = conn.cursor() cursor.search("Security", "by_SIC", exdb.Operation.Equals, SIC) ... cursor.close() conn.rollback()SQL Queries
The Cursor can also be used to execute SQL queries with methods
execute()andexecute_many(). The result sets from SQL queries can then be processed using the methodsfetchone(),fetchmany(), orfetchall(). For example:conn = db.connect() cursor = conn.cursor() cursor.execute("SELECT * FROM Metatable WHERE TableName='%s'" % clsname) res = cursor.fetchall() ... sql = "INSERT INTO Quote(ikey, symbol, stamp, low, high, open, close, volume) VALUES (?,?,?,?,?,?,?,?)" params = [] for i in range(N_QUOTES // 2, N_QUOTES): params.append((i, 'AA%s' % i, datetime.datetime(2017, 8, 16, 9, 30+i), 1.0, 4.0, 2.0, 3.0, i*1000)) cursor.execute_many(sql, params) ... cursor.close() conn.rollback() cursor = conn.cursor() sql = "SELECT ikey, symbol, stamp, low, high, open, close, volume FROM Quote ORDER BY ikey" cursor.execute(sql) count = 0 while True: row = cursor.fetchone() if row is None: break print("Row read:", row) count += 1 ... cursor.close() conn.rollback()HNSW and Vamana Index Searches
Python commonly uses a
.mcoschema string or schema file to define HNSW and Vamana indexes:schema = ''' #define key_t unsigned<4> #define coord_t float #define DIM 3 declare database hnswdb; class Embeddings { key_t pk; coord_t coord[DIM]; tree<pk> pk_idx; hnsw<coord> hnsw_idx[16]; }; '''Runtime options are set before opening or building the database:
exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_HNSW_M, 16) exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_HNSW_DIST_FUNC, exdb.HnswDistFunc.DIST_L2) exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_HNSW_EF_CONSTRUCTION, 64) exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_HNSW_EF_SEARCH, 128) exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_VAMANA_ALPHA, 0)For Vamana indexes, specify a positive
VAMANA_ALPHAvalue and optionally choose a build method:exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_VAMANA_ALPHA, 120) exdb.runtime_setoption(exdb.RuntimeOption.MCO_RT_OPTION_VAMANA_BUILD_METHOD, exdb.VamanaBuildMethod.VAMANA_BUILD_INCREMENTAL)Search with a cursor using the
Neighbourhoodoperation:with conn.cursor() as cursor: found = cursor.search("Embeddings", "hnsw_idx", exdb.Operation.Neighbourhood, pattern)The cursor returns neighboring objects found by the ANN search. Python applications can also use SQL DDL and SQL
NEARqueries through the Python SQL cursor interface.