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

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

Introduction

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

Prototype

String DEFAULT_INDEX_FIELD_NAME

To view the source code for org.apache.lucene.facet FacetsConfig DEFAULT_INDEX_FIELD_NAME.

Click Source Link

Document

Which Lucene field holds the drill-downs and ords (as doc values).

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;// www  .j  a  v a2 s .c o 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;
}

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

License:LGPL

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

    NestingMarker nestingMarker = null;//from   w  w  w . j  av  a  2 s. c  om
    JsonAccessorBuilder accessorBuilder = new JsonAccessorBuilder();
    for (IndexableField field : document.getFields()) {
        if (field instanceof NestingMarker) {
            nestingMarker = (NestingMarker) field;
            accessorBuilder.reset();
            accessorBuilder.append(((NestingMarker) field).getPath());
            continue; // Inspect the next field taking into account this metadata
        }

        String fieldPath = field.name();
        List<NestingPathComponent> nestingPath = nestingMarker == null ? null : nestingMarker.getPath();
        NestingPathComponent lastPathComponent = nestingPath == null ? null
                : nestingPath.get(nestingPath.size() - 1);
        EmbeddedTypeMetadata embeddedType = lastPathComponent == null ? null
                : lastPathComponent.getEmbeddedTypeMetadata();

        if (embeddedType != null && fieldPath.equals(embeddedType.getEmbeddedNullFieldName())) {
            // Case of a null indexed embedded

            // Exclude the last path component: it represents the null embedded
            nestingPath = nestingPath.subList(0, nestingPath.size() - 1);
            accessorBuilder.reset();
            accessorBuilder.append(nestingPath);

            Container containerType = embeddedType.getEmbeddedContainer();

            switch (containerType) {
            case ARRAY:
            case COLLECTION:
            case MAP:
                /*
                 * When a indexNullAs is set for array/collection/map embeddeds, and we get a null replacement
                 * token from the engine, just keep the token, and don't replace it back with null (which is
                 * what we do for other fields, see below).
                 * This behavior is necessary because Elasticsearch treats null arrays exactly as arrays
                 * containing only null, so propagating null for an array/collection/map as a whole would
                 * lead to conflicts when querying.
                 */
                String value = field.stringValue();

                accessorBuilder.buildForPath(fieldPath).set(root,
                        value != null ? new JsonPrimitive(value) : null);
                break;
            case OBJECT:
                // TODO HSEARCH-2389 Support indexNullAs for @IndexedEmbedded applied on objects with Elasticsearch
                break;
            default:
                throw new AssertionFailure("Unexpected container type: " + containerType);
            }
        } else if (FacetsConfig.DEFAULT_INDEX_FIELD_NAME.equals(field.name())) {
            /*
             * Lucene-specific fields for facets.
             * Just ignore such fields: Elasticsearch handles that internally.
             */
            continue;
        } else if (isDocValueField(field)) {
            /*
             * Doc value fields for facets or sorts.
             * Just ignore such fields: Elasticsearch handles that internally.
             */
            continue;
        } else if (fieldPath.equals(ProjectionConstants.OBJECT_CLASS)) {
            // Object class: no need to index that in Elasticsearch, because documents are typed.
            continue;
        } else {
            DocumentFieldMetadata documentFieldMetadata = indexBinding.getDocumentBuilder().getTypeMetadata()
                    .getDocumentFieldMetadataFor(field.name());

            if (documentFieldMetadata == null) {
                if (SpatialHelper.isSpatialField(fieldPath)) {
                    if (SpatialHelper.isSpatialFieldLatitude(fieldPath)) {
                        Number value = field.numericValue();
                        String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix(fieldPath);
                        accessorBuilder.buildForPath(spatialPropertyPath + ".lat").add(root,
                                value != null ? new JsonPrimitive(value) : null);
                    } else if (SpatialHelper.isSpatialFieldLongitude(fieldPath)) {
                        Number value = field.numericValue();
                        String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix(fieldPath);
                        accessorBuilder.buildForPath(spatialPropertyPath + ".lon").add(root,
                                value != null ? new JsonPrimitive(value) : null);
                    } else {
                        // here, we have the hash fields used for spatial hash indexing
                        String value = field.stringValue();
                        accessorBuilder.buildForPath(fieldPath).add(root,
                                value != null ? new JsonPrimitive(value) : null);
                    }
                } else {
                    // should only be the case for class-bridge fields; in that case we'd miss proper handling of boolean/Date for now
                    JsonAccessor accessor = accessorBuilder.buildForPath(fieldPath);
                    String stringValue = field.stringValue();
                    Number numericValue = field.numericValue();
                    if (stringValue != null) {
                        accessor.add(root, new JsonPrimitive(stringValue));
                    } else if (numericValue != null) {
                        accessor.add(root, new JsonPrimitive(numericValue));
                    } else {
                        accessor.add(root, null);
                    }
                }
            } else {
                JsonAccessor accessor = accessorBuilder.buildForPath(fieldPath);
                ExtendedFieldType type = FieldHelper.getType(documentFieldMetadata);
                if (ExtendedFieldType.BOOLEAN.equals(type)) {
                    FieldBridge fieldBridge = documentFieldMetadata.getFieldBridge();
                    Boolean value = (Boolean) ((TwoWayFieldBridge) fieldBridge).get(field.name(), document);

                    accessor.add(root, value != null ? new JsonPrimitive(value) : null);
                } else {
                    Number numericValue = field.numericValue();

                    if (numericValue != null) {
                        // If the value was initially null, explicitly propagate null and let ES handle the default token.
                        if (numericValue.toString().equals(documentFieldMetadata.indexNullAs())) {
                            numericValue = null;
                        }
                        accessor.add(root, numericValue != null ? new JsonPrimitive(numericValue) : null);
                    } else {
                        String stringValue = field.stringValue();
                        // If the value was initially null, explicitly propagate null and let ES handle the default token.
                        if (stringValue != null && stringValue.equals(documentFieldMetadata.indexNullAs())) {
                            stringValue = null;
                        }

                        accessor.add(root, stringValue != null ? new JsonPrimitive(stringValue) : null);
                    }
                }
            }
        }
    }

    return root;
}