Commit f7e12461 authored by 赵威's avatar 赵威

search diary

parent 99ed8914
...@@ -30,8 +30,8 @@ class UserInfoDeserializationSchema extends KafkaDeserializationSchema[UserInfo] ...@@ -30,8 +30,8 @@ class UserInfoDeserializationSchema extends KafkaDeserializationSchema[UserInfo]
object Main { object Main {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
// println("###############") // println("###############")
// val query = ES.generateTractateQuery(List("光子嫩肤", "水光针"), List("丰胸", "胸部塑身")) // val query = ES.generateDiaryQuery(List("光子嫩肤", "水光针"), List("丰胸", "胸部塑身"), -1)
// println(query) // query
// println("###############") // println("###############")
val env = StreamExecutionEnvironment.getExecutionEnvironment val env = StreamExecutionEnvironment.getExecutionEnvironment
......
...@@ -2,24 +2,32 @@ package com.gmei.up.utils ...@@ -2,24 +2,32 @@ package com.gmei.up.utils
import scala.collection.mutable.ListBuffer import scala.collection.mutable.ListBuffer
import scala.util.Try import scala.util.Try
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.http.JavaClient import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import org.json4s.JObject
import org.apache.http.HttpHost import org.apache.http.HttpHost
import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials } import org.apache.http.auth.{ AuthScope, UsernamePasswordCredentials }
import org.apache.http.impl.client.BasicCredentialsProvider import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback
import com.sksamuel.elastic4s.requests.searches.{ SearchRequest, SearchResponse } import com.sksamuel.elastic4s.requests.searches.{ SearchRequest, SearchResponse }
import com.sksamuel.elastic4s.requests.searches.sort.{ FieldSort, SortOrder, ScriptSortType }
import com.sksamuel.elastic4s.ElasticDsl._ import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.{ HitReader, Hit } import com.sksamuel.elastic4s.{ HitReader, Hit, ElasticClient }
import org.json4s.JsonDSL._ import com.sksamuel.elastic4s.http.JavaClient
import org.json4s.jackson.JsonMethods._ import com.sksamuel.elastic4s.requests.searches.queries.Query
import org.json4s.JObject import com.sksamuel.elastic4s.requests.searches.queries.term.TermsQuery
// import org.json4s.jackson.Serialization.write import com.sksamuel.elastic4s.requests.script.{ Script, ScriptType }
// import scala.concurrent._ import com.sksamuel.elastic4s.requests.searches.queries.funcscorer.{
// import scala.concurrent.duration._ CombineFunction,
// import ExecutionContext.Implicits.global FunctionScoreQueryScoreMode,
WeightScore,
ScoreFunction
}
object ESClient { object ESClient {
private class ElasticCredentials(username: String, password: String) extends HttpClientConfigCallback { private class ElasticCredentials(username: String, password: String) extends HttpClientConfigCallback {
...@@ -51,12 +59,12 @@ object ES { ...@@ -51,12 +59,12 @@ object ES {
} }
} }
def test(diaryQuery: String, tractateQuery: String) = { def test(diaryQuery: SearchRequest, tractateQuery: String) = {
// TODO read from config // TODO read from config
val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#") val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#")
val bb = multi( val bb = multi(
search("gm-dbmw-diary-read").source(s"""${diaryQuery}"""), diaryQuery,
search("gm-dbmw-tractate-read").source(tractateQuery) search("gm-dbmw-tractate-read").source(tractateQuery)
) )
...@@ -81,110 +89,69 @@ object ES { ...@@ -81,110 +89,69 @@ object ES {
resp resp
} }
// SearchRequest def generateDiaryQuery(
def generateDiaryQuery(projects: List[String], secondDemands: List[String], cityId: Int = -1): String = { projects: List[String],
val size = 30 secondDemands: List[String],
cityId: Int = -1
): SearchRequest = {
val size = 15
val includes = List("id", "tags_v3") val includes = List("id", "tags_v3")
var scoreListBuffer: ListBuffer[ScoreFunction] = ListBuffer.empty[ScoreFunction]
var shouldListBuffer: ListBuffer[Query] = ListBuffer.empty[Query]
// TODO read
val functions = new ListBuffer[JObject]()
val shoulds = new ListBuffer[JObject]()
if (projects.size > 0) { if (projects.size > 0) {
val promotes: List[JObject] = scoreListBuffer += WeightScore(60).filter(
List(("terms" -> ("tags_v3" -> projects)), ("term" -> ("is_promote" -> true))) must(termsQuery("tags_v3", projects), termQuery("is_promote", true))
)
functions += ("filter" -> ("bool" -> ("must" -> promotes))) ~ ("weight" -> 60) scoreListBuffer += WeightScore(50).filter(termsQuery("tags_v3", projects))
shouldListBuffer += TermsQuery("tags_v3", projects)
functions += ("filter" -> ("terms" -> { "tags_v3" -> projects })) ~ ("weight" -> 50)
shoulds += ("terms" -> ("tags_v3" -> projects))
} }
if (secondDemands.size > 0) { if (secondDemands.size > 0) {
functions += ("filter" -> ("terms" -> ("second_demands" -> secondDemands))) ~ ("weight" -> 40) scoreListBuffer += WeightScore(40).filter(termsQuery("second_demands", secondDemands))
shouldListBuffer += TermsQuery("second_demands", secondDemands)
shoulds += ("terms" -> ("second_demands" -> secondDemands))
} }
val query = s""" val res = search("gm-dbmw-diary-read")
{ .query {
"query": { functionScoreQuery()
"function_score": { .functions(scoreListBuffer.toList)
"functions": ${pretty(render(functions.toList))}, .scoreMode(FunctionScoreQueryScoreMode.Max)
"boost_mode": "replace", .boostMode(CombineFunction.Replace)
"score_mode": "max", .query {
"query": { boolQuery()
"bool": { .must(
"should": ${pretty(render(shoulds.toList))}, termQuery("is_online", true),
"minimum_should_match": 1, termQuery("has_cover", true),
"must": [ termQuery("is_sink", false),
{ termQuery("has_after_cover", true),
"term": { termQuery("has_before_cover", true),
"is_online": "true" rangeQuery("content_level").gte(3),
} termQuery("content_simi_bol_show", 0)
}, )
{ .not(termQuery("show_by_index", 2))
"term": { .should(shouldListBuffer.toList)
"has_cover": "true" .minimumShouldMatch(1)
} }
},
{
"term": {
"is_sink": "false"
}
},
{
"term": {
"has_after_cover": "true"
}
},
{
"term": {
"has_before_cover": "true"
}
},
{
"terms": {
"content_level": [6, 5, 4, 3.5, 3]
}
},
{
"term": {
"content_simi_bol_show": 0
}
}
],
"must_not": [{ "term": { "show_by_index": 2 } }]
}
}
}
},
"size": ${size},
"_source": {"include": ${pretty(render(includes))}},
"sort": [
{
"_script": {
"type": "number",
"script": {
"id": "diary-recommend",
"params": { "user_city_tag_id": ${cityId} }
},
"order": "desc"
} }
}, .size(size)
{ "_score": { "order": "desc" } }, .sourceInclude(includes)
{ "has_video_cover": { "order": "asc" } }, .sortBy(
{ "offline_score": { "order": "desc" } }, scriptSort(
{ "good_click": { "order": "desc" } }, script(
{ "last_update_time": { "order": "desc" } } "if (doc['doctor.hospital.city_tag_id'].size()!=0){if (doc['doctor.hospital.city_tag_id'].value ==params.user_city_tag_id){return 2;} else if (doc['nearby_city_tags_v1.tag_id'].contains(params.user_city_tag_id) ) {return 1;} else {return 0;} }else{return 0}"
] ).params(Map("user_city_tag_id" -> cityId)).lang("painless")
} ).typed("number").order(SortOrder.Desc),
""".stripMargin FieldSort("_score").desc(),
FieldSort("has_video_cover").asc(),
// println("diary query:") FieldSort("offline_score").desc(),
// println(query) FieldSort("good_click").desc(),
query FieldSort("last_update_time").desc()
)
println("diary query")
println(res.show)
res
} }
def generateTractateQuery(projects: List[String], secondDemands: List[String]): String = { def generateTractateQuery(projects: List[String], secondDemands: List[String]): String = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment