concept bridge in category hibernate

appears as: Bridges, bridge, bridge, Bridges, A bridge, BRIDGES, The bridge
Hibernate Search in Action

This is an excerpt from Manning's book Hibernate Search in Action.

3.3.2. Built-in bridges

A field bridge is the Hibernate Search infrastructure responsible for bridging the object world to the index world. Each property value will be passed to a field bridge, which will add it into the Lucene Document representing the entity instance. Bridges convert an object type into a string. Some field bridges also know how to convert the string back into the original object structure (which, you’ll see later, is useful for identity properties and projected fields).

All built-in bridges provided by Hibernate Search are two-way bridges (see table 3.1), but bridges degrading (or losing) information in the process of converting the object instance to a string representation are not uncommon. Because information is lost, building back the object representation is either approximative or simply not possible.

A bridge taking a Date object and indexing its year is a typical example. A bridge reading a PDF (represented by an array of bytes) and indexing the content also loses information; all the text structure, style, and metadata are gone.

While listing all the use cases where bridges are useful is quite frankly impossible, some examples will help you get a better grasp on bridges and unleash your imagination. You can:

  • Index the text in a PDF represented by an array of bytes; the bridge needs to extract the text from the PDF (see section 13.2.1).
  • Index a Microsoft Word document located at a given URL; the bridge needs to access the URL, read the Word document, and extract the text (see section 13.2.2).
  • Often a bridge is simply a conversion routine from an object representation to a string representation. You might also have to implement the routine to convert the string representation back to the object if the bridge is used on an identifier property or on a property meant to be projected. Let’s first discover how to write the simplest bridge: the conversion routine from an object to a string.

    Let’s implement the bridge used in listing 4.1. Listing 4.4 shows both the declaration and the implementation of the field bridge. The bridge implementation is a StringBridge that rounds and pads doubles.

    Listing 4.4. Declare the use of a bridge and implement it