Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
search_tips
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rank
search_tips
Commits
f500ae28
Commit
f500ae28
authored
Jun 04, 2019
by
lixiaofang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add
parent
f3f39396
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
73 deletions
+75
-73
es.py
libs/es.py
+31
-31
auto_tips.py
search/utils/auto_tips.py
+25
-21
settings_local.py
search_tips/settings_local.py
+11
-14
hospital_transfer.py
trans2es/utils/hospital_transfer.py
+8
-7
No files found.
libs/es.py
View file @
f500ae28
...
...
@@ -17,9 +17,8 @@ class ESPerform(object):
cli_info_list
=
settings
.
ES_INFO_LIST
index_prefix
=
settings
.
ES_INDEX_PREFIX
@classmethod
def
get_cli
(
cls
,
es_ip_list
=
None
):
def
get_cli
(
cls
,
es_ip_list
=
None
):
try
:
es_info_list
=
es_ip_list
if
es_ip_list
else
cls
.
cli_info_list
# if es_ip_list:#tricky的做法,兼容测试es
...
...
@@ -32,7 +31,7 @@ class ESPerform(object):
return
None
@classmethod
def
get_official_index_name
(
cls
,
sub_index_name
,
index_flag
=
None
):
def
get_official_index_name
(
cls
,
sub_index_name
,
index_flag
=
None
):
"""
:remark:get official es index name
:param sub_index_name:
...
...
@@ -40,7 +39,7 @@ class ESPerform(object):
:return:
"""
try
:
assert
(
index_flag
in
[
None
,
"read"
,
"write"
])
assert
(
index_flag
in
[
None
,
"read"
,
"write"
])
official_index_name
=
cls
.
index_prefix
+
"-"
+
sub_index_name
if
index_flag
:
...
...
@@ -52,11 +51,11 @@ class ESPerform(object):
return
None
@classmethod
def
__load_mapping
(
cls
,
doc_type
):
def
__load_mapping
(
cls
,
doc_type
):
try
:
mapping_file_path
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'..'
,
'trans2es'
,
'mapping'
,
'
%
s.json'
%
(
doc_type
,))
'..'
,
'trans2es'
,
'mapping'
,
'
%
s.json'
%
(
doc_type
,))
mapping
=
''
with
open
(
mapping_file_path
,
'r'
)
as
f
:
for
line
in
f
:
...
...
@@ -69,7 +68,7 @@ class ESPerform(object):
return
None
@classmethod
def
create_index
(
cls
,
es_cli
,
sub_index_name
):
def
create_index
(
cls
,
es_cli
,
sub_index_name
):
"""
:remark: create es index,alias index
:param sub_index_name:
...
...
@@ -82,11 +81,11 @@ class ESPerform(object):
index_exist
=
es_cli
.
indices
.
exists
(
official_index_name
)
if
not
index_exist
:
es_cli
.
indices
.
create
(
official_index_name
)
read_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"read"
)
es_cli
.
indices
.
put_alias
(
official_index_name
,
read_alias_name
)
read_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"read"
)
es_cli
.
indices
.
put_alias
(
official_index_name
,
read_alias_name
)
write_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"write"
)
es_cli
.
indices
.
put_alias
(
official_index_name
,
write_alias_name
)
write_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"write"
)
es_cli
.
indices
.
put_alias
(
official_index_name
,
write_alias_name
)
return
True
except
:
...
...
@@ -94,7 +93,7 @@ class ESPerform(object):
return
False
@classmethod
def
put_index_mapping
(
cls
,
es_cli
,
sub_index_name
,
mapping_type
=
"_doc"
):
def
put_index_mapping
(
cls
,
es_cli
,
sub_index_name
,
mapping_type
=
"_doc"
):
"""
:remark: put index mapping
:param es_cli:
...
...
@@ -105,13 +104,13 @@ class ESPerform(object):
try
:
assert
(
es_cli
is
not
None
)
write_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"write"
)
write_alias_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"write"
)
index_exist
=
es_cli
.
indices
.
exists
(
write_alias_name
)
if
not
index_exist
:
return
False
mapping_dict
=
cls
.
__load_mapping
(
sub_index_name
)
es_cli
.
indices
.
put_mapping
(
index
=
write_alias_name
,
body
=
mapping_dict
,
doc_type
=
mapping_type
)
es_cli
.
indices
.
put_mapping
(
index
=
write_alias_name
,
body
=
mapping_dict
,
doc_type
=
mapping_type
)
return
True
except
:
...
...
@@ -119,7 +118,7 @@ class ESPerform(object):
return
False
@classmethod
def
put_indices_template
(
cls
,
es_cli
,
template_file_name
,
template_name
):
def
put_indices_template
(
cls
,
es_cli
,
template_file_name
,
template_name
):
"""
:remark put index template
:param es_cli:
...
...
@@ -131,7 +130,7 @@ class ESPerform(object):
assert
(
es_cli
is
not
None
)
mapping_dict
=
cls
.
__load_mapping
(
template_file_name
)
es_cli
.
indices
.
put_template
(
name
=
template_name
,
body
=
mapping_dict
)
es_cli
.
indices
.
put_template
(
name
=
template_name
,
body
=
mapping_dict
)
return
True
except
:
...
...
@@ -139,7 +138,7 @@ class ESPerform(object):
return
False
@classmethod
def
es_helpers_bulk
(
cls
,
es_cli
,
data_list
,
sub_index_name
,
auto_create_index
=
False
,
doc_type
=
"_doc"
):
def
es_helpers_bulk
(
cls
,
es_cli
,
data_list
,
sub_index_name
,
auto_create_index
=
False
,
doc_type
=
"_doc"
):
try
:
assert
(
es_cli
is
not
None
)
...
...
@@ -150,8 +149,8 @@ class ESPerform(object):
logging
.
error
(
"index:
%
s is not existing,bulk data error!"
%
official_index_name
)
return
False
else
:
cls
.
create_index
(
es_cli
,
sub_index_name
)
cls
.
put_index_mapping
(
es_cli
,
sub_index_name
)
cls
.
create_index
(
es_cli
,
sub_index_name
)
cls
.
put_index_mapping
(
es_cli
,
sub_index_name
)
bulk_actions
=
[]
for
data
in
data_list
:
...
...
@@ -162,7 +161,7 @@ class ESPerform(object):
'_id'
:
data
[
'id'
],
'_source'
:
data
,
})
elasticsearch
.
helpers
.
bulk
(
es_cli
,
bulk_actions
)
elasticsearch
.
helpers
.
bulk
(
es_cli
,
bulk_actions
)
return
True
except
:
...
...
@@ -170,41 +169,43 @@ class ESPerform(object):
return
False
@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
,
is_suggest_request
=
False
,
batch_search
=
False
):
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
,
is_suggest_request
=
False
,
batch_search
=
False
):
try
:
assert
(
es_cli
is
not
None
)
official_index_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"read"
)
official_index_name
=
cls
.
get_official_index_name
(
sub_index_name
,
"read"
)
index_exists
=
es_cli
.
indices
.
exists
(
official_index_name
)
if
not
index_exists
:
if
not
auto_create_index
:
logging
.
error
(
"index:
%
s is not existing,get_search_results error!"
%
official_index_name
)
return
None
else
:
cls
.
create_index
(
es_cli
,
sub_index_name
)
cls
.
put_index_mapping
(
es_cli
,
sub_index_name
)
cls
.
create_index
(
es_cli
,
sub_index_name
)
cls
.
put_index_mapping
(
es_cli
,
sub_index_name
)
logging
.
info
(
"duan add,query_body:
%
s"
%
str
(
query_body
)
.
encode
(
"utf-8"
))
if
not
batch_search
:
res
=
es_cli
.
search
(
index
=
official_index_name
,
doc_type
=
doc_type
,
body
=
query_body
,
from_
=
offset
,
size
=
size
)
res
=
es_cli
.
search
(
index
=
official_index_name
,
doc_type
=
doc_type
,
body
=
query_body
,
from_
=
offset
,
size
=
size
)
if
is_suggest_request
:
return
res
else
:
result_dict
=
{
"total_count"
:
res
[
"hits"
][
"total"
],
"hits"
:
res
[
"hits"
][
"hits"
]
"total_count"
:
res
[
"hits"
][
"total"
],
"hits"
:
res
[
"hits"
][
"hits"
]
}
if
aggregations_query
:
result_dict
[
"aggregations"
]
=
res
[
"aggregations"
]
return
result_dict
else
:
res
=
es_cli
.
msearch
(
body
=
query_body
,
index
=
official_index_name
,
doc_type
=
doc_type
)
res
=
es_cli
.
msearch
(
body
=
query_body
,
index
=
official_index_name
,
doc_type
=
doc_type
)
logging
.
info
(
"duan add,msearch res:
%
s"
%
str
(
res
))
return
res
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
{
"total_count"
:
0
,
"hits"
:[]}
\ No newline at end of file
return
{
"total_count"
:
0
,
"hits"
:
[]}
search/utils/auto_tips.py
View file @
f500ae28
...
...
@@ -12,53 +12,56 @@ from libs.tools import g_hospital_pos_dict
from
libs.tools
import
point_distance
def
get_suggest_tips
(
query
,
lat
,
lng
,
offset
=
0
,
size
=
50
):
def
get_suggest_tips
(
query
,
lat
,
lng
,
offset
=
0
,
size
=
50
):
try
:
# ios输入法在某些情况下会携带\\u2006
query
=
query
.
replace
(
"
\u2006
"
,
''
)
q
=
{
"suggest"
:{
"tips-suggest"
:
{
"prefix"
:
query
,
"completion"
:
{
"field"
:
"suggest"
,
"size"
:
size
,
"contexts"
:{
"is_online"
:[
True
]
},
"fuzzy"
:{
"fuzziness"
:
0
}
}
"suggest"
:
{
"tips-suggest"
:
{
"prefix"
:
query
,
"completion"
:
{
"field"
:
"suggest"
,
"size"
:
size
,
"contexts"
:
{
"is_online"
:
[
True
]
},
"fuzzy"
:
{
"fuzziness"
:
0
}
}
}
},
"_source"
:
{
"includes"
:
[
"id"
,
"ori_name"
,
"offline_score"
,
"is_online"
,
"type_flag"
,
"results_num"
]
"includes"
:
[
"id"
,
"ori_name"
,
"offline_score"
,
"is_online"
,
"type_flag"
,
"results_num"
]
}
}
have_read_tips_set
=
set
()
ret_list
=
list
()
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
"suggest"
,
query_body
=
q
,
offset
=
offset
,
size
=
size
,
is_suggest_request
=
True
)
result_dict
=
ESPerform
.
get_search_results
(
ESPerform
.
get_cli
(),
sub_index_name
=
"suggest"
,
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
hit_item
[
"_source"
][
"ori_name"
]
not
in
have_read_tips_set
:
have_read_tips_set
.
add
(
hit_item
[
"_source"
][
"ori_name"
])
highlight_marks
=
u'<ems>
%
s</ems>'
%
query
hit_item
[
"_source"
][
"highlight_name"
]
=
hit_item
[
"_source"
][
"ori_name"
]
.
replace
(
query
,
highlight_marks
)
hit_item
[
"_source"
][
"highlight_name"
]
=
hit_item
[
"_source"
][
"ori_name"
]
.
replace
(
query
,
highlight_marks
)
if
hit_item
[
"_source"
][
"type_flag"
]
==
"hospital"
:
if
hit_item
[
"_source"
][
"ori_name"
]
in
g_hospital_pos_dict
:
distance
=
point_distance
(
lng
,
lat
,
g_hospital_pos_dict
[
hit_item
[
"_source"
][
"ori_name"
]][
0
],
g_hospital_pos_dict
[
hit_item
[
"_source"
][
"ori_name"
]][
1
])
if
distance
<
1000
*
50
:
distance
=
point_distance
(
lng
,
lat
,
g_hospital_pos_dict
[
hit_item
[
"_source"
][
"ori_name"
]][
0
],
g_hospital_pos_dict
[
hit_item
[
"_source"
][
"ori_name"
]][
1
])
if
distance
<
1000
*
50
:
if
distance
<
1000
:
if
distance
<
100
:
hit_item
[
"_source"
][
"describe"
]
=
"<100"
+
"米"
else
:
hit_item
[
"_source"
][
"describe"
]
=
"约"
+
str
(
int
(
distance
))
+
"米"
else
:
hit_item
[
"_source"
][
"describe"
]
=
"约"
+
str
(
round
(
1.0
*
distance
/
1000
,
1
))
+
"km"
hit_item
[
"_source"
][
"describe"
]
=
"约"
+
str
(
round
(
1.0
*
distance
/
1000
,
1
))
+
"km"
else
:
hit_item
[
"_source"
][
"describe"
]
=
">50km"
else
:
...
...
@@ -67,7 +70,8 @@ def get_suggest_tips(query,lat,lng,offset=0,size=50):
if
hit_item
[
"_source"
][
"type_flag"
]
==
"doctor"
:
hit_item
[
"_source"
][
"describe"
]
=
""
else
:
hit_item
[
"_source"
][
"describe"
]
=
"约"
+
str
(
hit_item
[
"_source"
][
"results_num"
])
+
"个结果"
if
hit_item
[
"_source"
][
"results_num"
]
else
""
hit_item
[
"_source"
][
"describe"
]
=
"约"
+
str
(
hit_item
[
"_source"
][
"results_num"
])
+
"个结果"
if
\
hit_item
[
"_source"
][
"results_num"
]
else
""
ret_list
.
append
(
hit_item
[
"_source"
])
return
ret_list
...
...
search_tips/settings_local.py
View file @
f500ae28
...
...
@@ -4,16 +4,15 @@
ALLOWED_HOSTS
=
[
"192.168.78.2"
]
SECRET_KEY
=
'e
%
$v6snev0807=t0@gk_n2#r5m6r1h(eil6cp^y3ub@ja@gk_t'
SENTRY_CELERY_ENDPOINT
=
"http://60b0004c8884420f8067fb32fc3ed244:20f97fc73ffa4aad9735d0e6542a6d78@sentry.igengmei.com/140"
SENTRY_CELERY_ENDPOINT
=
"http://60b0004c8884420f8067fb32fc3ed244:20f97fc73ffa4aad9735d0e6542a6d78@sentry.igengmei.com/140"
BROKER_URL
=
"redis://127.0.0.1:6379/8"
#REDIS_URL = "redis://127.0.0.1:6379"
#
REDIS_URL = "redis://127.0.0.1:6379"
REDIS_URL
=
"redis://127.0.0.1:6379/1"
CELERY_BROKER_URL
=
"redis://127.0.0.1:6379/8"
DATABASES
=
{
'default'
:
{
'ENGINE'
:
'django.db.backends.mysql'
,
...
...
@@ -42,19 +41,17 @@ DATABASES = {
}
ES_INFO_LIST
=
[
{
"host"
:
"10.29.130.141"
,
"port"
:
9200
}
{
"host"
:
"10.29.130.141"
,
"port"
:
9200
}
]
GM_ORI_ES_INFO_LIST
=
[
{
"host"
:
"10.29.130.141"
,
"port"
:
9200
}
{
"host"
:
"10.29.130.141"
,
"port"
:
9200
}
]
ES_INDEX_PREFIX
=
"gm-dbmw"
ES_INDEX_PREFIX
=
"gm_test"
trans2es/utils/hospital_transfer.py
View file @
f500ae28
...
...
@@ -4,13 +4,14 @@ import os
import
sys
import
logging
import
traceback
from
libs.tools
import
tzlc
,
getMd5Digest
from
libs.tools
import
tzlc
,
getMd5Digest
from
libs.es
import
ESPerform
from
libs.cache
import
redis_client
import
json
from
trans2es.commons.commons
import
get_tips_suggest_list
from
trans2es.commons.words_utils
import
QueryWordAttr
,
get_tips_word_type
from
trans2es.commons.words_utils
import
QueryWordAttr
,
get_tips_word_type
class
HospitalTransfer
(
object
):
...
...
@@ -34,20 +35,20 @@ class HospitalTransfer(object):
suggest_list
=
get_tips_suggest_list
(
instance
.
name
)
return
(
item_dict
,
suggest_list
)
return
(
item_dict
,
suggest_list
)
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
return
([],[])
return
([],
[])
@classmethod
def
get_hospital_lat_lng_info_to_redis
(
cls
,
instance
):
try
:
redis_key_prefix
=
"search_tips:hospital_latlng_info"
name
=
instance
.
name
name
=
instance
.
name
.
strip
()
lng
=
instance
.
baidu_loc_lng
lat
=
instance
.
baidu_loc_lat
pos_list
=
[
lng
,
lat
]
redis_client
.
hset
(
redis_key_prefix
,
name
,
pos_list
)
pos_list
=
[
lng
,
lat
]
redis_client
.
hset
(
redis_key_prefix
,
name
,
pos_list
)
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment