Hi everyone,
is there a way to use bitwise operator in hibernate search. For example I would like to get all the entities where value & 2 = 0.
Thank you in advance!
Hi everyone,
is there a way to use bitwise operator in hibernate search. For example I would like to get all the entities where value & 2 = 0.
Thank you in advance!
Hi,
No, I don’t think that’s possible (at least not with decent performance), even with native predicates that are not exposed in Hibernate Search.
However, if that makes sense, you can use a custom bridge to index bit values. That may be a bit wasteful, but it would work.
For example index each bit in its own field:
@GenericField
@GenericField(name = "value_bit0", valueBinder = @ValueBinderRef(type = IntegerMaskBinder.class, params = @Param("mask", "1"))
@GenericField(name = "value_bit1", valueBinder = @ValueBinderRef(type = IntegerMaskBinder.class, params = @Param("mask", "2"))
@GenericField(name = "value_bit2", valueBinder = @ValueBinderRef(type = IntegerMaskBinder.class, params = @Param("mask", "4"))
public Integer value;
public class IntegerMaskBinder implements ValueBinder {
@Override
public void bind(ValueBindingContext<?> context) {
int mask = Integer.parseInt((String) context.param("mask"));
context.bridge(
Integer.class,
(value, context) -> value == null ? null : value % mask
);
}
}
Then:
List<MyEntity> hits = Search.session(entityManager).search(MyEntity.class)
.where(f -> f.match().field("value_bit1").matching(0))
.fetchHits(20);
There are ways to improve on this, but it all depends on the requirements.