7.1 客户端全文搜索API调用(Verison2.0)
更新日志
V2.0
支持Description 参数使用
拼音搜索
,比如搜索:防晒霜
时 可以传入 “fangshai”。优化中文分词。 支持
口红的
搜索 包含口红
的索引文档,并且过滤stop
词库中的document
。
7.1.1全文搜索 Url
// 示例:搜索资讯、产品、评论、所有文档
// 基本搜索参数:第一页 、 10条 、 排序按照 _score 评分值
// 分词搜索参数:搜索与'粉嫩'相关的文档
// 具体参数参数使用参考,7.1.2
7.1.2传递 Json 规范(搜索参数)
Name | IsNecessary | Comment | Type | Etc. |
---|---|---|---|---|
IndexList | True | 搜索索引名 | String | info,comment,product |
IndexType | True | 搜索分类名 | String | full |
CurrentPage | True | 当前页数 | Int | 1 |
PageSize | True | 每页条数 | Int | 10 |
Sort | True | 排序 | String | ID,_score |
Description | False | 分词搜索词 | String | 粉嫩的 |
调用方式
http-get
Python-Code
#!/usr/bin/python2.7
#-*- coding:utf8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import json
import time
import datetime
import os
import redis
from es_connect_host import ES
from esbash.info_bash import Model
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime("%Y-%m-%d")
else:
return json.JSONEncoder.default(self, obj)
class full_es_search:
def __init__(self):
pool = redis.ConnectionPool(host='xxx', port=6379, db=0 ,password='xxx')
self.r = redis.Redis(connection_pool=pool)
self.es = ES()
# 全文搜索的Api
## 至少匹配一种should-match
## info :Title + Description
## comment :Comment + Description
## product :ProductCName.pinyin(支持拼音+分词) + ProductEName + Description
def get_full_query(self,index,type,fromSize,size,sort,Description):
index = index
type = type
query_body = {
"query": {
"bool": {
"should": [
{ "match": { "Description": Description }},
{"match": {"Title": Description}},
{"match": {"Comment": Description}},
{ "match": { "ProductCName.pinyin": Description }}
]
}
},
# "query": {"match": {"Description": Description}},
# "_source": ["Description", "Title","ID"],
# "highlight": {
# "pre_tags": ["<tag1>", "<tag2>"],
# "post_tags": ["</tag1>", "</tag2>"],
# "fields": {
# "Description": {}
# }
# },
"from": fromSize,
"size": size,
"sort": {
sort: {"order": "desc"}
}
}
info = self.es.get_index_query(index,type,query_body)
infolen = len(info['hits']['hits'])
# print (json.dumps(info['hits']['hits'][0]))
# print (json.dumps(info['hits']['hits'][0]['_source']['KeyNO']))
for i in range(0,infolen):
# 添加当前资讯的评论总数
if(info['hits']['hits'][i]['_index']=='info'):
KeyNO = info['hits']['hits'][i]['_source']['KeyNo']
# print KeyNO
# print info['hits']['hits'][i]['_source']
if info['hits']['hits'][i]['_source']['Tag']:
model = Model('ali', 'InfoTag')
all_info_list = model.getTagListByTag(info['hits']['hits'][i]['_source']['Tag'])
info['hits']['hits'][i]['_source']['Tag'] = all_info_list
info['hits']['hits'][i]['_source']['CommentCount'] = self.r.hget('comment2', KeyNO)
model = Model('ali', 'InfoStatic')
Static = model.getAllStaticByKeyNO(KeyNO)
# # print info['hits']['hits'][i]['_source']['CommentCount']
info['hits']['hits'][i]['_source']['CommentCount'] = (info['hits']['hits'][i]['_source']['CommentCount'] if info['hits']['hits'][i]['_source']['CommentCount']>0 else 0)
# # print info['hits']['hits'][i]['_source']['CommentCount']
# # 添加资讯的点赞数
info['hits']['hits'][i]['_source']['LikeCount'] = self.r.bitcount('operation'+ KeyNO+'4')
# # print info['hits']['hits'][i]['_source']['LikeCount']
info['hits']['hits'][i]['_source']['LikeCount'] = (info['hits']['hits'][i]['_source']['LikeCount'] if info['hits']['hits'][i]['_source']['LikeCount']>0 else 0)
info['hits']['hits'][i]['_source']['LikeCount'] = Static[0]['FakeLikeCount'] + Static[0]['LikeCount']
# #
info['hits']['hits'][i]['_source']['ViewCount'] = Static[0]['FakeViewCount']+ Static[0]['ViewCount']
print (json.dumps(info,cls=CJsonEncoder))
if __name__ == "__main__":
index = str(sys.argv[1])
type = str(sys.argv[2])
fromSize = str(sys.argv[3])
size = str(sys.argv[4])
sort = str(sys.argv[5])
Description = str(sys.argv[6])
full_es_search().get_full_query(index,type,fromSize,size,sort,Description)