<commit_before>package org.scalex
package elastic

import com.sksamuel.elastic4s.source.JsonDocumentSource

import scala.concurrent.{ Future }
import scala.util.{ Success, Failure }

import akka.actor._
import akka.pattern.{ pipe }
import com.sksamuel.elastic4s.{ElasticDsl ⇒ ES, CountDefinition, ElasticClient, SearchDefinition}
import com.sksamuel.elastic4s.ElasticDsl._
import com.typesafe.config.Config
import play.api.libs.json.{JsString, Json, JsObject}

private[scalex] final class ElasticActor(
    config: Config,
    indexName: String,
    indexSettings: Map[String, String]) extends Actor with ActorLogging {

  import ElasticActor._

  var client: ElasticClient = _

  override def preStart {
    client = instanciateElasticClient
    self ! EnsureIndex
  }

  override def postStop {
    println("[search] Stop elastic client")
    Option(client) foreach { _.close() }
  }

  def receive = {

    case EnsureIndex ⇒ client execute {
      ES.create index indexName // mappings (typeName as mapping)
    }

    case api.Clear(typeName, mapping) ⇒ Future {
      try {
        client execute { ES.delete from s"$indexName/typeName" where ES.matchall }
      }
      catch {
        case e: org.elasticsearch.indices.TypeMissingException ⇒
      }
      // TODO set type mappings
    }

    case api.Optimize ⇒ client execute { ES optimize indexName } pipeTo sender

    case api.IndexMany(typeName, docs) ⇒
      val commands = docs map {
        case (id, source) ⇒ {
          val json = JsObject(Seq("id" -> JsString(id), "source" -> source))
          ES.index into s"$indexName/$typeName" doc (JsonDocumentSource(Json.stringify(json)))
        }
      }
      client execute { bulk(commands: _*) }

    case search: SearchDefinition ⇒ execute(client execute search, sender)

    case count: CountDefinition ⇒ {
      val replyTo = sender
      client execute count onComplete {<commit_msg>Specify ID for each documents correctly<commit_after>package org.scalex
package elastic

import com.sksamuel.elastic4s.source.JsonDocumentSource

import scala.concurrent.{ Future }
import scala.util.{ Success, Failure }

import akka.actor._
import akka.pattern.{ pipe }
import com.sksamuel.elastic4s.{ElasticDsl ⇒ ES, CountDefinition, ElasticClient, SearchDefinition}
import com.sksamuel.elastic4s.ElasticDsl._
import com.typesafe.config.Config
import play.api.libs.json.{JsString, Json, JsObject}

private[scalex] final class ElasticActor(
    config: Config,
    indexName: String,
    indexSettings: Map[String, String]) extends Actor with ActorLogging {

  import ElasticActor._

  var client: ElasticClient = _

  override def preStart {
    client = instanciateElasticClient
    self ! EnsureIndex
  }

  override def postStop {
    println("[search] Stop elastic client")
    Option(client) foreach { _.close() }
  }

  def receive = {

    case EnsureIndex ⇒ client execute {
      ES.create index indexName // mappings (typeName as mapping)
    }

    case api.Clear(typeName, mapping) ⇒ Future {
      try {
        client execute { ES.delete from s"$indexName/typeName" where ES.matchall }
      }
      catch {
        case e: org.elasticsearch.indices.TypeMissingException ⇒
      }
      // TODO set type mappings
    }

    case api.Optimize ⇒ client execute { ES optimize indexName } pipeTo sender

    case api.IndexMany(typeName, docs) ⇒
      val commands = docs map {
        case (id, source) ⇒ {
          ES.index into s"$indexName/$typeName" id id doc JsonDocumentSource(Json.stringify(source))
        }
      }
      client execute { bulk(commands: _*) }

    case search: SearchDefinition ⇒ execute(client execute search, sender)

    case count: CountDefinition ⇒ {
      val replyTo = sender
      client execute count onComplete {
{'fraction_non_alphanumeric': 0.09136786188579017, 'fraction_numerical': 0.0015936254980079682, 'mean_word_length': 3.161325966850829, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1356832', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1121, 'n_words': 323}