예제 #1
0
 @Override
 public void preParse(ParseContext context) throws IOException {
   if (context.sourceToParse().id() != null) {
     context.id(context.sourceToParse().id());
     super.parse(context);
   }
 }
예제 #2
0
 @Override
 public void postParse(ParseContext context) throws IOException {
   if (context.id() == null && !context.sourceToParse().flyweight()) {
     throw new MapperParsingException("No id found while parsing the content source");
   }
   // if we did not have the id as part of the sourceToParse, then we need to parse it here
   // it would have been filled in the _id parse phase
   if (context.sourceToParse().id() == null) {
     super.parse(context);
     // since we did not have the uid in the pre phase, we did not add it automatically to the
     // nested docs
     // as they were created we need to make sure we add it to all the nested docs...
     if (context.docs().size() > 1) {
       UidField uidField = (UidField) context.rootDoc().getField(UidFieldMapper.NAME);
       assert uidField != null;
       // we need to go over the docs and add it...
       for (int i = 1; i < context.docs().size(); i++) {
         // we don't need to add it as a full uid field in nested docs, since we don't need
         // versioning
         context
             .docs()
             .get(i)
             .add(new Field(UidFieldMapper.NAME, uidField.uid(), Defaults.NESTED_FIELD_TYPE));
       }
     }
   }
 }
예제 #3
0
 @Override
 protected Field parseCreateField(ParseContext context) throws IOException {
   if (context.parser().currentName() != null
       && context.parser().currentName().equals(Defaults.NAME)) {
     // we are in the parsing of _parent phase
     String parentId = context.parser().text();
     context.sourceToParse().parent(parentId);
     return new Field(
         names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType);
   }
   // otherwise, we are running it post processing of the xcontent
   String parsedParentId = context.doc().get(Defaults.NAME);
   if (context.sourceToParse().parent() != null) {
     String parentId = context.sourceToParse().parent();
     if (parsedParentId == null) {
       if (parentId == null) {
         throw new MapperParsingException(
             "No parent id provided, not within the document, and not externally");
       }
       // we did not add it in the parsing phase, add it now
       return new Field(
           names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType);
     } else if (parentId != null
         && !parsedParentId.equals(Uid.createUid(context.stringBuilder(), type, parentId))) {
       throw new MapperParsingException(
           "Parent id mismatch, document value is ["
               + Uid.createUid(parsedParentId).id()
               + "], while external value is ["
               + parentId
               + "]");
     }
   }
   // we have parent mapping, yet no value was set, ignore it...
   return null;
 }
예제 #4
0
 @Override
 public void preParse(ParseContext context) throws IOException {
   // if we have the id provided, fill it, and parse now
   if (context.sourceToParse().id() != null) {
     context.id(context.sourceToParse().id());
     super.parse(context);
   }
 }
예제 #5
0
 @Override
 protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
   if (context.sourceToParse().token() != null) {
     Long token = context.sourceToParse().token();
     if (token != null) {
       fields.add(new LongFieldMapper.CustomLongNumericField(token, fieldType()));
       fields.add(new SortedNumericDocValuesField(fieldType().names().indexName(), token));
     }
   }
 }
  @Override
  protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    boolean parent = context.docMapper().isParent(context.type());
    if (parent) {
      addJoinFieldIfNeeded(fields, parentJoinFieldType, context.id());
    }

    if (!active()) {
      return;
    }

    if (context.parser().currentName() != null
        && context.parser().currentName().equals(Defaults.NAME)) {
      // we are in the parsing of _parent phase
      String parentId = context.parser().text();
      context.sourceToParse().parent(parentId);
      fields.add(
          new Field(
              fieldType().name(),
              Uid.createUid(context.stringBuilder(), parentType, parentId),
              fieldType()));
      addJoinFieldIfNeeded(fields, childJoinFieldType, parentId);
    } else {
      // otherwise, we are running it post processing of the xcontent
      String parsedParentId = context.doc().get(Defaults.NAME);
      if (context.sourceToParse().parent() != null) {
        String parentId = context.sourceToParse().parent();
        if (parsedParentId == null) {
          if (parentId == null) {
            throw new MapperParsingException(
                "No parent id provided, not within the document, and not externally");
          }
          // we did not add it in the parsing phase, add it now
          fields.add(
              new Field(
                  fieldType().name(),
                  Uid.createUid(context.stringBuilder(), parentType, parentId),
                  fieldType()));
          addJoinFieldIfNeeded(fields, childJoinFieldType, parentId);
        } else if (parentId != null
            && !parsedParentId.equals(
                Uid.createUid(context.stringBuilder(), parentType, parentId))) {
          throw new MapperParsingException(
              "Parent id mismatch, document value is ["
                  + Uid.createUid(parsedParentId).id()
                  + "], while external value is ["
                  + parentId
                  + "]");
        }
      }
    }
    // we have parent mapping, yet no value was set, ignore it...
  }
 @Override
 protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
   if (context.sourceToParse().routing() != null) {
     String routing = context.sourceToParse().routing();
     if (routing != null) {
       if (fieldType.indexOptions() == IndexOptions.NONE && !fieldType.stored()) {
         context.ignoredValue(fieldType.names().indexName(), routing);
         return;
       }
       fields.add(new Field(fieldType.names().indexName(), routing, fieldType));
     }
   }
 }
 @Override
 protected Field parseCreateField(ParseContext context) throws IOException {
   if (context.sourceToParse().routing() != null) {
     String routing = context.sourceToParse().routing();
     if (routing != null) {
       if (!indexed() && !stored()) {
         context.ignoredValue(names.indexName(), routing);
         return null;
       }
       return new Field(names.indexName(), routing, store, index);
     }
   }
   return null;
 }
 @Override
 public void validate(ParseContext context) throws MapperParsingException {
   String routing = context.sourceToParse().routing();
   if (path != null && routing != null) {
     // we have a path, check if we can validate we have the same routing value as the one in the
     // doc...
     String value = null;
     Fieldable field = context.doc().getFieldable(path);
     if (field != null) {
       value = field.stringValue();
       if (value == null) {
         // maybe its a numeric field...
         if (field instanceof NumberFieldMapper.CustomNumericField) {
           value = ((NumberFieldMapper.CustomNumericField) field).numericAsString();
         }
       }
     }
     if (value == null) {
       value = context.ignoredValue(path);
     }
     if (value == null) {
       // maybe its a numeric field
     }
     if (!routing.equals(value)) {
       throw new MapperParsingException(
           "External routing ["
               + routing
               + "] and document path routing ["
               + value
               + "] mismatch");
     }
   }
 }
예제 #10
0
 @Override
 public void postParse(ParseContext context) throws IOException {
   if (context.id() == null && !context.sourceToParse().flyweight()) {
     throw new MapperParsingException("No id found while parsing the content source");
   }
   // it either get built in the preParse phase, or get parsed...
 }
예제 #11
0
 @Override
 public Mapper parse(ParseContext context) throws IOException, MapperParsingException {
   if (context.sourceToParse().ttl() < 0) { // no ttl has been provided externally
     long ttl;
     if (context.parser().currentToken() == XContentParser.Token.VALUE_STRING) {
       ttl = TimeValue.parseTimeValue(context.parser().text(), null, "ttl").millis();
     } else {
       ttl = context.parser().longValue(coerce.value());
     }
     if (ttl <= 0) {
       throw new MapperParsingException(
           "TTL value must be > 0. Illegal value provided [" + ttl + "]");
     }
     context.sourceToParse().ttl(ttl);
   }
   return null;
 }
예제 #12
0
 @Override
 protected void innerParseCreateField(ParseContext context, List<Field> fields)
     throws IOException, AlreadyExpiredException {
   if (enabledState.enabled && !context.sourceToParse().flyweight()) {
     long ttl = context.sourceToParse().ttl();
     if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value
       ttl = defaultTTL;
       context.sourceToParse().ttl(ttl);
     }
     if (ttl > 0) { // a ttl has been provided either externally or in the _source
       long timestamp = context.sourceToParse().timestamp();
       long expire = new Date(timestamp + ttl).getTime();
       long now = System.currentTimeMillis();
       // there is not point indexing already expired doc
       if (context.sourceToParse().origin() == SourceToParse.Origin.PRIMARY && now >= expire) {
         throw new AlreadyExpiredException(
             context.index(), context.type(), context.id(), timestamp, ttl, now);
       }
       // the expiration timestamp (timestamp + ttl) is set as field
       fields.add(new CustomLongNumericField(this, expire, (NumberFieldType) fieldType));
     }
   }
 }
 @Override
 public void postParse(ParseContext context) throws IOException {
   if (context.sourceToParse().flyweight() == false) {
     parse(context);
   }
 }