Created
March 28, 2012 17:17
-
-
Save molekilla/2228366 to your computer and use it in GitHub Desktop.
Escuelita de Scala - Parte 4a - Typeahead
This file contains 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
package services | |
import play.api.libs.json._ | |
import org.elasticsearch.action.index._ | |
import org.elasticsearch.common.settings._ | |
import org.elasticsearch.client.transport._ | |
import org.elasticsearch.common.transport.InetSocketTransportAddress | |
import org.elasticsearch.action.search._ | |
import org.elasticsearch.index.query._ | |
import org.elasticsearch.search.sort._ | |
import org.elasticsearch.common.xcontent._ | |
import org.elasticsearch.search._ | |
import org.elasticsearch.node.{Node,NodeBuilder} | |
import org.elasticsearch.index.query.FilterBuilders._ | |
import org.elasticsearch.index.query._ | |
import org.elasticsearch.search.facet._ | |
abstract trait SearchUtil { | |
protected def search(query:String, pagePosition:Int, isExact:Boolean):JsValue | |
protected def typeahead(query:String):Seq[JsValue] | |
protected def relationships(query:String, directores:Seq[String]):JsValue | |
} | |
trait SearchService extends SearchUtil { | |
import NodeBuilder.nodeBuilder | |
val settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build() | |
val regPubIndex = "regpub" | |
/** Finds 10 matches and returns an array of company names | |
* @param query Query text to look up | |
* */ | |
protected def typeahead(query:String):Seq[JsValue] = { | |
val client = new TransportClient(settings) | |
.addTransportAddress(Config.elasticSearchAddress) | |
val queryTerm = QueryBuilders.queryString(query) | |
.useDisMax(true) | |
.autoGeneratePhraseQueries(true) | |
.field("nombre", 1) | |
.field("dignatarios", 0.8f) | |
.field("subscriptores",0.7f) | |
.defaultOperator(QueryStringQueryBuilder.Operator.AND) | |
val builder = client.prepareSearch(regPubIndex) | |
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH) | |
.setQuery(queryTerm) | |
.addField("nombre") | |
.addField("ficha") | |
.addField("directores") | |
val response = builder.setFrom(0).setSize(10).execute().actionGet() | |
client.close | |
Json.parse(response.toString) \ "hits" \\ "hits" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment