|
| 1 | +--- |
| 2 | +title: "Global Index" |
| 3 | +weight: 6 |
| 4 | +type: docs |
| 5 | +aliases: |
| 6 | +- /pypaimon/global-index.html |
| 7 | +--- |
| 8 | +<!-- |
| 9 | +Licensed to the Apache Software Foundation (ASF) under one |
| 10 | +or more contributor license agreements. See the NOTICE file |
| 11 | +distributed with this work for additional information |
| 12 | +regarding copyright ownership. The ASF licenses this file |
| 13 | +to you under the Apache License, Version 2.0 (the |
| 14 | +"License"); you may not use this file except in compliance |
| 15 | +with the License. You may obtain a copy of the License at |
| 16 | +
|
| 17 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | +
|
| 19 | +Unless required by applicable law or agreed to in writing, |
| 20 | +software distributed under the License is distributed on an |
| 21 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 22 | +KIND, either express or implied. See the License for the |
| 23 | +specific language governing permissions and limitations |
| 24 | +under the License. |
| 25 | +--> |
| 26 | + |
| 27 | +# Global Index |
| 28 | + |
| 29 | +PyPaimon supports querying global indexes built on Data Evolution (append) tables. Three index types are available: |
| 30 | + |
| 31 | +- **BTree Index**: B-tree based index for scalar column lookups. Supports equality, IN, range, and combined predicates. |
| 32 | +- **Vector Index (Lumina)**: Approximate nearest neighbor (ANN) index for vector similarity search. |
| 33 | +- **Full-Text Index (Tantivy)**: Full-text search index for text retrieval with relevance scoring. |
| 34 | + |
| 35 | +> Global indexes must be built beforehand (e.g., via Spark or Flink). See [Global Index]({{< ref "append-table/global-index" >}}) for how to create indexes. |
| 36 | +
|
| 37 | +## BTree Index |
| 38 | + |
| 39 | +BTree index is automatically used during scan when a filter predicate matches the indexed column. No special API is needed — just set a filter on the read builder. |
| 40 | + |
| 41 | +```python |
| 42 | +import pypaimon |
| 43 | + |
| 44 | +catalog = pypaimon.create_catalog(...) |
| 45 | +table = catalog.get_table("db.my_table") |
| 46 | + |
| 47 | +# BTree index is used automatically when filtering on indexed columns |
| 48 | +read_builder = table.new_read_builder() |
| 49 | +read_builder = read_builder.with_filter( |
| 50 | + pypaimon.PredicateBuilder(table.fields) |
| 51 | + .in_("name", ["a200", "a300"]) |
| 52 | +) |
| 53 | + |
| 54 | +scan = read_builder.new_scan() |
| 55 | +read = read_builder.new_read() |
| 56 | +splits = scan.plan().splits |
| 57 | +data = read.to_arrow(splits) |
| 58 | +``` |
| 59 | + |
| 60 | +Supported predicates: `equal`, `not_equal`, `less_than`, `less_or_equal`, `greater_than`, `greater_or_equal`, `in_`, `not_in`, `between`, `is_null`, `is_not_null`. |
| 61 | + |
| 62 | +## Vector Index (Lumina) |
| 63 | + |
| 64 | +Use `VectorSearchBuilder` to perform approximate nearest neighbor search on a vector column, then read the matched rows. |
| 65 | + |
| 66 | +```python |
| 67 | +table = catalog.get_table("db.my_table") |
| 68 | + |
| 69 | +# Step 1: vector search to get matching row IDs |
| 70 | +builder = table.new_vector_search_builder() |
| 71 | +index_result = ( |
| 72 | + builder |
| 73 | + .with_vector_column("embedding") |
| 74 | + .with_query_vector([1.0, 2.0, 3.0, ...]) |
| 75 | + .with_limit(10) |
| 76 | + .execute_local() |
| 77 | +) |
| 78 | + |
| 79 | +# Step 2: read actual data for matched rows |
| 80 | +read_builder = table.new_read_builder() |
| 81 | +scan = read_builder.new_scan() |
| 82 | +scan.with_global_index_result(index_result) |
| 83 | +read = read_builder.new_read() |
| 84 | +data = read.to_arrow(scan.plan().splits) |
| 85 | +``` |
| 86 | + |
| 87 | +You can also add a scalar filter to pre-filter rows before vector search: |
| 88 | + |
| 89 | +```python |
| 90 | +predicate = ( |
| 91 | + pypaimon.PredicateBuilder(table.fields) |
| 92 | + .equal("category", "electronics") |
| 93 | +) |
| 94 | + |
| 95 | +index_result = ( |
| 96 | + table.new_vector_search_builder() |
| 97 | + .with_vector_column("embedding") |
| 98 | + .with_query_vector([1.0, 2.0, 3.0, ...]) |
| 99 | + .with_limit(10) |
| 100 | + .with_filter(predicate) |
| 101 | + .execute_local() |
| 102 | +) |
| 103 | + |
| 104 | +read_builder = table.new_read_builder() |
| 105 | +scan = read_builder.new_scan() |
| 106 | +scan.with_global_index_result(index_result) |
| 107 | +read = read_builder.new_read() |
| 108 | +data = read.to_arrow(scan.plan().splits) |
| 109 | +``` |
| 110 | + |
| 111 | +## Full-Text Index (Tantivy) |
| 112 | + |
| 113 | +Use `FullTextSearchBuilder` to perform full-text search on a text column, then read the matched rows. |
| 114 | + |
| 115 | +```python |
| 116 | +table = catalog.get_table("db.my_table") |
| 117 | + |
| 118 | +# Step 1: full-text search to get matching row IDs |
| 119 | +builder = table.new_full_text_search_builder() |
| 120 | +index_result = ( |
| 121 | + builder |
| 122 | + .with_text_column("content") |
| 123 | + .with_query_text("search keywords") |
| 124 | + .with_limit(20) |
| 125 | + .execute_local() |
| 126 | +) |
| 127 | + |
| 128 | +# Step 2: read actual data for matched rows |
| 129 | +read_builder = table.new_read_builder() |
| 130 | +scan = read_builder.new_scan() |
| 131 | +scan.with_global_index_result(index_result) |
| 132 | +read = read_builder.new_read() |
| 133 | +data = read.to_arrow(scan.plan().splits) |
| 134 | +``` |
| 135 | + |
| 136 | +For better performance when reading from remote storage, consider enabling the [Local Disk Cache]({{< ref "pypaimon/file-cache" >}}). |
0 commit comments