override def ingestEmail()

in backend/app/services/index/ElasticsearchResources.scala [287:376]


  override def ingestEmail(email: Email, ingestion: String, sourceMimeType: String, parentBlobs: List[Uri], workspace: Option[WorkspaceItemContext], languages: List[Language]): Attempt[Unit] = {
    val collection = ingestion.split("/").headOption.getOrElse("unknown")
    val recipients = email.recipients.map { r => recipientToMap(languages, Some(r)) }

    val parentBlobUris = parentBlobs.map(_.value)

    val metadataList = email.metadata.map { case(key, values) =>
      Map(
        NestedField.key -> key,
        NestedField.values -> values
      )
    }

    val defaultFields = Map(
      IndexFields.`type` -> "email",
      IndexFields.text -> multiLanguageValue(languages, email.body),
      IndexFields.flags -> Flags.unseen,
      IndexFields.collection -> Set(collection),
      IndexFields.ingestion -> Set(ingestion),
      IndexFields.parentBlobs -> parentBlobUris,
      IndexFields.metadataField -> Map(
        IndexFields.metadata.mimeTypes -> List(sourceMimeType),
        IndexFields.metadata.fromField -> recipientToMap(languages, email.from),
        IndexFields.metadata.recipientsField -> recipients,
        IndexFields.metadata.sentAt -> email.sentAt.orNull,
        IndexFields.metadata.sensitivity -> email.sensitivity.map(_.toString).orNull,
        IndexFields.metadata.priority -> email.priority.map(_.toString).orNull,
        IndexFields.metadata.subject -> multiLanguageValue(languages, email.subject),
        IndexFields.metadata.references -> email.references,
        IndexFields.metadata.inReplyTo -> email.inReplyTo,
        IndexFields.metadata.html -> email.html.map(multiLanguageValue(languages, _)).orNull,
        IndexFields.metadata.attachmentCount -> email.attachmentCount,
        IndexFields.metadata.extractedMetadataField -> metadataList
      )
    ) ++ getWorkspaceFields(workspace)

    val createdAtField: Option[(String, Long)] = email.sentAtMillis().map(IndexFields.createdAt -> _)

    val upsertFields = defaultFields ++ createdAtField

    executeUpdate {
      updateById(indexName, email.uri.value)
        .script {
          Script(
            s"""
               |params.recipients
               |  .removeIf(recipient ->
               |    ctx._source.metadata.${IndexFields.metadata.recipientsField}
               |      .stream()
               |      .map(r -> r.address)
               |      .anyMatch(a -> a.equals(recipient.address))
               |    );
               |
               |ctx._source.metadata.${IndexFields.metadata.recipientsField}.addAll(params.recipients);
               |ctx._source.${IndexFields.collection}.add(params.collection);
               |ctx._source.${IndexFields.ingestion}.add(params.ingestion);
               |
               |params.parentBlobs.removeIf(uri -> ctx._source.${IndexFields.parentBlobs}.contains(uri));
               |ctx._source.${IndexFields.parentBlobs}.addAll(params.parentBlobs);
               |
               |if(params.workspaceBlobUri != null && params.workspaceId != null && params.workspaceNodeId != null) {
               |  if(ctx._source.${IndexFields.workspacesField} == null) {
               |    ctx._source.${IndexFields.workspacesField} = [[
               |      "${IndexFields.workspaces.uri}": params.workspaceBlobUri,
               |      "${IndexFields.workspaces.workspaceId}": params.workspaceId,
               |      "${IndexFields.workspaces.workspaceNodeId}": params.workspaceNodeId
               |    ]];
               |  } else {
               |    ctx._source.${IndexFields.workspacesField}.add([
               |      "${IndexFields.workspaces.uri}": params.workspaceBlobUri,
               |      "${IndexFields.workspaces.workspaceId}": params.workspaceId,
               |      "${IndexFields.workspaces.workspaceNodeId}": params.workspaceNodeId
               |    ]);
               |  }
               |}
           """.stripMargin.replaceAll("\\\r?\\\n", "").trim())
            .params(
              Map(
                "recipients" -> recipients.asJava,
                "collection" -> collection,
                "ingestion" -> ingestion,
                "parentBlobs" -> parentBlobUris.asJava,
                "workspaceBlobUri" -> workspace.map(_.blobAddedToWorkspace).orNull,
                "workspaceId" -> workspace.map(_.workspaceId).orNull,
                "workspaceNodeId" -> workspace.map(_.workspaceNodeId).orNull
              )
            ).lang("painless")
        }.upsert(upsertFields)
    }
  }