Repository navigation
Description
Parent: #4516
Motivation
Lance JSON inverted indexes should preserve array/object structure so path queries over arrays of objects can distinguish exact positions, wildcards, and nested-object constraints.
Tantivy documents a known JSON-index flaw:
{
"cart_id": 3234234 ,
"cart": [
{"product_type": "sneakers", "attributes": {"color": "white"} },
{"product_type": "t-shirt", "attributes": {"color": "red"}},
]
}
because an array document is treated as a bag of terms, a query like cart.product_type:sneakers AND cart.attributes.color:red can match even when sneakers and red live in different objects in the same array. Lance's current single-token-stream JSON index has the same class of problem.
Proposal
Introduce FlattenedSubDocs mode for new JSON inverted indexes. A JSON row can flatten into multiple internal sub-documents, and matches are mapped back to the original Lance row id. This lets related JSON path constraints match inside the same flattened array element before row-id remapping.
Expected behavior
row0: cart[0]=sneakers/white; cart[1]=t-shirt/red
row1: cart[0]=sneakers/red
operator: MatchQuery AND
Before (SingleDocument):
"cart.product_type,str,sneakers;cart.attributes.color,str,red"
-> row0, row1 (row0 is a false positive)
After (FlattenedSubDocs):
"cart[*].product_type,str,sneakers;cart[*].attributes.color,str,red"
-> row1
After, as two separate BooleanQuery.must leaves -> row0, row1
Existing indices keep SingleDocument; new indices use FlattenedSubDocs. Exact versus wildcard path results are shown in Example 1.
Error handling
| Setting | Behavior |
|---|---|
No max_sub_docs_per_row |
Unlimited sub-documents. |
Limit exceeded, fail (default) |
Abort before indexing any part of the row; error suggests a higher limit or independent sibling arrays. |
Limit exceeded, skip_row |
Warn, omit the entire row, continue. |
The same tokenizer policy applies to immutable writes and MemWAL.
What Changes
INGEST
Python / Java options [2] ---> persisted Rust settings [2] ---+
JSON row ------------------------------------------------------+--> JSON tokenizer [1]
sub-docs + $idx
/ \
immutable writer [3] MemWAL [3]
\ / flush
DocSet
QUERY
query triplets ---> path / $idx tokens [1]
| raw rows
+---> flat scan [4] <----+
|
+---> indexed search + planner [4] <--- DocSet
|
source row + max score [4]
|
Boolean composition by row [4]
Bracketed paths, sibling-array expansion, and the optional row cap are configured in [2]. The PR links for [1]–[4] are below.
Example 1 - Used to explain the idea
Raw Documents
doc-0
{"foo":[{"bar":["x","y"]}]}
doc-1
{"foo":[{"bar":["y"]},{"bar":"z"}]}
Flattened Documents
| Flatten ID | Flattened Path | Value |
|---|---|---|
| 0 | foo[0].bar[0] |
"x" |
| 1 | foo[0].bar[1] |
"y" |
| 2 | foo[0].bar[0] |
"y" |
| 3 | foo[1].bar |
"z" |
Token Dictionary
Each token is represented as: (path, type, value)
| Path | Type | Value | Flatten Posting |
|---|---|---|---|
foo..bar. |
str | x |
0 |
foo..bar. |
str | y |
1, 2 |
foo..bar |
str | z |
3 |
foo$idx |
num | 0 |
0, 1, 2 |
foo$idx |
num | 1 |
3 |
foo..bar$idx |
num | 0 |
0, 2 |
foo..bar$idx |
num | 1 |
1 |
Notes
- Prior array index
[<idx>]is compressed to.when walking through the path from left to right.foo$idxrepresents the index of the element in thefooarray.foo..bar$idxrepresents the index inside thebararray.
Search Query A
Query:
foo[0].bar[0] = "y"
Lookup tokens:
| Token | Flatten Posting |
|---|---|
foo..bar., str, "y" |
1, 2 |
foo..bar$idx, num, 0 |
0, 2 |
foo$idx, num, 0 |
0, 1, 2 |
Flatten Posting Intersection:
(1, 2) intersection (0, 2) intersection (0, 1, 2) = {2}
Lookup original document:
Flatten ID 2
->
Doc ID 1
Search Query B
Query:
foo[0].bar[*] = "y"
Lookup tokens:
| Token | Flatten Posting |
|---|---|
foo..bar., str, "y" |
1, 2 |
foo$idx, num, 0 |
0, 1, 2 |
Notes
- When there's a
[*]in the search query, we don't need to generate index constraint tokenprefix$idxto match the specific array index on that position.
Flatten Posting Intersection:
(1, 2) intersection (0, 1, 2) = {1, 2}
Lookup original document:
Flatten ID [1, 2]
->
Doc ID [0, 1]
Example 2: sibling arrays
{"foo":[{"bar":["x","y"]},{"bar":["a","b"]}],"foo2":["u"]}
{"foo":[{"bar":["y","z"]}],"foo2":["u"]}
Expected flattened sub-docs:
row0/sub0: foo$idx=0, foo..bar$idx=0, foo..bar.=x, foo2$idx=0, foo2.=u
row0/sub1: foo$idx=0, foo..bar$idx=1, foo..bar.=y, foo2$idx=0, foo2.=u
row0/sub2: foo$idx=1, foo..bar$idx=0, foo..bar.=a, foo2$idx=0, foo2.=u
row0/sub3: foo$idx=1, foo..bar$idx=1, foo..bar.=b, foo2$idx=0, foo2.=u
row1/sub0: foo$idx=0, foo..bar$idx=0, foo..bar.=y, foo2$idx=0, foo2.=u
row1/sub1: foo$idx=0, foo..bar$idx=1, foo..bar.=z, foo2$idx=0, foo2.=u
Query behavior:
foo[0].bar[0],str,y -> row1 only
foo[0].bar[*],str,y -> row0 and row1
Example 3: disable_cross_array_unnest
Input:
{"a":["x","y"],"b":["u","v"],"c":1}
Default disable_cross_array_unnest=false produces exact Cartesian-product sub-docs:
sub0: a$idx=0, a.=x, b$idx=0, b.=u, c=1
sub1: a$idx=0, a.=x, b$idx=1, b.=v, c=1
sub2: a$idx=1, a.=y, b$idx=0, b.=u, c=1
sub3: a$idx=1, a.=y, b$idx=1, b.=v, c=1
With disable_cross_array_unnest=true, sibling arrays are indexed independently:
sub0: a$idx=0, a.=x, c=1
sub1: a$idx=1, a.=y, c=1
sub2: b$idx=0, b.=u, c=1
sub3: b$idx=1, b.=v, c=1
This avoids combinatorial sub-doc growth. Queries that constrain values across multiple sibling arrays can sacrifice accuracy because no single flattened sub-doc contains terms from both sibling arrays.
Example 4: Tantivy nested-object false positive
Tantivy documents this pitfall for JSON arrays: a document is a bag of terms, so cart.product_type:sneakers AND cart.attributes.color:red can match even when sneakers and red come from different objects in the same cart array. FlattenedSubDocs mode avoids that by preserving each array element as a separate internal inverted-index document.
Raw Documents
row0 should not match because sneakers and red are in different cart objects:
{"cart_id":3234234,"cart":[{"product_type":"sneakers","attributes":{"color":"white"}},{"product_type":"t-shirt","attributes":{"color":"red"}}]}
row1 should match because both terms are in the same cart object:
{"cart_id":3234235,"cart":[{"product_type":"sneakers","attributes":{"color":"red"}}]}
Flattened Sub-Docs
row0/sub0: cart$idx=0, cart..product_type=sneakers, cart..attributes.color=white, cart_id=3234234
row0/sub1: cart$idx=1, cart..product_type=t-shirt, cart..attributes.color=red, cart_id=3234234
row1/sub0: cart$idx=0, cart..product_type=sneakers, cart..attributes.color=red, cart_id=3234235
Correct Query Shape For Nested-Object Semantics
Use one JSON MatchQuery that contains all related JSON triplets separated by ;, and set Operator::And on that MatchQuery:
FtsQuery::Match(
MatchQuery::new(
"cart[*].product_type,str,sneakers;cart[*].attributes.color,str,red".to_string(),
)
.with_column(Some("json_field".to_string()))
.with_operator(Operator::And),
)
Expected result:
row1 only
Here ; only separates JSON triplets. Operator::And is what requires all generated tokens to match, and because they are inside one MatchQuery, the intersection happens on flattened sub-doc ids before Lance maps matches back to row ids.
Anti-Example: Row-Level Boolean Composition
Do not express nested-object constraints as separate BooleanQuery.must children:
FtsQuery::Boolean(BooleanQuery {
must: vec![
MatchQuery::new("cart[*].product_type,str,sneakers".to_string())
.with_column(Some("json_field".to_string()))
.into(),
MatchQuery::new("cart[*].attributes.color,str,red".to_string())
.with_column(Some("json_field".to_string()))
.into(),
],
should: vec![],
must_not: vec![],
})
Each child MatchQuery runs independently and returns Lance row ids. The outer boolean query then composes those row ids, so row0 can still match at row level even though no single cart object contains both terms.
Apache Pinot JSON Index References
- JSON flattening behavior and array index keys: JsonUtils.flatten
- Pinot
disableCrossArrayUnnestconfig field: JsonIndexConfig - Mutable JSON index SPI, including write entrypoint and reader inheritance: MutableJsonIndex
- Immutable JSON index writer SPI: JsonIndexCreator
- Immutable JSON index reader SPI: JsonIndexReader
- Immutable writer implementation: BaseJsonIndexCreator
- Mutable implementation read/write behavior: MutableJsonIndexImpl
- Immutable reader implementation: ImmutableJsonIndexReader
Implementation roadmap
- Tokenizer and JSON paths: Split an array-bearing row into sub-documents and turn bracketed paths into index constraints. Existing indices keep their tokenizer behavior. feat(index): tokenize JSON arrays into sub-documents #7377
- JSON index configuration: Persist the tokenizer mode and ingestion policies, and expose sibling-array handling and row limits through Python and Java. feat(index): configure JSON sub-document ingestion #10563
- JSON write path: Write sub-documents through immutable builds and MemWAL, preserving their source rows through rebuild and flush. feat(index): write JSON sub-documents across index lifecycles wirybeaver/lance#3
- JSON query path: Match within sub-documents in flat and indexed search, then combine source rows and apply limits. Enable the new mode while keeping array positions exact in fuzzy search. feat(index): enable flattened JSON queries wirybeaver/lance#5
The follow-up PRs compare against the preceding fork branch. Submit each upstream after rebasing on the merged predecessor.
Activity
wirybeaver commentedon Sep 30, 2026
@Xuanwo @wjones127 @westonpace Would you mind taking a review of the algorithm depicted in the Issue and the implementation roadmap?