Created
December 25, 2014 11:43
-
-
Save fbettag/c2b334c91f907f91eaf2 to your computer and use it in GitHub Desktop.
Foursquare Slashem trait to save records to SOLR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.foursquare.slashem.SolrSchema | |
import com.twitter.finagle.http.RequestBuilder | |
import com.twitter.util.Awaitable | |
import net.liftweb.json._ | |
import org.jboss.netty.buffer.ChannelBuffers._ | |
import org.jboss.netty.handler.codec.http.{ HttpHeaders, HttpRequest, HttpResponse } | |
/** | |
* Trait to export Lift Record objects to SOLR | |
* @tparam T SolrSchema | |
*/ | |
trait RecordToSolr[T <: SolrSchema[T]] { this: SolrSchema[T] => | |
/* Need a more crazy query path? override */ | |
lazy val updatePath: String = meta.core match { | |
case None => "/solr/update/" | |
case Some(x) => "/solr/%s/update".format(x) | |
} | |
/** | |
* Generates the JSON which is being saved to SOLR. | |
* Defaults to asJValue | |
* @return JValue being saved into SOLR | |
*/ | |
def asJValueForSolr: JValue = this.asJValue | |
/** | |
* Save this record to SOLR | |
* @return Future HTTP Response | |
*/ | |
def saveToSolr(): Awaitable[HttpResponse] = { | |
val json = Serialization.write(List(asJValueForSolr)).getBytes("UTF-8") | |
val request: HttpRequest = RequestBuilder() | |
.url("http://%s%s".format(meta.servers.head, updatePath)) | |
.setHeader(HttpHeaders.Names.HOST, meta.servers.head) | |
.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json") | |
.setHeader(HttpHeaders.Names.CONTENT_LENGTH, json.length.toString) | |
.buildPost(wrappedBuffer(json)) | |
meta.client(request) | |
} | |
/** | |
* Save a collection of records to SOLR | |
* @return Future HTTP Response | |
*/ | |
def saveToSolr(list: List[T with RecordToSolr[T]]): Awaitable[HttpResponse] = { | |
val json = Serialization.write(list.map(_.asJValueForSolr)).getBytes("UTF-8") | |
val request: HttpRequest = RequestBuilder() | |
.url("http://%s%s".format(meta.servers.head, updatePath)) | |
.setHeader(HttpHeaders.Names.HOST, meta.servers.head) | |
.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json") | |
.setHeader(HttpHeaders.Names.CONTENT_LENGTH, json.length.toString) | |
.buildPost(wrappedBuffer(json)) | |
meta.client(request) | |
} | |
/** | |
* Commit changes on this core/collection to SOLR | |
* @return Future HTTP Response | |
*/ | |
def commitToSolr(): Awaitable[HttpResponse] = { | |
val request: HttpRequest = RequestBuilder() | |
.url("http://%s%s".format(meta.servers.head, updatePath + "?commit=true")) | |
.setHeader(HttpHeaders.Names.HOST, meta.servers.head) | |
.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json") | |
.buildGet() | |
meta.client(request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment