# Numeric enum mapping

**URL:** <https://discourse.hibernate.org/t/numeric-enum-mapping/7179>\
**Category:** Hibernate Search\
**Created:** [January 23, 2023, 1:52am UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179 "2023-01-23T01:52:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 23, 2023, 1:52am UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179/1 "2023-01-23T01:52:28Z")

</div>

This migration is quite a challenge for me because every day there are new issues to solve. 🤣  
For example, enum `Imporance` is not creating its numeric field in Lucene no matter I wrote 2 separate annotations - one for String version and one for Numeric version. When I check created index with Luke, String version is handled properly while numeric one is empty.

(P.S. I need numeric importance to sort articles by real int importance and not alphabetically by enum name.)

(P.P.S. I didn’t create new topic because so far I’m not sure is it a bug or I went crazy going through all those migrations. 🤣 )

 ![xxx2](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/94bf097279f8023cdbebd4b1cd64538ff4b49cb8.png)

* * *

**Article.java**

```auto
@GenericField(name = "importance", projectable = Projectable.NO,
	searchable = Searchable.YES, sortable = Sortable.NO)
@GenericField(name = "importance_numeric", projectable = Projectable.NO,
	searchable = Searchable.YES, sortable = Sortable.YES,
	valueBridge = @ValueBridgeRef(type = ImportanceToNumericBridge.class))
@Enumerated(EnumType.STRING)
private Importance importance = Importance.NORMAL;

```

* * *

**Importance.java**

```auto
@AllArgsConstructor @Getter
enum Importance {
	FIRST(3), TOP(2), HIGH(1), NORMAL(0), LOW(-1), BOTTOM(-2), LAST(-3);
	private final int score;
}

```

* * *

**ImportanceToNumericBridge.java**

```auto
class ImportanceToNumericBridge implements ValueBridge<Importance, Integer> {
	@Override
	public Integer toIndexedValue(
			Importance value, ValueBridgeToIndexedValueContext context
	) {
		if( value == null) {
			throw new IllegalStateException("Importance should not be NULL");
		}
		Integer score = Integer.valueOf(value.getScore());
		if( score.intValue() != 0 ) {
			// This message shows up 14 times in console which
			// means the bridge returns expected values.
			System.out.println("Here I am being not zero!");
		}
		return score;
	}
}

```

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [January 23, 2023, 7:41am UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179/2 "2023-01-23T07:41:26Z")

</div>

Hey,

> [@horvoje](#):
>
> (P.P.S. I didn’t create new topic because so far I’m not sure is it a bug or I went crazy going through all those migrations. 🤣 )

Please do, for the sake of others looking for answers.

> [@horvoje](#):
>
> When I check created index with Luke

Two things:

1. Did you [reindex](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-batchindex-massindexer)?
2. Are you sure that Luke even displays a count of terms for numeric fields? I’m not, since the concept of “term” only makes sense in the context of text fields. Luke is great but sometimes confusing that way. I would recommend testing fields are getting indexed by running a query instead of using Luke.

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 23, 2023, 5:56pm UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179/3 "2023-01-23T17:56:12Z")

</div>

Hi!

I’m using Spring Profiles and have separate profile for developmet, test and production.

application-dev.properties:

```auto
com.thevegcat.app.config.switches.reindex-lucene-at-startup = true

```

This swhich is `true` during any work on search or anything else that could interfere with indexed data.  
Later in a service that I run after application boot I have:

```auto
if (Boolean.TRUE.equals( this.customProperties.getSwitches().getReindexLuceneAtStartup() ) ) {
	this.reindexingService.reindex();
}

```

And my reindexing is here:

```auto
final MassIndexer massIndexer =
	Search
		.session(this.entityManager)
		.massIndexer()
		.monitor(new CustomMassIndexingMonitor(this))
		.purgeAllOnStart(true)
		.mergeSegmentsAfterPurge(true)
		.mergeSegmentsOnFinish(true);
massIndexer.startAndWait();

```

I guess this should be enough, but if you think I should delete index files before application start, I can do this too.

* * *

Few minutes ago I downloaded the index from production environment - HibernateSearch5 and Lucene5.

 ![old-hibernate-search-old-lucene](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/3/3e3f4c317057c6a966c1de2b61399a4a78e083cf.png)

I’m not sure I understand `Freq` value in this case but for sure those 14 items are articles from my friend’s company that I want to put at the front and they have `HIGH` importance which has numeric value (int)1. And also the next value above in sum with 14 gives other `Freq` values and also there are 5 entries. Which tells me something got indexed in contrary to Lucene 8 which has 0 entries for the same field.

```auto
mysql> SELECT DISTINCT importance, COUNT(*) FROM article GROUP BY importance;
+------------+----------+
| importance | COUNT(*) |
+------------+----------+
| NORMAL | 2852 |
| HIGH | 14 |
+------------+----------+
2 rows in set (0.02 sec)

```

---

<div class="post-metadata">

**Author:** ![horvoje](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/horvoje/32/779_2.png) [@horvoje](https://discourse.hibernate.org/u/horvoje)\
**Post date:** [January 23, 2023, 6:19pm UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179/4 "2023-01-23T18:19:00Z")

</div>

Ok, that one is - I’m pretty sure - false positive.  
After reindexing with Hibernate Search 6 and opening index with Luke - the true is there are no “terms” stored for “importance\_numeric” as there was with previous Hibernate Search 5, but when I go to sorting tab of Luke and sort documents by `importance_numeric` DESC, there is visible “disorder” in document ID which tells me top 14 documents are those marked with enum `HIGH` (int 1) because their ID’s are not in ascending order.

On first screen there are 10 Doc ID’s (16, 21, 26, 30, 34, 38, 42, 44, 48, 49) and on second screen here are 4 Doc ID’s (55, 61, 66, 340) which are not in natural order and the count of 14 is the same as the count of my documents marked as `HIGH` (int 1). After those 14 documents, Doc ID’s is sorted in natural order which tells me all other documents except those 14 have the same value of `NORMAL` (int 0).

Right now I cannot prove it as my application is not in runnable state, but I’d bet on $1 this sort is working no matter it has no “terms” as is used to have before.

* * *

 ![screen1](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/6/6798045e9cb2303ff0011ab31d59600cee7b614f.png)

* * *

 ![screen2](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/8/88ca2ed1f48d7b1c9295d09bd57fb7dacbf8a760.png)

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [January 24, 2023, 7:30am UTC](https://discourse.hibernate.org/t/numeric-enum-mapping/7179/5 "2023-01-24T07:30:00Z")

</div>

Right, I suspect there’s nothing to see here. This is a simple use case that’s tested again and again in our test suite, so I’d be surprised if there was such a blatant bug.

On the other hand, I wouldn’t be surprised if your Hibernate Search 5 “numeric” bridge actually indexed your property both as text and as a number. That’s a very, very common thing in examples of Hibernate Search 5 bridges I saw on the web.
