Skip to main content

NUDB

A Python module for NUDB.
NUDB is a fast database and search engine.

Menu:

Install

$ pip install nudb

Usage

資料格式

  • GAIS record

    • @ 開頭, : 結尾作為欄位名稱

    • : 之後為欄位內容

    • For example:

      // "@title:" 為欄位名稱
      @title:Mayday五月天 [ 頑固Tough ] Official Music Video
      
  • JSON

Connect to NUDB

from nudb import Nudb

nudb = Nudb()
nudb.connect('host', 'port', 'db')

參數說明

  • host: DB host
  • port: DB port
  • db: 指定 DB 名稱

Get DB info

result = nudb.get_DB_info(timeout=10)

參數說明

  • timeout: 設定 timeout,單位為 s,預設是 10s.

Search

options = {
    'db': 'test',
    'matchmode': 'BestMatch',
    'groupby': '@title:',
    'getrec': 'y',
    'orderby': 'score',
    'order': 'decreasing',
    'minrid': 100,
    'maxrid': 10000,
    'ridrange': 10000,
    'minscore': 100,
    'maxscore': 5000,
    'q': '旅遊',
    'filter': '@viewcount:>1000',
    'Sensitivity': 'sensitive',
    'p': 1,
    'ps': 10,
    'select': '@title:,@body:,@viewcount:',
    'out': 'json'
}
result = nudb.search(options, timeout=10);

參數說明

  • timeout: 設定 timeout,單位為 s,預設是 10 s.
  • options: query options
    • db: 指定DB

    • matchmode

      • AndMatch (預設)
      • OrMatch
      • BestMatch
    • groupby: 指定欄位群組, 預設只有輸出key, count, 欄位格式為GAIS record

    • getrec=y: 搭配groupby使用, 輸出全部資料

    • orderby

      • rid: 依照rid排序

      • score: 必須有參數q 才有score

      • groupsize: 搭配groupby

      • {FieldName}: 依照欄位(FieldName)排序

        • 在建立DB時, 數值欄位須設定 -numfieldindex
        • 在建立DB時, 時間欄位須設定 -timeindex
        {
          'orderby': '@viewcount:'
        }
        
      • [min|max|ave|sum]{FieldName}: 找出欄位(FieldName)的最小/最大/平均/總和值

        {
          'orderby': 'sum@viewcount:' 
        }
        
      • order: 搭配orderby使用, 預設為decreasing

        • decreasing: 遞減
        • increasing: 遞增
      • minrid: 設定rid最小值

      • maxrid: 設定rid最大值

      • ridrange: 設定搜尋的rid範圍, rid由大至小, 僅搜尋此範圍內的資料

      • minscore: score最小值

      • maxscore: score最大值

      • q: 搜尋關鍵字

        • 可指定欄位搜尋, 欄位格式為GAIS record:
        {
          'q': '@title:日本旅遊'
        }
        
      • 可指定欄位值須完全符合:

        {
          'q': '@id:=abcd1234'
        }
        
      • 可設定所有條件須符合(AndMatch):

        {
          'q': '+@id:1234,+@name:test'
        }
        
        • 可搜尋多個欄位, 以","區隔:
        {
          'q': "@title:日本旅遊,@body:東京"
        }
        
    • time: 可設定搜尋時間範圍

      • 在建立DB時, 時間欄位須設定 -timeindex

      • 限定時間區間

        {
          'time': '=20180101-20180301'
        }
        
      • 特定時間以後

        {
          'time': '=>20180220122000'  # YYYYMMDDHHmmss
        }
        
      • 特定時間以前

        {
          'time': '=<20180220122000'  # YYYYMMDDHHmmss
        }
        
      • 限定某天

        {
          'time': '=20180220'
        }
        
    • filter: 數值條件檢索, 沒有做數值欄位索引(-numfieldindex)也可查詢

      {
        'filter': '@price:<200'
      }
      
      {
        'filter': '@price:200-400'  # 數值區間
      }
      
    • maxcandidnum

    • Sensitivity

      • sensitive: 預設, 區分大小寫
      • insensitive: 不分大小寫
    • keytermfield

    • keytermstat

    • p: page, 指定輸出page, 預設為1

    • ps: page size, 每個page大小, 預設為10

    • select: 指定輸出欄位, 欄位格式為GAIS record, 多個欄位之間以","區隔

    • L: 指定回傳起始比數及總筆數

      {
        'L': 30       # 回傳30筆
      }
      
      {
        'L': '11,60'  # 從第11筆開始, 輸出60筆
      }
      
    • out: 輸出格式 (json or text)

Get record by rid or key

result = nudb.rget(data_id, search_field='rid', timeout=10)

參數說明

  • data_id: Record ID or primary key.
  • search_field: 搜尋的欄位,rid 或 key, 預設是 rid.
  • timeout: 設定 timeout,單位為 s,預設是 10s.

Put record

# format: json or text
result = nudb.rput(data, data_type, rec_beg=None, timeout=10)

參數說明

  • data: 資料
  • data_type: 資料格式(json or text)
  • rec_beg: record begin pattern, 若資料格式為text則必須有此參數
  • timeout: 設定 timeout,單位為 s,預設是 10s.

Put record from file

result = nudb.fput(file_path, data_type, rec_beg=None, timeout=60)

參數說明

  • file_path: 要上傳的檔案
  • data_type: 資料格式(json or text)
  • rec_beg: record begin pattern, 若資料格式為text則必須有此參數
  • timeout: 設定 timeout,單位為 s,預設是 60s.

Delete record by rid or key

result = nudb.rdel(data_id, search_field='rid', timeout=10)

參數說明

  • data_id: Record ID 或 primary key, 一次刪除多筆可使用,區隔多個 id
  • search_field: 搜尋的欄位,rid 或 key, 預設是 rid.
  • timeout: 設定 timeout,單位為 s,預設是 10s.

Update record

result = nudb.rupdate(data_id, data, data_type, search_field='rid', update_method='replaceRecord', timeout=10)

參數說明

  • data_id: 要更新的資料rid or primary key
  • data: 更新的資料內容
  • data_type: 資料格式(json or text)
  • search_field: 搜尋的欄位,rid 或 key, 預設是 rid.
  • update_method: 更新方式
    • replaceRecord: 取代整筆資料 (Default)
    • replaceField: 取代指定欄位的資料
  • timeout: 設定 timeout,單位為 s,預設是 10s.

Change log

Release files for nudb 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nudb 1.1.4
File Size Uploaded
nudb-1.1.4.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nudb 1.1.4
File Interpreter ABI Platform
nudb-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / nudb-1.1.4.tar.gz

Download URL nudb-1.1.4.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
15b013f4c838ddd40a1f92e85ac768aa080292968d49e49ec0ee603a1c0126d7
BLAKE2b-256 checksum
How to use checksums
2ca2dede589d0e2a370db25ae431675329237ffb7d471d4dafcecedeb6fdadc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

Release files / nudb-1.1.4-py3-none-any.whl

Download URL nudb-1.1.4-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f87b321d2f34e5fde7c05a07c8afca8e2ad335f804a2adcea69957b18525822
BLAKE2b-256 checksum
How to use checksums
7008e321b72d951c2f99ec60db032a08eaaf4ffb16aabd1e40ac4cbafd097c63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page