Ejemplo n.º 1
0
  private void serializeObject(final ParseContext context, String currentFieldName)
      throws IOException {
    if (currentFieldName == null) {
      throw new MapperParsingException(
          "object mapping ["
              + name
              + "] trying to serialize an object with no field associated with it, current value ["
              + context.parser().textOrNull()
              + "]");
    }
    context.path().add(currentFieldName);

    Mapper objectMapper = mappers.get(currentFieldName);
    if (objectMapper != null) {
      objectMapper.parse(context);
    } else {
      Dynamic dynamic = this.dynamic;
      if (dynamic == null) {
        dynamic = context.root().dynamic();
      }
      if (dynamic == Dynamic.STRICT) {
        throw new StrictDynamicMappingException(fullPath, currentFieldName);
      } else if (dynamic == Dynamic.TRUE) {
        // we sync here just so we won't add it twice. Its not the end of the world
        // to sync here since next operations will get it before
        synchronized (mutex) {
          objectMapper = mappers.get(currentFieldName);
          if (objectMapper == null) {
            // remove the current field name from path, since template search and the object builder
            // add it as well...
            context.path().remove();
            Mapper.Builder builder =
                context.root().findTemplateBuilder(context, currentFieldName, "object");
            if (builder == null) {
              builder = MapperBuilders.object(currentFieldName).enabled(true).pathType(pathType);
              // if this is a non root object, then explicitly set the dynamic behavior if set
              if (!(this instanceof RootObjectMapper) && this.dynamic != Defaults.DYNAMIC) {
                ((Builder) builder).dynamic(this.dynamic);
              }
            }
            BuilderContext builderContext =
                new BuilderContext(context.indexSettings(), context.path());
            objectMapper = builder.build(builderContext);
            putDynamicMapper(context, currentFieldName, objectMapper);
          } else {
            objectMapper.parse(context);
          }
        }
      } else {
        // not dynamic, read everything up to end object
        context.parser().skipChildren();
      }
    }

    context.path().remove();
  }
Ejemplo n.º 2
0
    @Override
    public OntologyMapper build(BuilderContext context) {
      ContentPath.Type origPathType = context.path().pathType();
      context.path().pathType(pathType);

      Map<String, FieldMapper<String>> fieldMappers = Maps.newHashMap();

      context.path().add(name);

      if (propertyBuilders != null) {
        for (String property : propertyBuilders.keySet()) {
          StringFieldMapper sfm = propertyBuilders.get(property).build(context);
          fieldMappers.put(sfm.names().indexName(), sfm);
        }
      }

      // Initialise field mappers for the pre-defined fields
      for (FieldMappings mapping : ontologySettings.getFieldMappings()) {
        if (!fieldMappers.containsKey(context.path().fullPathAsText(mapping.getFieldName()))) {
          StringFieldMapper mapper =
              MapperBuilders.stringField(mapping.getFieldName())
                  .store(true)
                  .index(true)
                  .tokenized(!mapping.isUriField())
                  .build(context);
          fieldMappers.put(mapper.names().indexName(), mapper);
        }
      }

      context.path().remove(); // remove name
      context.path().pathType(origPathType);

      return new OntologyMapper(
          buildNames(context),
          fieldType,
          docValues,
          indexAnalyzer,
          searchAnalyzer,
          postingsProvider,
          docValuesProvider,
          similarity,
          fieldDataSettings,
          context.indexSettings(),
          new MultiFields.Builder().build(this, context),
          ontologySettings,
          fieldMappers,
          threadPool);
    }
Ejemplo n.º 3
0
  @Override
  public void parse(ParseContext context) throws IOException {
    String iri;
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();

    if (token == XContentParser.Token.VALUE_STRING) {
      iri = parser.text();
    } else {
      throw new MapperParsingException(name() + " does not contain String value");
    }

    ContentPath.Type origPathType = context.path().pathType();
    context.path().pathType(ContentPath.Type.FULL);
    context.path().add(names.name());

    try {
      OntologyHelper helper = getHelper(ontologySettings, threadPool);

      OntologyData data = findOntologyData(helper, iri);
      if (data == null) {
        logger.debug("Cannot find OWL class for IRI {}", iri);
      } else {
        addFieldData(
            context,
            getPredefinedMapper(FieldMappings.URI, context),
            Collections.singletonList(iri));

        // Look up the label(s)
        addFieldData(context, getPredefinedMapper(FieldMappings.LABEL, context), data.getLabels());

        // Look up the synonyms
        addFieldData(
            context, getPredefinedMapper(FieldMappings.SYNONYMS, context), data.getLabels());

        // Add the child details
        addRelatedNodesWithLabels(
            context,
            data.getChildIris(),
            getPredefinedMapper(FieldMappings.CHILD_URI, context),
            data.getChildLabels(),
            getPredefinedMapper(FieldMappings.CHILD_LABEL, context));

        // Add the parent details
        addRelatedNodesWithLabels(
            context,
            data.getParentIris(),
            getPredefinedMapper(FieldMappings.PARENT_URI, context),
            data.getParentLabels(),
            getPredefinedMapper(FieldMappings.PARENT_LABEL, context));

        if (ontologySettings.isIncludeIndirect()) {
          // Add the descendant details
          addRelatedNodesWithLabels(
              context,
              data.getDescendantIris(),
              getPredefinedMapper(FieldMappings.DESCENDANT_URI, context),
              data.getDescendantLabels(),
              getPredefinedMapper(FieldMappings.DESCENDANT_LABEL, context));

          // Add the ancestor details
          addRelatedNodesWithLabels(
              context,
              data.getAncestorIris(),
              getPredefinedMapper(FieldMappings.ANCESTOR_URI, context),
              data.getAncestorLabels(),
              getPredefinedMapper(FieldMappings.ANCESTOR_LABEL, context));
        }

        if (ontologySettings.isIncludeRelations()) {
          // Add the related nodes
          Map<String, Collection<String>> relations = data.getRelationIris();

          for (String relation : relations.keySet()) {
            // Sanitise the relation name
            String sanRelation = relation.replaceAll("\\W+", "_");
            String uriMapperName = sanRelation + DYNAMIC_URI_FIELD_SUFFIX;
            String labelMapperName = sanRelation + DYNAMIC_LABEL_FIELD_SUFFIX;

            // Get the mapper for the relation
            FieldMapper<String> uriMapper =
                mappers.get(context.path().fullPathAsText(uriMapperName));
            FieldMapper<String> labelMapper =
                mappers.get(context.path().fullPathAsText(labelMapperName));

            if (uriMapper == null) {
              // No mappers created yet - build new ones for URI and label
              BuilderContext builderContext =
                  new BuilderContext(context.indexSettings(), context.path());
              uriMapper =
                  MapperBuilders.stringField(uriMapperName)
                      .store(true)
                      .index(true)
                      .tokenized(false)
                      .build(builderContext);
              labelMapper =
                  MapperBuilders.stringField(labelMapperName)
                      .store(true)
                      .index(true)
                      .tokenized(true)
                      .build(builderContext);
            }

            addRelatedNodesWithLabels(
                context,
                relations.get(relation),
                uriMapper,
                helper.findLabelsForIRIs(relations.get(relation)),
                labelMapper);
          }
        }
      }

      helper.updateLastCallTime();
    } catch (OntologyHelperException e) {
      throw new ElasticsearchException("Could not initialise ontology helper", e);
    } finally {
      context.path().remove();
      context.path().pathType(origPathType);
    }
  }