Example usage for org.apache.lucene.facet FacetsConfig stringToPath

List of usage examples for org.apache.lucene.facet FacetsConfig stringToPath

Introduction

In this page you can find the example usage for org.apache.lucene.facet FacetsConfig stringToPath.

Prototype

public static String[] stringToPath(String s) 

Source Link

Document

Turns an encoded string (from a previous call to #pathToString ) back into the original String[] .

Usage

From source file:org.hibernate.search.backend.elasticsearch.impl.ElasticsearchIndexWorkVisitor.java

License:LGPL

private JsonObject convertToJson(Document document, Class<?> entityType) {
    EntityIndexBinding indexBinding = searchIntegrator.getIndexBinding(entityType);
    JsonObject source = new JsonObject();

    String parentPath = null;/*from  ww w.  j a v a  2 s  .co m*/
    for (IndexableField field : document.getFields()) {
        // marker field for denoting the parent element of the subsequent fields
        if ("$nesting".equals(field.name())) {
            parentPath = field.stringValue();
            continue;
        }

        if (!field.name().equals(ProjectionConstants.OBJECT_CLASS)
                && !field.name().equals(indexBinding.getDocumentBuilder().getIdKeywordName())
                && !FacetsConfig.DEFAULT_INDEX_FIELD_NAME.equals(field.name()) && !isDocValueField(field)) {

            JsonObject parent = getOrCreateDocumentTree(source, parentPath);
            String jsonPropertyName = FieldHelper.getEmbeddedFieldPropertyName(field.name());

            DocumentFieldMetadata documentFieldMetadata = indexBinding.getDocumentBuilder().getTypeMetadata()
                    .getDocumentFieldMetadataFor(field.name());

            if (documentFieldMetadata == null) {
                if (SpatialHelper.isSpatialField(jsonPropertyName)) {
                    if (isNumeric(field) && (SpatialHelper.isSpatialFieldLatitude(jsonPropertyName)
                            || SpatialHelper.isSpatialFieldLongitude(jsonPropertyName))) {
                        // work on the latitude/longitude fields
                        Number value = field.numericValue();
                        String spatialJsonPropertyName = SpatialHelper
                                .getSpatialFieldRootName(jsonPropertyName);
                        JsonObject spatialParent;

                        if (parent.get(spatialJsonPropertyName) != null) {
                            spatialParent = parent.get(spatialJsonPropertyName).getAsJsonObject();
                        } else {
                            spatialParent = new JsonObject();
                            parent.add(spatialJsonPropertyName, spatialParent);
                        }

                        if (SpatialHelper.isSpatialFieldLatitude(jsonPropertyName)) {
                            addPropertyOfPotentiallyMultipleCardinality(spatialParent, "lat",
                                    value != null ? new JsonPrimitive(value) : null);
                        } else if (SpatialHelper.isSpatialFieldLongitude(jsonPropertyName)) {
                            addPropertyOfPotentiallyMultipleCardinality(spatialParent, "lon",
                                    value != null ? new JsonPrimitive(value) : null);
                        }
                    } else {
                        // here, we have the hash fields used for spatial hash indexing
                        String value = field.stringValue();
                        addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                                value != null ? new JsonPrimitive(value) : null);
                    }
                } else {
                    String[] fieldNameParts = FieldHelper.getFieldNameParts(field.name());

                    EmbeddedTypeMetadata embeddedType = getEmbeddedTypeMetadata(
                            indexBinding.getDocumentBuilder().getTypeMetadata(), fieldNameParts);

                    // Make sure this field does not represent an embeddable (not a field thereof)
                    if (embeddedType == null) {
                        // should only be the case for class-bridge fields; in that case we'd miss proper handling of boolean/Date for now
                        String stringValue = field.stringValue();
                        if (stringValue != null) {
                            addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                                    new JsonPrimitive(stringValue));
                        } else {
                            addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                                    field.numericValue() != null ? new JsonPrimitive(field.numericValue())
                                            : null);
                        }
                    }
                }
            } else if (FieldHelper.isBoolean(indexBinding, field.name())) {
                FieldBridge fieldBridge = documentFieldMetadata.getFieldBridge();
                Boolean value = (Boolean) ((TwoWayFieldBridge) fieldBridge).get(field.name(), document);
                addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                        value != null ? new JsonPrimitive(value) : null);
            }
            // TODO falling back for now to checking actual Field type to cover numeric fields created by custom
            // bridges
            else if (FieldHelper.isNumeric(documentFieldMetadata) || isNumeric(field)) {
                // Explicitly propagate null in case value is not given and let ES handle the default token set
                // in the meta-data
                Number value = field.numericValue();

                if (value != null && value.toString().equals(documentFieldMetadata.indexNullAs())) {
                    value = null;
                }

                addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                        value != null ? new JsonPrimitive(value) : null);
            } else {
                // Explicitly propagate null in case value is not given and let ES handle the default token set in the meta-data
                String value = field.stringValue();
                if (value != null && value.equals(documentFieldMetadata.indexNullAs())) {
                    value = null;
                }

                addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                        value != null ? new JsonPrimitive(value) : null);
            }
        } else if (FacetsConfig.DEFAULT_INDEX_FIELD_NAME.equals(field.name())
                && field instanceof SortedSetDocValuesField) {
            // String facet fields
            String[] facetParts = FacetsConfig.stringToPath(field.binaryValue().utf8ToString());
            if (facetParts == null || facetParts.length != 2) {
                continue;
            }
            String fieldName = facetParts[0];
            String value = facetParts[1];

            // if it's not just a facet field, we ignore it as the field is going to be created by the standard
            // mechanism
            if (indexBinding.getDocumentBuilder().getTypeMetadata()
                    .getDocumentFieldMetadataFor(fieldName) != null) {
                continue;
            }

            JsonObject parent = getOrCreateDocumentTree(source, fieldName);
            String jsonPropertyName = FieldHelper.getEmbeddedFieldPropertyName(fieldName);
            addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                    value != null ? new JsonPrimitive(value) : null);
        } else if (isDocValueField(field) && field instanceof NumericDocValuesField) {
            // Numeric facet fields: we also get fields created for sorting so we need to exclude them.
            if (indexBinding.getDocumentBuilder().getTypeMetadata()
                    .getDocumentFieldMetadataFor(field.name()) != null) {
                continue;
            }

            Number value;
            if (field instanceof DoubleDocValuesField) {
                // double values are encoded so we need to decode them
                value = Double.longBitsToDouble(field.numericValue().longValue());
            } else {
                value = field.numericValue();
            }
            JsonObject parent = getOrCreateDocumentTree(source, parentPath);
            String jsonPropertyName = FieldHelper.getEmbeddedFieldPropertyName(field.name());
            addPropertyOfPotentiallyMultipleCardinality(parent, jsonPropertyName,
                    value != null ? new JsonPrimitive(value) : null);
        }
    }

    return source;
}