How Infino makes OR queries fast
Introduction
Search engines have long been built for people typing into a search box. People type short queries, and the average query in a 1998 AltaVista log had 2.35 terms [1]. Google matched these queries with AND, and returned only pages that had all of the search terms [2]. Given the size of the web, this was a sensible default, as thousands of pages usually still had every word of a query. Additionally, AND is the cheap case for a search engine, since the rarest word in the query decides which documents need to be looked at.
Agents, on the other hand, write their own queries, and these tend to be longer than the 2 to 4 terms of a typical human query [3]. An agent adds words from the task it was given and from what it has read so far [4]. For example, consider an agent helping a support engineer, which searches a table of tickets for customer charged twice refund not received after cancellation. Hardly any ticket has all eight words, so an AND would return almost nothing, and would miss a ticket that says “charged twice after I cancelled, still no refund”. Thus, keyword search for agents usually treats the words as an OR, and lets BM25 rank every ticket that has any of them, where a ticket with more of the words, or with rarer ones, scores higher. OR is the default in Elasticsearch’s match query [5], and in Infino’s bm25_search as well [6].
A ticket can also say the same thing in different words, say “I was billed two times and never got my money back”, which shares no word with the query. An agent can add synonyms to its query, which makes the OR longer still, or it can search by meaning as well, with vector embeddings. As a result, agents often pair keyword search with semantic search, aka hybrid search, where keyword search finds exact terms such as an order number or an error code, which embeddings can miss [7], and semantic search finds documents that use other words for the same thing. Infino supports this with hybrid_search, which runs BM25 and vector search over the same table and merges the two rankings with reciprocal rank fusion [8]. Note that the keyword half of a hybrid search is the same OR query, so what we describe here applies to hybrid search as well.
Unlike AND, OR gets more expensive as a query gets longer. Every additional term adds its documents to the matches, and a long OR of common words can match most of a table while the agent needs only the top 10. While designing Infino, which as of this writing answers the union queries of the Search Benchmark Game in 0.41 to 0.48 of Lucene’s query time [9], we had to make OR queries fast at two levels, namely (a) inside a superfile (Infino’s Parquet file that holds data and indexes), and (b) across the thousands of superfiles that make up a large table. In this post, we describe how Infino runs an OR query ranked by BM25 at both levels, and we’ll cover hybrid search in a separate post.
Our previous post on Infino’s object-store architecture may be helpful for the second part, but is not required before reading further.
Dynamic pruning
For every term, a full-text index keeps a postings list, which holds the documents that contain the term, in document order, along with how often the term appears in each. For an AND query, the engine walks the postings of the rarest term and checks each of its documents against the other terms by skipping ahead in their postings, so the cost of the query depends mostly on the rarest term. For an OR query, every document in any of the postings lists is a match, and its score is the sum of the BM25 scores of the query terms it contains. Thus, a naive engine merges all the postings lists and scores every document it finds.
That said, a top-k query only needs the k best documents (and not the score of every match). The engine keeps the k best documents found so far in a heap, and the score of the k-th best of them is the threshold a new document has to beat to get into the heap. This threshold only goes up as the query proceeds. Additionally, the index stores the highest score each term can give any document, which we call the term’s score bound. A document can’t score more than the sum of the bounds of the terms it contains, so the engine can skip a document whose bounds add up to no more than the threshold, without scoring it. This idea is called dynamic pruning, and the two classic ways of applying it to OR queries are WAND [10] and MaxScore [11].
Infino is built on MaxScore, which splits the terms of a query into essential and non-essential terms. The engine sorts the terms by their score bound, and adds up the bounds starting from the weakest term. The weakest terms, whose bounds add up to no more than the threshold, are non-essential, since a document that has only those terms can’t get into the top k. The engine looks for candidates only in the postings of the essential terms, and checks the non-essential terms only for candidates that can still make it into the top k.
For example, say the four terms in the diagram have score bounds of 7.9 (refund), 5.2 (cancellation), 3.1 (charged) and 2.4 (twice). At the start of the query, the threshold is 0, and all four terms are essential. Once the threshold reaches 9, charged and twice together (3.1 + 2.4 = 5.5) can’t beat it, so they become non-essential. Once the threshold goes past 10.7 (5.2 + 3.1 + 2.4), only refund is essential, and the query is now a walk over the postings of refund, with the other three terms checked for each of its documents. Note that the terms that drop out first are the common ones, as BM25 gives common terms a low weight, and these are also the terms with the longest postings lists, which the engine now only checks at candidates instead of reading in full.
Infino also stores a score bound for every block of every postings list [12], which is usually much lower than the bound of the term over its whole list. The engine uses these block bounds to skip whole blocks that can’t beat the threshold, and to bound the non-essential terms more tightly at each candidate.
The windowed kernel
The textbook way to merge the postings lists of an OR query is a document at a time, where the engine keeps one cursor per term in a heap ordered by the document each cursor is on, takes the smallest document, scores it with every cursor on that document, advances those cursors, and repeats. This works well when one term dominates the score. That said, for a union of common terms, the engine pays a heap operation and a comparison across cursors for each of the millions of matching documents, and this bookkeeping costs more than decoding the postings themselves.
Infino instead walks the documents of a superfile in windows of up to 8,192 documents [13]. Within a window, the engine goes through the essential terms one at a time, and for each posting of a term, it adds the term’s BM25 score at the document’s position in an array of 8,192 scores, and sets the document’s bit in a presence bitset. There is no heap and no comparison across terms, so each term is a tight sequential loop that the CPU can run several documents at a time with SIMD, and the score array (32 KB) and the bitset (1 KB) stay in the CPU’s cache for the whole window. Once all the essential terms are added, the engine walks the bitset in document order, one 64-bit machine word (64 documents) at a time, and checks each candidate against the threshold. If the best score in a word of the bitset can’t beat the threshold, the engine clears the whole word without looking at its documents.
The engine then checks the non-essential terms for the candidates that survive, strongest term first. It adds each term’s score to a candidate as it reads it, and drops the candidate as soon as its score so far, plus the bounds of the terms it hasn’t checked yet, can’t beat the threshold. Checking the strongest term first means that most candidates that can’t make it are dropped after one or two checks.
The split into essential and non-essential terms is recomputed for every window from the current threshold [14], so one kernel handles every kind of OR query without choosing a strategy up front. Early in a query, or for a union of words that are all common, the threshold is low, every term is essential, and the kernel is a plain SIMD sum over the window. As the threshold rises, terms drop out, and only the essential ones are summed. Eventually, a single term may be the only essential one, in which case the kernel walks that term a block at a time, scores eight postings at a time with SIMD, and keeps only those that can still beat the threshold. Additionally, within a window, a term can be treated as non-essential when its block bounds in that window are low, even if its bound over the whole list keeps it essential.
Lucene’s MaxScoreBulkScorer works the same way, with windows of 4,096 documents [15], and here, as in several other places, Infino builds directly on ideas that Lucene pioneered. The difference in the results below therefore comes from the details, and we think three of them matter most.
First, Infino stores the score bound of every block as a ready-to-use score, so testing a block against the threshold needs no BM25 computation, while Lucene stores the term frequencies and document lengths that can produce a block’s best score, and computes the bound from them with BM25 [15]. With bounds this cheap, Infino recomputes the split for every window and tests bounds per block and per candidate, while Lucene recomputes its split over larger windows, sized so that the cost of computing bounds is spread over enough documents, and again when the threshold rises substantially. Second, Lucene’s threshold starts at zero and rises as its top k fills up, while Infino starts the threshold above zero where it can, as described in the next section. Third, for counts, Lucene runs a union of two or more terms through the full query, while Infino avoids walking the union whenever it can, as described in the section after that. That said, we haven’t measured how much each of these details contributes on its own.
Starting the threshold above zero
The windowed kernel starts with a threshold of zero, so it can’t skip anything until the top k fills up. This hurts most when a rare term is ORed with a common one, say the preakness, as the kernel then scores documents of the one by one until the top k holds enough documents with preakness to raise the threshold. For such queries, where one term is much rarer than the others (16 times or more), Infino first ranks every document of the rare term, and checks the other terms for each of them [16]. A document without the rare term can score at most the sum of the other terms’ bounds, so if the k-th best score is already above that sum, the ranked documents of the rare term are the answer, and the engine never walks the postings of the common term. If not, the k-th best score found so far becomes the starting threshold of the windowed kernel. Additionally, when the rare term has fewer than k documents, the engine scores those documents in full, ranks the other terms on their own, and merges the two lists.
For a large k (64 or more), the threshold rises slowly even without a rare term, so the engine also starts it at a lower bound computed before the walk [17]. A document’s OR score is at least its score for any one of its terms, so the k-th best score of a single term is a lower bound on the k-th best score of the OR. The engine computes this for each term from the term’s highest-scoring blocks, and starts the walk at the largest of them.
Counting matches
Some queries only need the number of documents that match, say to show a result count, or to let an agent decide whether a search is too broad before it reads the results. A count needs no scores, and Infino also avoids walking the union of the postings whenever it can [18]. For two terms, the count is the sum of the two terms’ document counts, which the index already stores, minus the number of documents that have both terms, so the engine counts the cheaper AND instead. For three or more terms, the engine picks one of two shortcuts. If the terms’ postings add up to at least a sixteenth of the superfile’s documents, it ORs all of them into one bitset over the whole superfile and counts its bits, which skips the per-window bookkeeping. If instead one term has at least eight times as many documents as all the other terms combined, it takes that term’s document count from the index, and adds only the other terms’ documents that the common term doesn’t appear in. Otherwise, the engine walks the postings much as the top-k kernel does, with presence bits and no scores.
Across superfiles
The sections so far describe how Infino runs an OR query inside one superfile. A large table has thousands of superfiles, often on object storage, where every read is a network round trip, and a query that opens every superfile pays for those round trips before it scores anything. Thus, the other half of making OR queries fast is to open as few superfiles as possible.
Finding the superfiles that hold the terms
As described in our previous post, each table has a term index that lists, for every term, the superfiles the term appears in, along with its document count and score bound in each, and where its postings are. For an OR query, the superfiles that can have a match are the ones that hold at least one of its terms. That said, this rarely rules out a superfile for an OR of common words, since a common word appears in every superfile. On one 1B-document table of 141 superfiles, presence alone ruled out no superfile for any query we tried [19].
Opening the best superfiles first
The term index also gives the engine a score bound for each superfile, which is the sum of the bounds of the query terms the superfile holds, and the engine opens superfiles in descending order of this bound, up to 256 at a time. The superfiles of a query also share one threshold, where each superfile’s kernel publishes its k-th best score as it runs, and the threshold is the k-th best score across all of them [20]. This way, a superfile that opens late begins with the threshold the others have already raised. Just before a superfile opens, the engine also compares its bound with the threshold, and skips the superfile if its bound is lower.
Level heads
A superfile’s bound for a query is loose, as it assumes that one document has the best score of every query term at once, while the best document for refund is rarely the best document for cancellation as well. For this reason, the term index also keeps a level head for every term [21], which lists the term’s best documents across all the superfiles of the table, in descending order of how much the term adds to their score. A term with up to 2,048 documents keeps all of them in its head, and a more common term keeps its top eighth. The head is stored in levels, where the first level has 64 entries and each level after that is 8 times larger, so a query reads only as deep as it needs.
Before opening any superfile, a query reads the heads of its terms one level at a time [22]. Each document named in a head becomes a candidate, along with the scores of the terms seen for it so far. After each level, the engine computes two bounds. The first is a lower bound on the final k-th best score, which we call θ, as in the diagram, and which is the k-th best sum among the candidates, since a candidate’s score can only grow as more of its terms are seen. The second is an upper bound, for each superfile, on the score of any of its documents, which comes from the candidates the superfile holds and, for documents no head has named yet, from what the unread part of each head can still add. A document has the same length for all its terms, so the engine computes this upper bound separately for each document length, which keeps it much tighter than a plain sum of the terms’ bounds.
A superfile whose upper bound is below θ holds nothing that can make it into the top k, and the engine never opens it. A superfile that every term’s head lists in full is answered from the index, without opening it. The rest open in order of their best candidate, with θ as their starting threshold. Additionally, while the superfiles open, the engine checks the best candidates against the terms they haven’t been seen with, by reading only the postings blocks that cover those candidates, and uses what it finds to rule out the superfiles that haven’t opened yet. This is close to Fagin’s threshold algorithm [23] from the database literature, with each term’s head as one of its sorted lists.
Reading stops when no head can lower a bound that still matters, when the heads have been read to their ends, or once the query has read 256K head entries, and the engine then opens whatever is still in play. As a result, the answer is always exact, since the heads only rule out superfiles that can’t hold a document in the top k.
That said, the heads pay off only when opening superfiles is expensive. Infino reads them when a table has more superfiles than it opens at once (256), or when the table’s own measurements show that reading the heads costs less than the opens it saves [24]. Also, an OR of only common words often still opens most superfiles, since the scores of its candidates stay close together deep into the heads, and no superfile’s bound falls below θ.
Results
We measure OR queries with the Search Benchmark Game [9], an open benchmark that compares search engines on 5 million English Wikipedia documents, with queries derived from the AOL query log. The benchmark runs each query as an intersection, a union and a phrase, on a single thread and after a warm-up, and each engine’s index is merged into a single segment (for Infino, a single superfile). Of its 962 queries, 301 are unions of two or more terms. Since the whole index is one superfile, the benchmark measures only the kernel inside a superfile, and not the cross-superfile path described above.
The table below shows the average latency over the 301 union queries in microseconds, and in brackets, the query time relative to Lucene, as the benchmark’s page reports it, where Lucene is 1.00 and lower is better [25].
| Infino | Lucene with BP | Lucene | |
|---|---|---|---|
| Top 10 | 311 (0.48) | 493 (0.76) | 650 (1.00) |
| Top 100 | 531 (0.43) | 890 (0.73) | 1,222 (1.00) |
| Top 1000 | 1,121 (0.41) | 2,042 (0.75) | 2,732 (1.00) |
| Count | 286 (0.36) | 754 (0.95) | 795 (1.00) |
In Lucene with BP, the documents are reordered by recursive graph bisection, which places similar documents next to each other and makes block bounds tighter, and Infino reorders its documents the same way. Infino’s query time is 0.48 of Lucene’s for the top 10, 0.41 for the top 1000 and 0.36 for counts, while that of Lucene with BP is 0.76, 0.75 and 0.95.
We also break down the average top-10 latency by the number of terms in the union, again with the query time relative to Lucene in brackets [25].
| Terms | Queries | Infino | Lucene with BP | Lucene |
|---|---|---|---|---|
| 2 | 198 | 196 (0.52) | 299 (0.79) | 378 (1.00) |
| 3 | 83 | 391 (0.49) | 613 (0.77) | 799 (1.00) |
| 4 or more | 20 | 1,116 (0.41) | 1,917 (0.70) | 2,729 (1.00) |
Infino’s query time relative to Lucene falls as the number of terms grows, from 0.52 for two terms and 0.49 for three to 0.41 for four or more. This matters for agents, as their queries tend to be longer than a person’s. Note that the last row has only 20 queries, so its averages are noisier than the others.
Summary
Agents write longer queries than people do, and their keyword searches usually treat the words as an OR, which is the more expensive kind of query for a search engine. In this post, we described how Infino makes OR queries fast inside a superfile, with a windowed MaxScore kernel that recomputes which terms are essential for every window of 8,192 documents, and across superfiles, with a term index whose level heads let a query skip the superfiles that can’t hold its top k. On the union queries of the Search Benchmark Game, Infino’s query time is 0.41 to 0.48 relative to Lucene, against 0.73 to 0.76 for Lucene with its documents reordered.
If you have reached this far, please consider giving our open source repo (infino-ai/infino) a star on GitHub.
Footnotes
- Craig Silverstein, Monika Henzinger, Hannes Marais and Michael Moricz, Analysis of a Very Large AltaVista Query Log, SRC Technical Note 1998-014, later published in SIGIR Forum (1999). The average of 2.35 terms is computed over distinct queries (Table 2). ↩
- Interpreting Your Query, Google Guide, by Nancy Blachman and Jerry Peek: “Google returns only pages that match all your search terms.” ↩
- Mahsa Amani et al., Characterizing Web Search by Conversational LLM Agents, arXiv, September 2026. The authors find that agent-written web queries “remain substantially longer than traditional human generated queries”, and put typical human queries at 2 to 4 terms, based on Bendersky and Croft (2009). ↩
- Jingjie Ning et al., Agentic Search in the Wild: Intents and Trajectory Dynamics from 14M+ Real Search Requests, SIGIR 2026. On average, 54% of the new terms an agent adds when it reformulates a query appear in the evidence it has gathered so far. ↩
- Match query, Elasticsearch reference. The
operatorparameter of the match query isORby default. ↩ - SQL reference, Infino docs.
bm25_searchtakes a mode of'or', which matches any term and is the default, or'and', which requires all terms. ↩ - Anthropic, Introducing Contextual Retrieval, September 2024: “While embedding models excel at capturing semantic relationships, they can miss crucial exact matches.” ↩
- Hybrid search, Infino docs. The BM25 side takes the same
modeasbm25_search, withoras the default. ↩ - Search Benchmark Game. Select the
uniontag to see the union queries alone. Back to the text: a b - Andrei Broder, David Carmel, Michael Herscovici, Aya Soffer and Jason Zien, “Efficient query evaluation using a two-level retrieval process”, CIKM 2003. ↩
- Howard Turtle and James Flood, “Query evaluation: strategies and optimizations”, Information Processing & Management, 1995. ↩
- The idea of block bounds is from Shuai Ding and Torsten Suel, “Faster top-k document retrieval using block-max indexes”, SIGIR 2011. Infino stores each block’s bound as a precomputed score, along with a coarser bound for every run of 32 blocks, so the engine can skip a whole run with one comparison. ↩
OR_WINDOWin src/superfile/fts/reader/core.rs. For a small k (under 512) over one or two terms, a window ends early at the nearest block boundary of an essential term, so the threshold can rise after every block. ↩run_windowed_maxscorein src/superfile/fts/reader/scorers.rs. ↩INNER_WINDOW_SIZEand the outer windows sized from impacts in Lucene’s MaxScoreBulkScorer.java, and for how Lucene stores and scores block bounds, CompetitiveImpactAccumulator.java and MaxScoreCache.java. Back to the text: a bdispatch_or_algo,rare_led_pair,rare_ledandrare_splitin src/superfile/fts/reader/scorers.rs. For a two-term OR, the rare-term pass runs when k is at most 128 or the rare term has fewer than k documents. ↩union_seedin src/superfile/fts/reader/scorers.rs. It reads four times as many blocks as would hold k documents, since the best blocks often hold several of the top documents each. ↩or_count_unranked,OR_COUNT_BITSET_DENSITY_DIVISORandOR_COUNT_ANCHOR_DOMINANCEin src/superfile/fts/reader/core.rs. ↩- infino-ai/infino#880. On the same table, the score bounds described next cut a rare single term from 141 opened superfiles to about 68. ↩
SharedTopKin src/supertable/query/fts.rs. The shared threshold only skips documents that score strictly below the k-th best, so the merged answer, including ties, is the same as if every superfile ran on its own. ↩- src/supertable/manifest/term_index/level_head.rs. Level heads landed in infino-ai/infino#1395. Strictly, a term has one head per layer of the term index, where each commit adds a layer for its new superfiles, and the background optimize step folds the layers into one. ↩
- src/supertable/query/heads.rs. Queries with a negated term, or whose filter admits only some rows, skip the heads and use the superfile bounds alone. ↩
- Ronald Fagin, Amnon Lotem and Moni Naor, “Optimal aggregation algorithms for middleware”, PODS 2001. ↩
bound_ordered_open_windowin src/supertable/options.rs, and the decision to read the heads in src/supertable/query/fts.rs. ↩- The benchmark’s run of 4 Dec 2026, on a Google Cloud
c3-highcpu-8VM (Intel Xeon Platinum 8481C, 8 vCPUs, 16 GB). Infino is the 0.13.3 release, built from the published crate, and the code links above point to the same release. Lucene is version 10.6.0 in both setups. Note that Infino scores with Lucene’s default BM25 parameters (k1 = 1.2, b = 0.75), while the benchmark’s Lucene engines use k1 = 0.9 and b = 0.4, as they do in the upstream benchmark. Each query runs 10 times, and the averages are over every run of every query in the group, as the benchmark’s page computes them. The results are in results-full.json, and the benchmark’s README describes how to run it. Back to the text: a b