Commit 53d05f07 authored by zhanglu's avatar zhanglu

Merge branch 'feature/searth_topic' into 'master'

Feature/searth topic

See merge request alpha/physical!90
parents 143d8b6f 0dc34b1f
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*~
# C extensions
*.so
# Distribution / packaging
.Python
.vscode
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
.idea/
*.egg-info/
.installed.cfg
*.egg
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Translations
*.mo
# Mr Developer
.mr.developer.cfg
.project
.pydevproject
# Rope
.ropeproject
# Django stuff:
*.log
*.pot
# Sphinx documentation
docs/_build/
# config
fabfile.py
settings.online.py
settings_local.py
media/
log/
crawldata/
conf/
/static
.vagrant/
Vagrantfile
*.DS_Store
dump.rdb
......@@ -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,80 @@ 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": {
"bool": {
"must": cls.process_filters(filters),
"must_not": cls.process_nfilters(nfilters),
}
},
"_source": {
"includes":["id"]
},
"sort": [],
}
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