KiaraDB: Tree-Based Indexing for Multi-Field In-Memory Search in Java and TypeScript

KiaraDB: Tree-Based Indexing for Multi-Field In-Memory Search in Java and TypeScript

In a database, you filter with a WHERE clause. With data already in memory, in a browser or a Node.js process, you usually write Array.filter callbacks, or bend a fuzzy-search library into a filter engine. Both work until users choose the filters. Five filters can be combined 31 ways; ten filters, 1,023. Each combination either scans every record or needs an index designed for it. KiaraDB is an open-source in-memory search library, not a persistent database, built for that problem. You describe your fields, load your records, and call prepare() once to build the indexes. After that, any combination of fields is a single query, with typo correction and type-ahead for text. KiaraDB is the project. It has two libraries with the same search behavior: Kiara for Java (Java 8+, Apache-2.0), for server-side services. kiara-ts for TypeScript (MIT, zero runtime dependencies), for browsers, Web Workers, and Node.js. A contract check runs the same queries through both libraries and compares the results. How it works Kiara stores every record as a path through a tree, one level per field, so records that share values share nodes. A single depth-first walk numbers the nodes, which gives each node a numeric range that covers all of its descendants. Each field gets its own index from values to tree nodes: sorted keys for numbers, so price product.id)); // ["p1", "p4"] const typed = new KeywordSearch(store).bestMatch("wireles chargr", { fields: ["title"], conditions: [underForty], }); console.log(typed.results.map((product) => product.id)); // ["p1"] console.log(typed.correctedKeywords); // { wireles: "wireless", chargr: "charger" } The first query combines a brand, a price limit, and a tag. The second is a search box with two typos. bestMatch returns the records that match the most query words, here both words after correction, and applies the price condition strictly: the $49.99 stand matches the words but not the budget. correctedKeywords lets the UI show "Showing results for wireless charger." A few details that aren't obvious from the code: Lists are stored as strings, such as "wireless,fast-charge", not arrays. Use NormalizationProfile.NATIVE for decimals. The default matches the Java library and truncates fractional search values, which would surprise you with prices. In TypeScript, defineSchema() checks field names against your record type. Strengths Any combination of fields in one query. A filter panel needs one function that adds a condition for each active filter, not code or an index for each combination. Fast repeated queries. On 100,000 synthetic products, a three-field query took 0.156 ms, compared with 19.07 ms for a generic Array.filter applying the same rules, and 40.88 ms for Fuse.js. Across 86 benchmark queries, KiaraDB beat Array.filter in 84. Details: vs Array.filter, vs Fuse.js, and the benchmark summary below. Operators for real data, not hand-written callbacks. Numeric comparisons and inclusive BETWEEN; "all of", "any of", and "none of" on lists; explicit null matching; and fields that hold a range, such as a rate valid from December 1 to December 20, so you can ask which records' ranges contain a given date. Typo-tolerant text alongside strict filters. Keyword search corrects one wrong, missing, or extra letter per word and reports every correction. It also matches English word forms (decorating finds Decoration), suggests completions as users type, and applies hard filters at the same time. Shorthand queries that refuse to guess. An optional interpreter turns input such as mlcc 10uF voltage>=25V 0603 into conditions, converts units, and throws an error rather than silently ignoring a fragment it can't resolve.Zero dependencies and easy to embed. kiara-ts adds no runtime dependencies to your project. It includes typed schemas or standard decorators, a Web Worker client and host, and atomic replacement of a store when data is refreshed. Weaknesses Building indexes is slow and memory-hungry. In the TypeScript benchmark, preparing 100,000 products took about 4 seconds and retained 477 MB of heap; Fuse.js took about 0.23 seconds and 110 MB. In a browser, run prepare() in a Web Worker and size the data to the device; the live demo uses 10,000 products. The Java benchmark took about 3 minutes for 100,000 catalog records, against 4.8 seconds for Lucene. It used a different data generator and one wide record type shared by every dataset, so it isn't directly comparable with the TypeScript figure, but both point the same way. No OR across fields. Conditions on different fields are always combined with AND. OR works within one field ("brand is A or B"), but "brand is A, or price under 10" means running two queries and merging the results yourself. Data is a snapshot. You can't update a single record. Changes mean rebuilding the store, or preparing a replacement and swapping it in. Records must be flat. Nested objects aren't supported, and lists and ranges are stored as strings, so most data needs mapping before loading. Typo correction is narrow. One edit per word, only on words longer than three letters. Swapped letters aren't corrected, and typos near the start of a word are often missed. Fuse.js is far more tolerant. It filters; it doesn't rank. There are no relevance scores, weighted fields, or match highlighting. Gains depend on the query. Broad queries gain less: one matching 17,605 of 100,000 products was only 1.8x faster than Array.filter. Array.filter won two benchmark queries outright, both on fields that hold ranges. Benchmarks at a glance Typical speedup is how many times faster KiaraDB's median query time was, averaged (as a geometric mean) over five datasets: 3,788 real retail products, plus synthetic catalogs and schedules of 10,000 and 100,000 records. Below 1x means the other engine was faster. Compared with Typical speedup (higher = KiaraDB faster) Range across datasets Notes Fuse.js 65x 28x to 116x Could run only the 2 to 8 exact queries per dataset it can express. Built indexes 13x to 26x faster with 3x to 8x less memory. Apache Lucene 7.4x 3.5x to 16x Built 100,000 catalog records in 4.8 s versus about 3 minutes, with 20 MB versus 327 MB of extra heap. Also ranks full-text results. SQLite, generic indexes 2.6x 0.94x to 11.6x Won 24 of 87 queries, and was faster overall on 100,000 scheduled events. SQLite, tuned for these queries 1.0x 0.30x to 5.3x Faster on 3 of 5 datasets; won 35 of 89 queries. VoltDB, generic indexes 3.0x 1.1x to 28.6x Won 40 of 89 queries. VoltDB, tuned for these queries 1.6x 0.80x to 10.9x Faster on 3 of 5 datasets; won 43 of 89 queries. What this shows: KiaraDB is fastest when nobody tuned for the query. A database given compound indexes designed for known queries catches up or wins. KiaraDB's advantage is not needing that work when users choose the filter combinations. How it was measured: Generic indexes means one index per column, with no indexes designed for specific queries. Tuned adds compound indexes and SQL chosen for these exact queries. Only equivalent queries count. Every engine's results were checked against an independent reference first. Queries an engine couldn't express exactly were excluded, not counted as losses. Two queries where SQLite didn't finish within 60 seconds were also excluded. Setup: kiara-ts 1.2.1 against Fuse.js 7.5.0 in Node.js, with default Fuse.js settings; a tuned Fuse.js configuration gave similar times. Java Kiara 2.0.16 against Lucene 10.5.2, SQLite 3.53.4 (in memory), and VoltDB Community Edition 10.1.1 on the JVM. VoltDB was timed server-side, without network time. Query times exclude building indexes. The Array.filter baseline was a generic filter with the same rules, not a hand-optimized loop. The rule of thumb: KiaraDB pays off when the data changes rarely and the questions change constantly. Your situation Best fit Users combine filters you can't predict in advance KiaraDB A search box that must tolerate typos and still respect hard filters KiaraDB Lists, date ranges, and wildcard rules in the same query KiaraDB The same search behavior in a browser and on a Java server KiaraDB (kiara-ts and Kiara) A few hundred rows, or occasional queries Array.filter or a loop Fuzzy text, relevance ranking, weights, or highlighting Fuse.js or a similar library A fixed set of known queries on a server A database with indexes tuned for them Frequent writes, persistence, or joins SQLite, IndexedDB, or another database Full-text relevance search over large document collections Lucene or a search server built on it The size limit is memory, not a row count: in a browser, the device sets the budget; on a server, you choose it. For data larger than one machine, the Java documentation describes partitioning a dataset across servers. The build pays for itself over many queries. For the selective three-field query above, which saved about 19 ms per run, the 4-second build broke even after about 210 queries. Broad queries save less, so they take longer to pay off. An interactive filter UI can get there in one session; a nightly report won't. Try it See it first. The browser demo searches 10,000 products with typos and filters, entirely in your browser. Write your own queries. The Kiara TS Playground is an in-browser editor for writing and running queries against real data: 5,043 movies or 7,698 airports. It has field-aware autocomplete, 43 guided examples with expected results, and ten application examples. You can also import your own CSV; it stays in your browser and is never uploaded. Run it in your project. npm install @kapoorlabs/kiara-ts, then paste the example above into a .mjs file and run it with Node.js. For Java, start with the Kiara README. Pick your use case. Each of these short articles solves one problem with runnable code and its limits: Filter panels: one function for every combination of filters Type-ahead with typo correction: airport search that survives "londn gatwik" Required, optional, and excluded skills: all, any, and none on lists Availability windows: hotel rates valid for every night of a stay Most-specific config rules: wildcards, with conflicts reported Unit-aware parts search: engineers type 10uF 25V 0603 Check the numbers. Benchmark code and raw results for TypeScript and Java. Each benchmark ran on a single machine with mostly synthetic data; your results will differ. If KiaraDB solves a problem for you, a GitHub star helps other developers find it. If it was slower or harder to use than you expected, please open an issue; reports from real datasets are the most useful feedback.

Original Source

Read the full article at Hackernoon →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.