Reputation: 2164
I've tried many ways to search, in a keyword query, for a Long field in my database. I always got errors because Hibernate Search will index the Long as a numeric field. I've tried using FieldBridge, but it just wouldn't work. Here is my field I want to search on.
@Field
@Column(unique = true)
private Long numericField;
Upvotes: 0
Views: 833
Reputation: 9977
Keyword queries support long fields just fine: you just have to provide a long value when querying, because the field is of type long. So just use Long.parseLong
to turn the text value provided by your user into a long before you pass it to Hibernate Search.
In some cases, you may really need to use a string field for your numeric value. For example if you're targeting multiple fields in the same keyword query, and some of the other fields are string fields.
Then your answer will work fine, though it unfortunately has some side effects that will cause Hibernate Search to reindex data more often (because it doesn't know where getNumbericFieldAsString
gets its data from exactly).
Alternatively, you could use bridges, either a custom one, or just this one which is built into Hibernate Search 5:
@Column(unique = true)
@Field(name="numericField", analyze = Analyze.NO, bridge = @FieldBridge(impl = org.hibernate.search.bridge.builtin.LongBridge.class))
private Long numericField;
For information about custom bridges, see here for Hibernate Search 5 and here for Hibernate Search 6 (the newer version of Hibernate Search, with a different API).
Upvotes: 1
Reputation: 2164
I couldn't find anything online on this issue so I'm posting my answer here Q&A-style. I figured a way to do it with a dynamic field, see below:
@Column(unique = true)
private Long numericField;
@Field(name="numericField", analyze = Analyze.NO, index=Index.YES)
public String getNumericFieldAsString(){
return numericField.toString();
}
The field is indexed dynamically as a String now and I can use it in my keyword query.
Upvotes: 1