Searching ISBNs and Catalogue Numbers in Elasticsearch

Customers type "978-1-62277-123-4", "9781622771234" or "g10234". How to make Elasticsearch find the exact product every time, rank it first, and handle ISBN-10, partial codes and editions.

Customers who type a product code know exactly what they want. Church musicians copy octavo numbers from a programme, teachers type ISBNs from a reading list, and bookshops paste codes from an order. If that search fails, it is the most frustrating kind of failure, because the customer has already done the work.

On many catalogue sites it does fail, for one reason: the default analyzer was built for words, not codes.

What goes wrong

The standard tokenizer splits on hyphens and some punctuation. 978-1-62277-123-4 becomes five tokens: 978, 1, 62277, 123 and 4. Then:

  • the query matches any product with a 1 or 123 somewhere in its text;
  • the right product is not necessarily first;
  • 9781622771234 without hyphens is one token and matches nothing, because the index only has the pieces.

Catalogue numbers have the same problem: G-10234, G10234 and g 10234 all produce different tokens.

Fix 1: a normalised keyword field

Store each code in a keyword field with a normalizer that removes hyphens, spaces and dots and lowercases the rest:

PUT catalogue
{
  "settings": {
    "analysis": {
      "char_filter": {
        "strip_separators": { "type": "pattern_replace", "pattern": "[\\s\\-\\.\\/]", "replacement": "" }
      },
      "normalizer": {
        "code": { "type": "custom", "char_filter": ["strip_separators"], "filter": ["lowercase"] }
      }
    }
  },
  "mappings": {
    "properties": {
      "codes": { "type": "keyword", "normalizer": "code" }
    }
  }
}

codes is an array: put every identifier for the product in it, such as the ISBN-13, the ISBN-10, your catalogue number and older numbers from previous editions. The normalizer runs at index time and at query time, so 978-1-62277-123-4 and 9781622771234 both become 9781622771234.

Fix 2: store ISBN-10 and ISBN-13

Older reading lists and customers still use ISBN-10. Convert at ingest and store both: the ISBN-13 is 978 plus the first nine digits of the ISBN-10, with a new check digit. Doing it once when indexing is far simpler than trying to match both forms at query time.

Fix 3: rank the exact code above everything

Search the code field alongside the normal text search, and give an exact code match a score nothing else can reach:

GET catalogue/_search
{
  "query": {
    "bool": {
      "should": [
        { "constant_score": { "filter": { "term": { "codes": "978-1-62277-123-4" } }, "boost": 100 } },
        { "multi_match": { "query": "978-1-62277-123-4", "fields": ["title^3", "composer^2", "description"] } }
      ]
    }
  }
}

The term query on a keyword field with a normalizer applies the normalizer to the query value, so hyphens do not matter. If the code exists, that product is first. If not, normal search still runs.

Fix 4: partial codes

Customers often type part of a code: 10234 without the G- prefix, or the first digits of an ISBN. Two options:

  • Prefix match on the normalised field: { "prefix": { "codes": "g1023" } }. Fast, and fine for "starts with".
  • Suffix variants at ingest: also store the number without its letter prefix (10234 next to g10234), so the bare number matches exactly.

Avoid wildcard queries with a leading *. They scan every term and get slow on large catalogues.

Fix 5: detect codes and skip the noise

If the whole query looks like a code (only digits, or a known prefix followed by digits), your application can send it straight to the code search and, on a single hit, go directly to the product page. Customers who paste an ISBN do not want a results list.

One work, many editions

A hymn can exist as an octavo, in a collection, as a digital download and on a recording, each with its own code. Index a shared work_id on all of them. When a customer finds one edition by its code, the product page can show the others. This also stops search results from listing the same piece eight times.

Check it with real codes

Take 100 codes from real orders and customer emails, in the form people typed them, and run them all. Every one should return the right product first. Repeat after every change to the analyzers.

This is often the first fix in our search reviews for publishers, because it is cheap and customers notice immediately. See catalogue search for publishers, and for other zero-result causes, why your catalogue search returns no results.

Talk to an Elasticsearch expert

We reply within one business day. Your details are used only to answer you.