Commit 17140e3b authored by 赵威's avatar 赵威

try multi search

parent 41acdc4e
...@@ -29,6 +29,11 @@ class UserInfoDeserializationSchema extends KafkaDeserializationSchema[UserInfo] ...@@ -29,6 +29,11 @@ class UserInfoDeserializationSchema extends KafkaDeserializationSchema[UserInfo]
object Main { object Main {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
// println("###############")
// val query = ES.generateTractateQuery(List("光子嫩肤", "水光针"), List("丰胸", "胸部塑身"))
// println(query)
// println("###############")
val env = StreamExecutionEnvironment.getExecutionEnvironment val env = StreamExecutionEnvironment.getExecutionEnvironment
// TODO read from config // TODO read from config
...@@ -47,17 +52,14 @@ object Main { ...@@ -47,17 +52,14 @@ object Main {
val stream: DataStream[UserInfo] = env.addSource(kafkaConsumer) val stream: DataStream[UserInfo] = env.addSource(kafkaConsumer)
// println("###############")
// ES.generateDiaryQuery(-1, List("光子嫩肤", "水光针"), List("丰胸", "胸部塑身"))
// println("###############")
stream.map { user => stream.map { user =>
println(user.deviceId) println(user.deviceId)
val projects = user.projects.toList val projects = user.projects.toList
val secondDemands = user.secondDemands.toList val secondDemands = user.secondDemands.toList
if ((projects.size > 0) && (secondDemands.size > 0)) { if ((projects.size > 0) || (secondDemands.size > 0)) {
val query = ES.generateDiaryQuery(-1, projects, secondDemands) val diaryQuery = ES.generateDiaryQuery(projects, secondDemands, -1)
ES.test(query) val tractateQuery = ES.generateTractateQuery(projects, secondDemands)
ES.test(diaryQuery, tractateQuery)
} }
println("########################") println("########################")
} }
......
...@@ -40,39 +40,29 @@ object ESClient { ...@@ -40,39 +40,29 @@ object ESClient {
} }
object ES { object ES {
def test(query: String) = { def test(diaryQuery: String, tractateQuery: String) = {
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 json = """
{
"query": {
"bool": {
"filter": [
{
"terms": { "tags_v3": ["光子嫩肤", "水光针"] }
}
]
}
}
}
""";
val resp = client.execute { val resp = client
search("gm-dbmw-diary-read").source(query) .execute(
}.await multi(
search("gm-dbmw-diary-read").source(diaryQuery),
search("gm-dbmw-tractate-read").source(tractateQuery)
)
)
.await
println(resp) println(resp)
resp resp
} }
// SearchRequest // SearchRequest
def generateDiaryQuery( def generateDiaryQuery(projects: List[String], secondDemands: List[String], cityId: Int = -1): String = {
cityId: Int = -1, val size = 10
projects: List[String] = List.empty[String],
secondDemands: List[String] = List.empty[String]
): String = {
val size = 100
val includes = List("id", "tags_v3") val includes = List("id", "tags_v3")
// TODO read
val functions = new ListBuffer[JObject]() val functions = new ListBuffer[JObject]()
val shoulds = new ListBuffer[JObject]() val shoulds = new ListBuffer[JObject]()
if (projects.size > 0) { if (projects.size > 0) {
...@@ -92,7 +82,7 @@ object ES { ...@@ -92,7 +82,7 @@ object ES {
shoulds += ("terms" -> ("second_demands" -> secondDemands)) shoulds += ("terms" -> ("second_demands" -> secondDemands))
} }
val res = s""" val query = s"""
{ {
"query": { "query": {
"function_score": { "function_score": {
...@@ -167,8 +157,71 @@ object ES { ...@@ -167,8 +157,71 @@ object ES {
} }
""" """
// println("diary query\n") // println("diary query:")
// println(res) // println(query)
res query
}
def generateTractateQuery(projects: List[String], secondDemands: List[String]): String = {
val size = 10
val includes = List("id", "tags_v3")
val functions = new ListBuffer[JObject]()
val shoulds = new ListBuffer[JObject]()
if (projects.size > 0) {
val promotes: List[JObject] =
List(("terms" -> ("tags_v3" -> projects)), ("term" -> ("is_promote" -> true)))
functions += ("filter" -> ("bool" -> ("must" -> promotes))) ~ ("weight" -> 60)
functions += ("filter" -> ("terms" -> { "tags_v3" -> projects })) ~ ("weight" -> 50)
shoulds += ("terms" -> ("tags_v3" -> projects))
}
if (secondDemands.size > 0) {
functions += ("filter" -> ("terms" -> ("second_demands" -> secondDemands))) ~ ("weight" -> 40)
shoulds += ("terms" -> ("second_demands" -> secondDemands))
}
val query = s"""
{
"query": {
"function_score": {
"functions": ${pretty(render(functions.toList))},
"boost_mode": "replace",
"score_mode": "max",
"query": {
"bool": {
"should": ${pretty(render(shoulds.toList))},
"minimum_should_match": 1,
"must": [
{
"term": {
"is_online": "true"
}
},
{
"terms": {
"content_level": [6, 5, 4, 3.5, 3]
}
}
],
"must_not": [
{ "term": { "status": 4 } },
{ "term": { "show_by_index": 2 } }
]
}
}
}
},
"size": ${size},
"_source": ${pretty(render(includes))}
}
"""
// println("tractate query:")
// println(query)
query
} }
} }
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