OnceDB quick start

OnceDB by onceoa on 1582792640649


OnceDB is a full-text search engine based on Redis. It can manage data like a SQL database.

OnceDB does not change the data storage structure of Redis and is fully compatible with Redis. Redis database files can be directly operated in OnceDB and then returned to Redis for use.

Quick installation

Select and download the packages:

https://github.com/OnceDoc/OnceDB/releases/

On Windows platforms, run oncedb-server.exe to start the service.

Update data

OnceDB use the upsert or insert commands to add data. OnceDB implements auxiliary indexes through ordered list (zset), which greatly improves the search performance under complex conditional queries. OnceDB automatically creates these indexes through operators. The syntax of upsert command is as follows:

upsert schema field operator value ...

For example, you want to add this data:

var userInfo = {
    username: 'dota'    // primary key
  , password: '123456'  // field
  , title: 'SDEI'       // Create a grouped index
  , skills: 'java,go,c' // Create keywords index
}

The corresponding operators are as follows:

= Ordinary field value, no index
@ Primary key
? Grouping index
* Keyword grouping index, separated by ',' between keywords
\ Sort index, the score weight of the index is the value of the field

Execute the following command:

upsert user username @ dota password = 123456 title ? SDEI skills * java,go,c
> OK

You can choose a familiar third-party Redis client to connect to OnceDB, We are using the default redis-cli here.

This successfully adds the data to the user table. "user:dota" data and secondary indexes are created automatically.

The secondary index starts with *, use keys * command to see these keys.

oncedb_start_0.png

Query data

You can use the find command to query data. The syntax is as follows:

find schema from to field operator value ...

from and to are selected data ranges, if it is 0 -1, it will output all the found data.

Search by index

If it is an index field, you can search from the index by an operator, such as searching user data containing the c keyword, and printing the username and password fields.

find user 0 -1 username = * password = * skills * c
1) (integer) 1
2) "user:dota"
3) "dota"
4) "123456"
5) "java,go,c"

The first line "(integer) 1" is the total number of eligible data.

Full-text search

You can also use string search. For example, to search for user data that contains c, use "~":

find user 0 -1 username = * password = * skills ~ c
1) (integer) -1
2) "user:dota"
3) "dota"
4) "123456"
5) "java,go,c"

The -1 returned on line 1 indicates that full-text search was used. It also supports “<,>, <=,> = ', etc. Full text search performance is poor. In the case of large amounts of data, an index search is recommended.

Node.JS client

OnceDB does not constrain the data model at the bottom, but uses the driver layer to dynamically define data tables, fields, types, indexes, and so on.

node.js client, installable via npm:

npm install oncedb

You can then create an instance and connect to the local oncedb-server:

var oncedb = require('oncedb')()

async/ await interface

API can be formatted using util.promisify, avoiding callbacks:

const util    = require('util')
const oncedb  = require('oncedb')()

const update  = util.promisify(oncedb.update).bind(oncedb)
const select  = util.promisify(oncedb.select).bind(oncedb)

Defining the schema

Before updating or querying data, you need to define schema first, specify fields, field types, and indexes. For example, the user table above can be defined according to the following pattern:

oncedb.schema('user', {
    username  : 'id'
  , password  : ''
  , title     : 'index'
  , skills    : 'keywords'
});

Update data via node.js client

You can use the following code to update and query the data:

(async () => {
  // update data
  await upsert('user', { username: 'dota', password: '123456', title: 'SDEI', skills: 'java,go,c' })
  // query data
  let rows = await select('user', { skills: 'c' })

  console.log('rows.count', rows.count)
  console.log(rows)
})();

Output result:

rows.count 1
[
  {
    _key: 'user:dota',
    skills: [ 'java', 'go', 'c' ],
    username: 'dota',
    password: '123456',
    title: 'SDEI'
  }
]

rows.count is not equal to -1, which means index search is enabled


Next acticle: Indexed data modification and query help documentation