I have an entity Product which has a property Manufacturer.
Manufacturer has its own bridge to get the data for index.
When indexing with Hibernate Search, I would like to add a token “manufacturer_not_set” to the Product for the case when Manufacturer property of Product is null. Later I could issue a query to get Products that has no Manufacturer just by querying this token.
Is there a way to get this behavior? Thanks!
P.S. The code I provided is not working as expected: Manufacturer is processed the right way when not null, but nothing happens when it’s null. I did put log.trace() into the bridge method and indexer calls bridge method when Manufacturer is null, but the token is not stored.
@Entity @Indexed
public class Product {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO,
bridge = @FieldBridge(impl = ManufacturerBridge.class))
private Manufacturer manufacturer;
}
public class ManufacturerBridge implements StringBridge {
@Override
public String objectToString(final Object object) {
if (object == null) { return "manufacturer_not_set"; }
if (object instanceof Manufacturer) {
return "manufacturer_" + ((Manufacturer) object).getId();
}
return "";
}
}
My questions are:
- Should I add “indexNullAs” to field definition? Should I add my own string such as “manufacturer_not_set” or is it reserved only for predefined values?
- This bridge is called during search query execution - why? Isn’t the bridge only to get properties of complex objects and add them to the Lucene index?
- For Manufacturer status I have another bridge that returns “manufacturer_enabled” or “manufacturer_disabled”; later when I use Boolean junction to create a query, only “must” works with it. If I put only one “should” with the same condition (i.e. match “manufacturer_enabled”) this thing does not work and I get all manufacturers; how to use this “should”
- If I do not put “indexNullAs” is it possible that bridge handles null i.e. returns “manufacturer_not_set” when it gets null as an input?
P.S. Been losing my time for few days before writing this post, so the only other thing I can imagine is to get source code and go step by step through indexing process and then through search process - which could take large amounts of time.
Thanks a lot!!!