Details

      Description

      Hi,

      In the documentation (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#numeric-field-annotation) we have the example:

      Example 27. Defining a custom NumericFieldBridge for BigDecimal

      public class BigDecimalNumericFieldBridge extends NumericFieldBridge {
          private static final BigDecimal storeFactor = BigDecimal.valueOf(100);
      
          @Override
          public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
              if ( value != null ) {
                  BigDecimal decimalValue = (BigDecimal) value;
                  long tmpLong = decimalValue.multiply( storeFactor ).longValue();
                  Long indexedValue = Long.valueOf( tmpLong );
                  luceneOptions.addNumericFieldToDocument( name, indexedValue, document );
              }
          }
      
          @Override
          public Object get(String name, Document document) {
              String fromLucene = document.get( name );
              BigDecimal storedBigDecimal = new BigDecimal( fromLucene );
              return storedBigDecimal.divide( storeFactor );
          }
      }
      

      By checking the class (org.hibernate.search.bridge.builtin.NumericFieldBridge), I realized that it is now an 'enum' and the constructor is private respectively.

      I believe the documentation is incorrect, or a refactoring made it impossible to extend the class.

      Particularly, I'm using with Elasticsearch 5.6 like this:

      public class BigDecimalFieldBridge implements MetadataProvidingFieldBridge, TwoWayFieldBridge {
      
          @Override
          public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
              if (value != null) {
                  Double indexedValue = ((BigDecimal) value).doubleValue();
                  luceneOptions.addNumericFieldToDocument(name, indexedValue, document);
              }
          }
      
          @Override
          public Object get(String name, Document document) {
              String fromLucene = document.get(name);
              return StringHelper.isEmpty(fromLucene) ? null : new BigDecimal(fromLucene);
          }
      
          @Override
          public String objectToString(Object object) {
             return object == null ? null : object.toString();
          }
      
          @Override
          public void configureFieldMetadata(String name, FieldMetadataBuilder builder) {
              builder.field(name, FieldType.DOUBLE);
          }
      }
      

      If it's just the documentation, I can update it. We can even add more examples, such as a custom FieldBridge to a complex enum too.

      Test Cases.

      TestCase with error: https://github.com/frekele/hibernate-search-elasticsearch-test-case/tree/HSEARCH-2906
      Travis CI log: https://travis-ci.org/frekele/hibernate-search-elasticsearch-test-case/builds/282576064

      TestCase without error: https://github.com/frekele/hibernate-search-elasticsearch-test-case/tree/HSEARCH-2906-fixed
      Travis CI log: https://travis-ci.org/frekele/hibernate-search-elasticsearch-test-case/builds/282577388

        Attachments

          Activity

            People

            • Assignee:
              yrodiere Yoann Rodière
              Reporter:
              leandro.freitas Leandro Kersting de Freitas
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: