NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

Indexes and search

An index is an additional search structure, like the index of a book: it finds hits without reading all the data. NHyDB comes with four kinds.

Four kinds of indexes

  • VALUE – exact values and value ranges, for fields of any type.
  • TOKEN – whole words in text fields.
  • TRIGRAM – parts of words in text fields, optionally case-insensitive.
  • FULLTEXT – words and exact word sequences, ranked by relevance.

Build once, then current

You build a new index once. After that NHyDB keeps it current by itself: changes are taken into account immediately when searching, and after enough changes NHyDB renews the index on its own when committing. The results are always the same that a search over all the data would return.

Full-text search with relevance

A FULLTEXT index finds texts that contain all search words or an exact word sequence. The hits can be ranked by estimated relevance; for this NHyDB uses the widely used BM25 method.

An example

A value index on the state and a full-text index on the description:

import nhydb
from nhydb.query import Q, QueryEngine

with nhydb.create("cities.nhydb") as db:
    db.create_realm(1, "main")
    db.create_shape(1, 1, "city", [{"v": 1, "name": "name", "type": "utf8"},
                                   {"v": 2, "name": "state", "type": "utf8"},
                                   {"v": 3, "name": "info", "type": "utf8"}])
    db.create_classic_layout(1, 1, "cities", shape_id=1)
    db.put_dense_batch(1, 1, [["Berlin", "Berlin", "capital on the Spree"],
                              ["Hamburg", "Hamburg", "port city on the Elbe"],
                              ["Dresden", "Saxony", "baroque city on the Elbe with a church"]])
    db.create_index(1, 1, "by_state", "state")                 # VALUE index
    db.create_fulltext_index(2, 1, "fulltext", "info")         # FULLTEXT index
    db.build_indexes(commit=True)                              # build once

    print(list(db.find_u(1, {"by_state": "Saxony"})))
    hits = QueryEngine(db).search(Q.fulltext("info", "elbe"), realm=1, select=["name"])
    print([(h["name"], round(h["relevance"], 3)) for h in hits["rows"]])
    db.put(1, 4, ["Leipzig", "Saxony", "trade fair city"], commit=True)   # index stays current
    print(list(db.find_u(1, {"by_state": "Saxony"})))

Output:

[3]
[('Hamburg', 0.494), ('Dresden', 0.402)]
[3, 4]

find_u returns the rows u of the hits. Hamburg comes first because “Elbe” occurs there in a shorter text. Leipzig is found immediately after writing, without rebuilding the index.

Good to know

  • Records in the CLASSIC layout are indexed.
  • After an update of NHyDB, you rebuild existing indexes once.
  • Each renewal writes the index anew; the old version stays in the file until compaction.

Searches usually do not go to the index directly but through queries: the QueryEngine and NHySQL use suitable indexes by themselves.

# Indexes and search An **index** is an additional search structure, like the index of a book: it finds hits without reading all the data. NHyDB comes with four kinds. ## Four kinds of indexes - **VALUE** – exact values and value ranges, for fields of any type.- **TOKEN** – whole words in text fields.- **TRIGRAM** – parts of words in text fields, optionally case-insensitive.- **FULLTEXT** – words and exact word sequences, ranked by relevance. ## Build once, then current You build a new index once. After that NHyDB keeps it current by itself: changes are taken into account immediately when searching, and after enough changes NHyDB renews the index on its own when committing. The results are always the same that a search over all the data would return. ## Full-text search with relevance A FULLTEXT index finds texts that contain all search words or an exact word sequence. The hits can be ranked by estimated relevance; for this NHyDB uses the widely used BM25 method. ## An example A value index on the state and a full-text index on the description: ```pythonimport nhydbfrom nhydb.query import Q, QueryEngine with nhydb.create("cities.nhydb") as db: db.create_realm(1, "main") db.create_shape(1, 1, "city", [{"v": 1, "name": "name", "type": "utf8"}, {"v": 2, "name": "state", "type": "utf8"}, {"v": 3, "name": "info", "type": "utf8"}]) db.create_classic_layout(1, 1, "cities", shape_id=1) db.put_dense_batch(1, 1, [["Berlin", "Berlin", "capital on the Spree"], ["Hamburg", "Hamburg", "port city on the Elbe"], ["Dresden", "Saxony", "baroque city on the Elbe with a church"]]) db.create_index(1, 1, "by_state", "state") # VALUE index db.create_fulltext_index(2, 1, "fulltext", "info") # FULLTEXT index db.build_indexes(commit=True) # build once print(list(db.find_u(1, {"by_state": "Saxony"}))) hits = QueryEngine(db).search(Q.fulltext("info", "elbe"), realm=1, select=["name"]) print([(h["name"], round(h["relevance"], 3)) for h in hits["rows"]]) db.put(1, 4, ["Leipzig", "Saxony", "trade fair city"], commit=True) # index stays current print(list(db.find_u(1, {"by_state": "Saxony"})))``` Output: ```text[3][('Hamburg', 0.494), ('Dresden', 0.402)][3, 4]``` `find_u` returns the rows u of the hits. Hamburg comes first because “Elbe” occurs there in a shorter text. Leipzig is found immediately after writing, without rebuilding the index. ## Good to know - Records in the [CLASSIC layout](/en/layouts/) are indexed.- After an update of NHyDB, you rebuild existing indexes once.- Each renewal writes the index anew; the old version stays in the file until compaction. Searches usually do not go to the index directly but through [queries](/en/queries/): the QueryEngine and NHySQL use suitable indexes by themselves.