# Every string search I did not have to write

> Chemical names break naive search in about four ways. What Lucene gave us for free, which parts we actually had to write, and where it is the wrong answer.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-04-23
- Tags: Lucene, Java, Search, C#, NLP, JVM
- URL: https://deyanpeev.com/posts/every-string-search-i-did-not-have-to-write

The first version of any search feature is a `LIKE`:

```sql
SELECT id, title FROM substances WHERE name LIKE '%' + @q + '%';
```

It works for about a week. Then somebody searches for aspirin in a corpus
that spells it *acetylsalicylic acid*, somebody else pastes in `C9H8O4` and
gets nothing, a third person types `2-(acetyloxy)benzoic acid` with a
different kind of dash than the typesetter used, and the fourth complaint is
that the results come back in primary-key order, which no user has ever wanted.

You can push a fair distance further than that. Full-text indexes in SQL
Server or Postgres are real search engines and I've shipped features on both.
But once the *tokenisation rules are domain-specific* you need to own the
analysis chain, and owning the analysis chain is what Lucene is for. This note
is about what you get by stepping onto it, written after two years of doing
that on a chemistry corpus.

# What a chemical name does to a normal tokeniser

This is what `StandardAnalyzer`, the sensible default for English prose, does
with four real queries:

```text
input                          StandardAnalyzer produces
2-(acetyloxy)benzoic acid  →   2, acetyloxy, benzoic, acid
C9H8O4                     →   c9h8o4
α-D-glucose                →   α, d, glucose
50-78-2                    →   50, 78, 2
```

Every line is wrong in a different way. The first threw away the structure
that distinguishes one isomer from another. The second is technically intact
but lowercased into a token that will never match a document where the same
formula was written `C₉H₈O₄`. The third split a stereodescriptor off the head
of the name and left a bare `d` that now matches half the corpus. The fourth
turned a CAS registry number into three integers.

The naive fix is a pile of regular expressions in front of the query box, and
that's the wrong place for them. Whatever you do to the query you must already
have done to the document, or the two strings will never meet. Lucene enforces
that structurally. I think it is the most useful thing it teaches.

# The inverted index is the primitive worth having

Underneath the analysers Lucene is one data structure, and it's simple to
describe. For every term it keeps the sorted list of documents containing it,
with positions and offsets. `benzoic` → documents 4, 17, 903, at these word
positions, at these character offsets.

That structure is what makes search *cheap and ranked*. A scan compares the
query against every row. An inverted index intersects a handful of short lists
and then scores only the documents that survived. Phrase queries fall out of
the positions and highlighting falls out of the offsets. Ranking has somewhere
to stand too, because the index already knows the term's document frequency.

I didn't appreciate this until I had to read the internals, but almost all of
Lucene's cleverness is in making that structure small and sequential on disk.
The term dictionary is an FST. Postings are delta-encoded and bit-packed with
skip lists over them. Numerics live in a BKD tree so a range query does not
become a term query with ten thousand clauses. Segments are immutable and get
merged in the background, and that's how writes and reads stop fighting.

I would otherwise have had to write all of that, badly. It also answers "why
not just build an index in Postgres". You can, and it's a good index, but you
don't get to replace the tokeniser with one that understands
stereodescriptors.

# The analysis chain you write yourself

An `Analyzer` is a factory for a chain: character filters, then a tokeniser,
then token filters. Ours, roughly:

```java
final class ChemicalAnalyzer extends Analyzer {

  private final NormalizeCharMap symbols;      // α → alpha, ′ → ', ‐ ‑ ‒ – → -
  private final SynonymMap synonyms;           // curated, from the substance registry

  @Override
  protected Reader initReader(String field, Reader reader) {
    // Character filters run before tokenisation and, crucially, keep an
    // offset map so highlighting still points at the original text.
    return new MappingCharFilter(symbols, reader);
  }

  @Override
  protected TokenStreamComponents createComponents(String field) {
    Tokenizer source = new WhitespaceTokenizer();

    TokenStream out = new WordDelimiterGraphFilter(
        source,
        WordDelimiterGraphFilter.GENERATE_WORD_PARTS
            | WordDelimiterGraphFilter.GENERATE_NUMBER_PARTS
            | WordDelimiterGraphFilter.CATENATE_ALL
            | WordDelimiterGraphFilter.PRESERVE_ORIGINAL,
        null);

    out = new LowerCaseFilter(out);
    out = new SynonymGraphFilter(out, synonyms, /* ignoreCase */ true);
    out = new FlattenGraphFilter(out);          // index side only - see below
    out = new KeywordRepeatFilter(out);         // keep the unstemmed form too
    out = new PorterStemFilter(out);
    out = new RemoveDuplicatesTokenFilter(out);

    return new TokenStreamComponents(source, out);
  }
}
```

Five details in there took real time to learn.

`PRESERVE_ORIGINAL` with `CATENATE_ALL` is what handles names with
punctuation. `2-(acetyloxy)benzoic` indexes as the parts, as the concatenation
`2acetyloxybenzoic`, and as itself. A user who types any of the three finds the
document.

`FlattenGraphFilter` belongs on the index side only. Both graph filters can
emit a token *lattice*, which is one input position producing alternatives of
different lengths. The index format can't store a lattice, so you flatten it
and accept a slightly lossy result. At query time you must not flatten. The
query parser handles the graph properly and builds the right disjunction, and
if you flatten there you get phrase queries that silently stop matching.

`KeywordRepeatFilter` goes before the stemmer and `RemoveDuplicates` after.
Every token is indexed twice, stemmed and not, so an exact form outranks a
stemmed one without a second field. Those two lines removed an entire category
of complaint.

Symbol normalisation is a character filter, not a token filter. It has to
happen before tokenisation because it changes what the tokeniser sees, and
`MappingCharFilter` keeps the offset correction so a highlight still lands on
the right character in the source document. A hand-rolled `String.replace`
in front of the analyser loses that and your highlights drift by one character
per substitution.

Formulas and registry numbers don't go through any of this. They are a
separate, unanalysed field:

```java
doc.add(new StringField("formula", "C9H8O4", Field.Store.YES));   // exact, TermQuery
doc.add(new StringField("cas", "50-78-2", Field.Store.YES));
doc.add(new TextField("name", name, Field.Store.YES));            // analysed
doc.add(new TextField("body", body, Field.Store.NO));
```

`StringField` is one token, verbatim. Trying to make one clever analyser serve
both prose and identifiers is a mistake I watched us nearly make twice, and the
answer is always another field. `PerFieldAnalyzerWrapper` then keeps the
per-field choices in one place.

# The rule that costs the most to learn

Index-time and query-time analysis have to agree. When they don't there is no
error, a document that obviously matches just does not come back.

Two tools make that a ten-minute problem instead of an afternoon. One is
dumping the chain by hand, which we wrapped in a test helper:

```java
static List<String> terms(Analyzer analyzer, String field, String text) throws IOException {
  var out = new ArrayList<String>();
  try (TokenStream ts = analyzer.tokenStream(field, text)) {
    var term = ts.addAttribute(CharTermAttribute.class);
    ts.reset();
    while (ts.incrementToken()) out.add(term.toString());
    ts.end();
  }
  return out;
}

@Test
void formula_survives_analysis() {
  assertThat(terms(indexAnalyzer, "name", "2-(acetyloxy)benzoic acid"))
      .contains("2acetyloxybenzoic", "2", "acetyloxy", "benzoic", "acid");
}
```

We ended up with about ninety of those assertions and they're the highest
value tests in the search tier. They are also the ones a reindex will break
loudly, and loudly is what you want.

The other tool is `explain`:

```java
System.out.println(searcher.explain(query, docId));
```

It prints the full scoring tree: which clauses matched, each term's IDF, the
length normalisation, the final sum. My intuition about relevance rarely
survived contact with it. Half the times I was sure the ranking was broken, the
ranking was fine and my query had a clause I didn't intend.

# The extension points that made it ours

The argument for building on Lucene and not on a search *product* is that
every layer is replaceable, including layers you wouldn't expect.

- **`Similarity`.** BM25 is the default and is the right default. We tuned
  `k1` and `b` per field - `new BM25Similarity(1.2f, 0.35f)` on `name`,
  because length normalisation is close to meaningless on a field where every
  value is a substance name, and left `body` alone. A
  `PerFieldSimilarityWrapper` holds that.
- **Payloads.** The annotator emits a confidence per annotation, and it goes
  into the postings as a payload on the term, then into the score through a
  `PayloadScoreQuery`. A term the NLP pipeline was unsure about contributes
  less. There's nowhere to put that in a `LIKE` query, and nowhere obvious to
  put it in most search products either.
- **Intervals.** `Intervals.ordered(Intervals.term("acetylsalicylic"),
  Intervals.maxgaps(4, Intervals.term("synthesis")))` expresses *this name,
  then that word, within five positions*. It's a proximity language with real
  composition, and much easier to reason about then the spans queries it
  replaced.
- **DocValues.** A column-oriented per-document store, which is how you sort
  and facet without loading fields. Publication year as a `NumericDocValues`,
  substance class as `SortedSetDocValuesFacetField`.
- **`UnifiedHighlighter`.** Given offsets in the postings, it re-runs the
  query against a single document to produce snippets with the matched spans
  marked. It reuses the analysis chain, so a synonym hit highlights the word
  that is in the document.
- **`SearcherManager`.** Near-real-time search: an `IndexSearcher` is a
  snapshot, and the manager hands out the current one with reference counting
  so a reopen doesn't pull the index out from under a running query. Newly
  annotated documents appear in seconds without a restart.

All of that is in the box. It has saved me a lot of afternoons, and that's why
I keep listing Lucene under things I'm interested in and not under things I
once used.

# Where it is the wrong answer

Lucene is a library, and the failure mode with a library this capable is
treating it as if it were a database.

A Lucene index is one index, one `IndexWriter`, one process, with no
transactions spanning documents, no queries across indexes and no replication.
Durability is a `commit`, which is an fsync and not cheap, so you batch, and
if the process dies between commits that batch is gone and something upstream
must be able to replay it. Our source of truth was Virtuoso and the index was
derived, always. That was intended.

Changing analysis means reindexing. Everything above is baked into the terms
on disk, so adding one synonym rule to the analyser means every affected
document is wrong until it's rebuilt. Budget for a full rebuild being routine
and automated on day one, not something you discover in month four.

Distribution is not included. Sharding, replication, failover, a query API,
cluster state - that's the gap Solr and Elasticsearch fill, and if you need
those, use them and don't grow your own. Our corpus fit on one machine's index
with room to spare, and that is the only reason straight Lucene was
defensible.

Wildcards and regexes will eat you. A leading wildcard has to enumerate the
term dictionary. `RegexpQuery` is worse. If users need infix matching, index
n-grams at index time and pay for it there, in disk, where you can measure it.

If you're on .NET, know what you are signing up for. Lucene.NET is a careful,
faithful port and I've shipped on it, but the current release line is still
the 4.8.0 betas. So years of Java-side improvements that aren't available yet,
and a beta version number you have to explain to somebody. On this product it
settled which runtime the search tier lived on. The index was Java because
Lucene is Java, and the annotator stayed .NET for [reasons I wrote about a
fortnight ago](https://deyanpeev.com/posts/the-java-port-we-could-not-make-fast-enough), so each
runtime sat where its best library already was.

# Summary

I chose Lucene because the list of things I'd otherwise have had to write (an
FST term dictionary, a bit-packed postings format with skip lists, a BKD tree,
a merge policy, crash-safe commits, BM25, a highlighter that agrees with the
analyser) is a career, and it isn't what anybody is paying for.

What's left is yours to write. The analysis chain, the fields, the synonym
list, and ninety tests asserting that a chemical name still looks like itself
after normalisation. That work needs the domain knowledge, but it is a couple
of weeks and not a couple of years.
