Skip to content

Instantly share code, notes, and snippets.

@molekilla
Created March 27, 2012 21:47
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 molekilla/2220670 to your computer and use it in GitHub Desktop.
Save molekilla/2220670 to your computer and use it in GitHub Desktop.
Escuelita de Scala - Parte 4 - RegPubServices
package services
import play.api.libs.json._
import play.api.Logger._
object RegPubServices
{
def apply() = new RegPubServices()
}
class RegPubServices extends SearchService with DataModelService {
def searchDocuments(query:String, pagePosition:Int, ops:Boolean):JsValue = {
search(query, pagePosition, isExact=ops) \ "hits"
}
def searchTypeahead(query:String):JsValue = {
val hits = typeahead(query)
val mappedItems = hits.map( hit => (hit \\ "fields").map( h => Map("item" -> h \ "nombre"))).flatten
Json.toJson(mappedItems)
}
def findByName(query:String):JsValue = {
val hits = typeahead(query)
val mappedItems = hits.map( hit => (hit \\ "fields").map( h => h \ "ficha")).flatten
try {
val ficha = mappedItems.head.asOpt[String]
// look in data service
val document = this.getDocumentByFicha(ficha.getOrElse(""))
val jsonDoc = Json.parse(document)
val directores = (jsonDoc \ "directores").as[List[String]]
val subscriptores = (jsonDoc \ "subscriptores").as[List[String]]
val items = (directores ++ subscriptores).distinct
val rels = relationships(query, items)
val result = Map("document" -> jsonDoc, "stats" -> rels)
Json.toJson(result)
}
catch {
case e: Exception =>
Json.parse("{}")
}
}
}
object Config {
import org.elasticsearch.common.transport.InetSocketTransportAddress
val elasticSearchAddress = new InetSocketTransportAddress("localhost",9300)
val regpubDb = "webdata"
val regpubColl = "regpub"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment