Skip to content

Commit

Permalink
HSEARCH-2172 Make it possible for different indexing services to use …
Browse files Browse the repository at this point in the history
…different NullMarkerCodecs

This essentially split responsibilities around null encoding:

 * encoding indexNullAs as an indexable value remains the responsibility
   of bridges implementing EncodingBridge, though they now do that
   more explicitely. They now return a simpler NullMarker instead
   of a NullMarkerCodec.
 * adding the encoded value to a document and querying null values
   remains the responsibility of the NullMarkerCodec, but it is
   now defined (based on the NullMarker) by a MissingValueStrategy,
   which is defined by the IndexManagerType.
  • Loading branch information
yrodiere authored and Sanne committed Dec 19, 2016
1 parent a952611 commit 59b2c7b
Show file tree
Hide file tree
Showing 33 changed files with 430 additions and 225 deletions.
Expand Up @@ -7,9 +7,12 @@
package org.hibernate.search.elasticsearch.bridge.builtin.impl;

import org.hibernate.search.bridge.TwoWayStringBridge;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.util.impl.EncodingStringBridge;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.elasticsearch.logging.impl.Log;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.StringHelper;
import org.hibernate.search.util.logging.impl.LoggerFactory;

Expand All @@ -22,7 +25,7 @@
* @author Sylvain Vieujot
* @author Yoann Rodiere
*/
public class ElasticsearchBooleanBridge extends EncodingStringBridge<Boolean> implements TwoWayStringBridge, IgnoreAnalyzerBridge {
public class ElasticsearchBooleanBridge implements EncodingBridge, TwoWayStringBridge, IgnoreAnalyzerBridge {

private static final Log LOG = LoggerFactory.make( Log.class );

Expand All @@ -48,6 +51,10 @@ public String objectToString(Object object) {
}

@Override
public NumericEncodingType getEncodingType() {
return NumericEncodingType.UNKNOWN;
}

protected Boolean parseIndexNullAs(String indexNullAs) throws IllegalArgumentException {
if ( Boolean.TRUE.toString().equals( indexNullAs ) ) {
return Boolean.TRUE;
Expand All @@ -59,4 +66,10 @@ else if ( Boolean.FALSE.toString().equals( indexNullAs ) ) {
throw LOG.invalidNullMarkerForBoolean();
}
}

@Override
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
Boolean booleanValue = parseIndexNullAs( indexNullAs );
return new ToStringNullMarker( booleanValue );
}
}
Expand Up @@ -11,14 +11,10 @@
import java.util.List;
import java.util.Set;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexableField;
import org.hibernate.search.analyzer.impl.AnalyzerReference;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NotEncodingCodec;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.elasticsearch.impl.ToElasticsearch;
import org.hibernate.search.elasticsearch.logging.impl.Log;
import org.hibernate.search.elasticsearch.schema.impl.model.DataType;
Expand All @@ -32,10 +28,12 @@
import org.hibernate.search.engine.impl.DefaultBoostStrategy;
import org.hibernate.search.engine.metadata.impl.BridgeDefinedField;
import org.hibernate.search.engine.metadata.impl.DocumentFieldMetadata;
import org.hibernate.search.engine.metadata.impl.DocumentFieldPath;
import org.hibernate.search.engine.metadata.impl.EmbeddedTypeMetadata;
import org.hibernate.search.engine.metadata.impl.FacetMetadata;
import org.hibernate.search.engine.metadata.impl.PropertyMetadata;
import org.hibernate.search.engine.metadata.impl.TypeMetadata;
import org.hibernate.search.engine.nulls.codec.impl.NullMarkerCodec;
import org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity;
import org.hibernate.search.engine.spi.EntityIndexBinding;
import org.hibernate.search.exception.AssertionFailure;
Expand Down Expand Up @@ -132,7 +130,7 @@ private void addPropertyMapping(ElasticsearchMappingBuilder mappingBuilder, Docu
logDynamicBoostWarning( mappingBuilder, sourceProperty.getDynamicBoostStrategy(), propertyPath );
}

addNullValue( propertyMapping, fieldMetadata );
addNullValue( propertyMapping, mappingBuilder, fieldMetadata );

for ( FacetMetadata facetMetadata : fieldMetadata.getFacetMetadata() ) {
try {
Expand Down Expand Up @@ -404,55 +402,33 @@ private DataType addTypeOptions(String fieldName, PropertyMapping propertyMappin
return elasticsearchType;
}

private void addNullValue(PropertyMapping propertyMapping, DocumentFieldMetadata fieldMetadata) {
private void addNullValue(PropertyMapping propertyMapping, ElasticsearchMappingBuilder mappingBuilder, DocumentFieldMetadata fieldMetadata) {
NullMarkerCodec nullMarkerCodec = fieldMetadata.getNullMarkerCodec();

if ( nullMarkerCodec != NotEncodingCodec.SINGLETON ) { // XXX NotEncodingCodec is not accessible...
JsonPrimitive nullTokenJson = retrieveNullTokenAsJson( propertyMapping.getType(), fieldMetadata, nullMarkerCodec );

NullMarker nullMarker = nullMarkerCodec.getNullMarker();
if ( nullMarker != null ) {
JsonPrimitive nullTokenJson = convertIndexedNullTokenToJson( mappingBuilder, fieldMetadata.getPath(), nullMarker.nullEncoded() );
propertyMapping.setNullValue( nullTokenJson );
}
}

private JsonPrimitive retrieveNullTokenAsJson(DataType dataType, DocumentFieldMetadata fieldMetadata, NullMarkerCodec nullMarkerCodec) {
Document dummyDocument = new Document();
LuceneOptions luceneOptions =
fieldMetadata.getSourceType().getFieldLuceneOptions( fieldMetadata.getSourceProperty(), fieldMetadata, null, 1.0f );

nullMarkerCodec.encodeNullValue( fieldMetadata.getAbsoluteName(), dummyDocument, luceneOptions );

List<IndexableField> fields = dummyDocument.getFields();

if ( fields.size() > 1 ) {
throw new AssertionFailure( "Expected only one field" );
}

if ( !fields.isEmpty() ) {
IndexableField field = fields.iterator().next();
return convertNullTokenFieldToJson( dataType, field );
}
else {
private JsonPrimitive convertIndexedNullTokenToJson(ElasticsearchMappingBuilder mappingBuilder,
DocumentFieldPath fieldPath, Object indexedNullToken) {
if ( indexedNullToken == null ) {
return null;
}
}

private JsonPrimitive convertNullTokenFieldToJson(DataType dataType, IndexableField field) {
Number numericValue = field.numericValue();
String stringValue = field.stringValue();
if ( numericValue != null ) {
return new JsonPrimitive( numericValue );
if ( indexedNullToken instanceof String ) {
return new JsonPrimitive( (String) indexedNullToken );
}
else if ( stringValue != null ) {
if ( DataType.BOOLEAN.equals( dataType ) ) {
// Parse the string so we send the proper type to Elasticsearch
return new JsonPrimitive( Boolean.parseBoolean( stringValue ) );
}
else {
return new JsonPrimitive( stringValue );
}
else if ( indexedNullToken instanceof Number ) {
return new JsonPrimitive( (Number) indexedNullToken );
}
else if ( indexedNullToken instanceof Boolean ) {
return new JsonPrimitive( (Boolean) indexedNullToken );
}
else {
return null;
throw LOG.unsupportedNullTokenType( mappingBuilder.getBeanClass(), fieldPath.getAbsoluteName(),
indexedNullToken.getClass() );
}
}

Expand Down
Expand Up @@ -8,6 +8,8 @@

import org.hibernate.search.analyzer.impl.RemoteAnalyzer;
import org.hibernate.search.analyzer.impl.RemoteAnalyzerProvider;
import org.hibernate.search.engine.nulls.impl.LuceneMissingValueStrategy;
import org.hibernate.search.engine.nulls.impl.MissingValueStrategy;
import org.hibernate.search.indexes.spi.AnalyzerExecutionStrategy;
import org.hibernate.search.indexes.spi.IndexManagerType;

Expand All @@ -24,6 +26,11 @@ public AnalyzerExecutionStrategy getAnalyzerExecutionStrategy() {
return AnalyzerExecutionStrategy.REMOTE;
}

@Override
public MissingValueStrategy getMissingValueStrategy() {
return LuceneMissingValueStrategy.INSTANCE;
}

@Override
public RemoteAnalyzer getRemoteAnalyzer(String name) {
return new RemoteAnalyzer( name );
Expand Down
Expand Up @@ -19,10 +19,10 @@
import org.hibernate.search.bridge.ParameterizedBridge;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.impl.DateResolutionUtil;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericLongNullCodec;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
Expand Down Expand Up @@ -109,9 +109,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericLongNullCodec( Long.parseLong( indexNullAs ) );
return new ToStringNullMarker( Long.parseLong( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForLong( e );
Expand Down
Expand Up @@ -11,13 +11,10 @@
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericDoubleNullCodec;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericFloatNullCodec;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericIntegerNullCodec;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericLongNullCodec;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
Expand Down Expand Up @@ -51,8 +48,8 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
return INT_FIELD_BRIDGE.createNullMarkerCodec( indexNullAs );
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
return INT_FIELD_BRIDGE.createNullMarker( indexNullAs );
}
},
/**
Expand All @@ -76,8 +73,8 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
return INT_FIELD_BRIDGE.createNullMarkerCodec( indexNullAs );
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
return INT_FIELD_BRIDGE.createNullMarker( indexNullAs );
}
},
/**
Expand All @@ -90,9 +87,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericIntegerNullCodec( Integer.parseInt( indexNullAs ) );
return new ToStringNullMarker( Integer.parseInt( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForInteger( e );
Expand All @@ -109,9 +106,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericFloatNullCodec( Float.parseFloat( indexNullAs ) );
return new ToStringNullMarker( Float.parseFloat( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForFloat( e );
Expand All @@ -128,9 +125,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericDoubleNullCodec( Double.parseDouble( indexNullAs ) );
return new ToStringNullMarker( Double.parseDouble( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForDouble( e );
Expand All @@ -147,9 +144,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericLongNullCodec( Long.parseLong( indexNullAs ) );
return new ToStringNullMarker( Long.parseLong( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForLong( e );
Expand Down
Expand Up @@ -13,9 +13,10 @@
import org.apache.lucene.search.Query;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.BridgeAdaptor;
import org.hibernate.search.bridge.util.impl.BridgeAdaptorUtils;
import org.hibernate.search.engine.nulls.codec.impl.NullMarkerCodec;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;

Expand Down Expand Up @@ -53,7 +54,8 @@ public Object get(String name, Document document) {
@Override
public String objectToString(Object object) {
if ( object == null ) {
return nullTokenCodec.nullRepresentedAsString();
NullMarker marker = nullTokenCodec.getNullMarker();
return marker == null ? null : marker.nullRepresentedAsString();
}
else {
return fieldBridge.objectToString( object );
Expand Down
Expand Up @@ -11,10 +11,10 @@
import org.apache.lucene.document.Document;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericLongNullCodec;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
Expand Down Expand Up @@ -75,9 +75,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericLongNullCodec( Long.parseLong( indexNullAs ) );
return new ToStringNullMarker( Long.parseLong( indexNullAs ) );
}
catch (NumberFormatException e) {
throw log.invalidNullMarkerForLong( e );
Expand Down
Expand Up @@ -11,10 +11,10 @@
import org.apache.lucene.document.Document;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericLongNullCodec;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
Expand All @@ -40,9 +40,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws IllegalArgumentException {
public NullMarker createNullMarker(String indexNullAs) throws IllegalArgumentException {
try {
return new NumericLongNullCodec( Long.parseLong( indexNullAs ) );
return new ToStringNullMarker( Long.parseLong( indexNullAs ) );
}
catch (NumberFormatException e) {
throw log.invalidNullMarkerForLong( e );
Expand Down
Expand Up @@ -11,10 +11,10 @@
import org.apache.lucene.document.Document;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.nullencoding.impl.NumericIntegerNullCodec;
import org.hibernate.search.bridge.spi.EncodingBridge;
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge;
import org.hibernate.search.bridge.spi.NullMarkerCodec;
import org.hibernate.search.bridge.spi.NullMarker;
import org.hibernate.search.bridge.util.impl.ToStringNullMarker;
import org.hibernate.search.metadata.NumericFieldSettingsDescriptor.NumericEncodingType;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;
Expand Down Expand Up @@ -58,9 +58,9 @@ public NumericEncodingType getEncodingType() {
}

@Override
public NullMarkerCodec createNullMarkerCodec(String indexNullAs) throws NumberFormatException {
public NullMarker createNullMarker(String indexNullAs) throws NumberFormatException {
try {
return new NumericIntegerNullCodec( Integer.parseInt( indexNullAs ) );
return new ToStringNullMarker( Integer.parseInt( indexNullAs ) );
}
catch (NumberFormatException e) {
throw LOG.invalidNullMarkerForInteger( e );
Expand Down

0 comments on commit 59b2c7b

Please sign in to comment.