Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2014 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/cb60c8b5ad9d777c31ec to your computer and use it in GitHub Desktop.
Save anonymous/cb60c8b5ad9d777c31ec to your computer and use it in GitHub Desktop.
import scalastic.elasticsearch._
import play.api._
// my Play! scala application has a global object which defines the indexer
object Global extends GlobalSettings {
val globIndexer = Indexer.local
override def onStart(app: Application) {
Logger.info("starting ES indexer")
globIndexer.start
}
override def onStop(app: Application) {
Logger.info("shutting down ES indexer")
globIndexer.stop()
}
}
// package where indexing actually happens, import the indexer from global
val indexer = Global.globIndexer
//indexer.deleteIndex(Seq(indexName))
logger.debug("trying to make a new index with name %s".format(indexName))
try {
indexer.createIndex(indexName, settings = Map("number_of_shards" -> "1","number_of_replicas" -> "0"))
} catch {
case e: Exception => logger.debug(e.toString()) //catches case where index already exists
}
// search Query - fails unless I uncomment deleteIndex above
indexer.waitForGreenStatus(Seq(indexName))
logger.debug("achieved green status, trying search")
val totresult = indexer.search(indices=Seq(indexName),types= Seq(indexTypeMsg,query=matchAllQuery())
// indexing operations
indexer.index(indexNameUser, indexTypeMsg, id, Json.stringify(msgJson))
@DrewEaster
Copy link

I'm not physically in a position to run through this code right now. I'm slightly confused, if you are hitting the exception where the index already exists, then that would suggest that it's okay to use a previously created index. I can't see why the search would fail in this scenario - what is the error exactly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment