Commit 3d8b104e authored by zhanglu's avatar zhanglu

帖子搜索

parent a15b85c7
......@@ -4,6 +4,9 @@
import logging
import traceback
import json
from alpha_types.venus import TOPIC_SEARCH_SORT
from libs.es import ESPerform
from .common import TopicDocumentField
from search.utils.common import *
......@@ -464,4 +467,89 @@ class TopicUtils(object):
return result_dict["hits"]
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
\ No newline at end of file
return list()
@classmethod
def process_filters(cls, filters):
"""处理过滤器部分。"""
f = [
{"term": {"is_online": True}},
{"term": {"is_deleted": False}},
]
if not filters:
return f
for k, v in filters.items():
if k == "group_id":
f.append({
"term": {"group_id": v},
})
return f
@classmethod
def process_nfilters(cls, nfilters):
"""处理过滤器部分。"""
nf = []
if not nfilters:
return nf
for k, v in nfilters.items():
pass
return nf
@classmethod
def process_sort(cls, sorts_by):
"""处理排序部分。"""
sort_rule = []
for sort in sorts_by:
if sort == TOPIC_SEARCH_SORT.VOTE_NUM:
sort_rule.append({
"vote_num":{
"order":"desc"
}
})
return sort_rule
@classmethod
def list_topic_ids(cls, filters, nfilters, sorts_by, offset=0, size=10):
q = {
"query": {
"filtered": {
"query": {"match_all":{}},
}
},
"_source": {
"includes":["id"]
},
"sort": [],
}
if filters or nfilters:
f = cls.process_filters(filters)
nf = cls.process_nfilters(nfilters)
q["query"]["filtered"]["filter"] = {
"bool": {
"must": f,
"must_not": nf,
}
}
if sorts_by:
sorts = cls.process_sort(sorts_by)
q["sort"] = sorts
try:
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="topic",
query_body=q, offset=offset, size=size)
return result_dict["hits"]
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
......@@ -254,3 +254,15 @@ def query_topic_by_tag_aggregation(user_id,tag_id, offset, size):
return {"recommend_topic_id": []}
@bind("physical/search/topic")
def topic_search(filters, nfilters=None, sorts_by=None, offset=0, size=10):
"""帖子搜索。"""
try:
result_list = TopicUtils.list_topic_ids(filters=filters, nfilters=nfilters,
sorts_by=sorts_by, offset=offset, size=size)
topic_ids = [item["_source"]["id"] for item in result_list]
return {"topic_ids": topic_ids}
except:
logging.error("catch exception, err_msg:%s" % traceback.format_exc())
return {"topic_ids": []}
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