Commit 6191d68a authored by 段英荣's avatar 段英荣

Merge branch 'dev' into 'master'

Dev

See merge request alpha/physical!67
parents 9e612ec9 b63d9b61
......@@ -167,7 +167,7 @@ class ESPerform(object):
@classmethod
def get_search_results(cls, es_cli,sub_index_name,query_body,offset=0,size=10,
auto_create_index=False,doc_type="_doc",aggregations_query=False):
auto_create_index=False,doc_type="_doc",aggregations_query=False,is_suggest_request=False):
try:
assert (es_cli is not None)
......@@ -183,14 +183,17 @@ class ESPerform(object):
logging.info("duan add,query_body:%s" % str(query_body).encode("utf-8"))
res = es_cli.search(index=official_index_name,doc_type=doc_type,body=query_body,from_=offset,size=size)
result_dict = {
"total_count":res["hits"]["total"],
"hits":res["hits"]["hits"]
}
if aggregations_query:
result_dict["aggregations"] = res["aggregations"]
return result_dict
if is_suggest_request:
return res
else:
result_dict = {
"total_count":res["hits"]["total"],
"hits":res["hits"]["hits"]
}
if aggregations_query:
result_dict["aggregations"] = res["aggregations"]
return result_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"total_count":0,"hits":[]}
\ No newline at end of file
......@@ -24,6 +24,7 @@ def get_highlight(fields=[]):
@bind("physical/search/query_tag")
def query_tag(query,offset,size):
try:
"""
q = {
"query":{
"bool":{
......@@ -48,17 +49,40 @@ def query_tag(query,offset,size):
"include": ["id", "name"]
}
}
q["highlight"] = get_highlight(["name"])
q["highlight"] = get_highlight(["name"])
"""
q = {
"suggest":{
"tips-suggest":{
"prefix":query,
"completion":{
"field":"suggest",
"contexts":{
"is_online": [True],
"is_deleted": [False]
}
}
}
},
"sort": [
{"near_new_topic_num": {"order": "desc"}}
],
"_source": {
"include": ["id", "name", "is_deleted", "is_online"]
}
}
ret_list = list()
result_dict = ESPerform.get_search_results(ESPerform.get_cli(),sub_index_name="tag",query_body=q,offset=offset,size=size)
for hit_item in result_dict["hits"]:
if "name" in hit_item["highlight"] and len(hit_item["highlight"]["name"])>0:
hit_item["_source"]["highlight"] = hit_item["highlight"]["name"][0]
else:
hit_item["_source"]["highlight"] = ""
ret_list.append(hit_item["_source"])
result_dict = ESPerform.get_search_results(ESPerform.get_cli(),sub_index_name="tag",query_body=q,offset=offset,size=size,is_suggest_request=True)
for tips_item in result_dict["suggest"]["tips-suggest"]:
for hit_item in tips_item["options"]:
#if len(hit_item["contexts"])==2:
if hit_item["_source"]["is_deleted"]==False and hit_item["_source"]["is_online"]==True:
hitLight = u'<ems>%s</ems>' % query
hit_item["_source"]["highlight"] = hit_item["_source"]["name"].replace(query,hitLight)
ret_list.append(hit_item["_source"])
return {"tag_list": ret_list}
except:
......
......@@ -2,11 +2,25 @@
"dynamic":"strict",
"properties": {
"id":{"type":"long"},
"suggest":{"type":"completion"},
"suggest":{
"type":"completion",
"contexts":[
{
"name":"is_online",
"type": "category",
"path": "is_online"
},
{
"name":"is_deleted",
"type": "category",
"path": "is_deleted"
}
]
},
"name":{"type":"text","analyzer":"gm_default_index","search_analyzer":"gm_default_index"},
"tag_type":{"type":"long"},
"is_online":{"type":"boolean"},//上线
"is_deleted":{"type":"boolean"},
"is_online":{"type":"keyword"},//上线
"is_deleted":{"type":"keyword"},
"near_new_topic_num":{"type":"long","store": true}
}
}
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, print_function
import os
import sys
import logging
......@@ -17,7 +19,21 @@ class TagTransfer(object):
res = dict()
res["id"] = instance.id
res["suggest"] = instance.name
tag_name_terms_list = list()
for i in range(len(instance.name)):
for j in range(i,len(instance.name)+1):
name_term = instance.name[i:j].strip()
if name_term:
tag_name_terms_list.append(name_term)
res["suggest"] = {
"input":tag_name_terms_list,
"contexts":{
"is_online": [instance.is_online],
"is_deleted": [instance.is_deleted]
}
}
res["name"] = instance.name
res["is_online"] = instance.is_online
res["is_deleted"] = instance.is_deleted
......
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