Search for calculated fields

Hello,
i am using Hibernate Search 6. I would like to search in a calculated field. Is it possible?

My object:

@Indexed
public class ObjectA {
    @Column(name = "value1", precision = 22, scale = 6)
	@GenericField(sortable = Sortable.YES)
	private BigDecimal value1;
	
	@Column(name = "value2", precision = 22, scale = 6)
	@GenericField(sortable = Sortable.YES)
	private BigDecimal value2;
}

i would like to get results if value2 - value1 > 0.

pf.range().field(“value2-value1”).greaterThan(new BigDecimal(0)).toPredicate();

Hi,

I’d suggest indexing value2-value1 in a calculated field.

See Reindexing when a derived value changes with @IndexingDependency.

@Indexed
public class ObjectA {
    @Column(name = "value1", precision = 22, scale = 6)
	@GenericField(sortable = Sortable.YES)
	private BigDecimal value1;
	
	@Column(name = "value2", precision = 22, scale = 6)
	@GenericField(sortable = Sortable.YES)
	private BigDecimal value2;

	@Transient
	@IndexingDependency(derivedFrom = {
		@ObjectPath(@PropertyValue(propertyName = "value1")),
		@ObjectPath(@PropertyValue(propertyName = "value2"))
	})
	@ScaledNumberField(decimalScale = 6)
	public BigDecimal getValue2MinusValue1() {
		return value2.add(value1.negate());
	}
}
pf.range().field(“value2MinusValue1”).greaterThan(BigDecimal.ZERO).toPredicate();

Thank you again. It works fine!