Esempio n. 1
0
    def search(cls, **kwargs):
        kwargs.update({
            'using': connections.get_connection(),
            'index': cls.get_index(),
            'doc_type': {
                cls._doc_type.name: cls.from_es
            },
        })
        sq = Search(**kwargs)

        # Add highlighting.
        sq = sq.highlight(*cls.excerpt_fields)
        sq = sq.highlight_options(order='score')

        return sq
Esempio n. 2
0
	def post(self):
		ts = self.args['_']
		if abs(int(time() * 1000) - int(ts)) > 1800000:
			return {'success':0, 'message': '时间戳无效'}, 200
		token = self.args['token']
		appkey = self.args['appkey']
		verify_token = flask_redis.get(appkey)
		if verify_token is None:
			return {'success': 0, 'message': 'token 无效'}, 200
		else:
			verify_token = verify_token.decode('utf-8') if type(verify_token) == type(b'') else verify_token
			if verify_token != token:
				return {'success': 0, 'message': 'token 无效'}, 200
		sign = self.args['sign']
		if hash_sha256("{0},{1},{2}".format(ts, token, appkey)) != sign:
			return {'success': 0, 'message': 'sign 无效'}, 200
		page = int(self.args['page'])
		size = int(self.args['size'])
		from_size = (page - 1) * size
		to_size = page * size
		channel = self.args['channel']
		category = self.args['category']
		location = self.args['location']
		times = self.args['times']
		and_ = self.args['and']
		sort_field = self.args['s']
		is_open = self.args['is_open']
		o = self.args['o']
		scope = self.args['scope']
		if scope in ['content', 'tag', 'title', 'description', 'author', 'writings']:
			scope = [scope]
		else:
			scope = ['content', 'tag', 'title', 'description', 'author', 'writings']
		keyword = self.args['keyword']
		try:
			s = Search(using=client, index='gdszx', doc_type='culture')
			if times:
				s = s.filter('term', times=times)
			if category:
				s = s.filter('term', category=category)
			if location:
				s = s.filter('term', location=location)
			if channel:
				s = s.filter('term', channel=channel)
			if is_open == '0':
				s = s.filter('term', is_open=False)
			elif is_open == '1':
				s = s.filter('term', is_open=True)
			s = s.query('multi_match', query=keyword, fields=scope)
			if and_ != None and and_.strip() != '':
				for word in split(r'\s+', and_.strip()):
					s = s.query('multi_match', query=word, fields=scope)
			s = s.highlight('title', fragment_size=50).highlight('content', fragment_size=100)
			s.aggs.bucket('times_all', 'terms', field='times', size=10)
			s.aggs.bucket('channel_all', 'terms', field='channel', size=10)
			s.aggs.bucket('category_all', 'terms', field='category', size=10)
			s.aggs.bucket('location_all', 'terms', field='location', size=10)
			s = s.sort(o+sort_field)
			s = s[from_size:to_size]
			response = s.execute()
			return {'success': 1, 'data': response.to_dict()}, 200
		except Exception as e:
			return {'success': 0, 'message': e}, 200
Esempio n. 3
0
	def post(self):
		ts = self.args['_']
		if abs(int(time() * 1000) - int(ts)) > 1800000:
			return {'success':0, 'message': '时间戳无效'}, 200
		token = self.args['token']
		appkey = self.args['appkey']
		verify_token = flask_redis.get(appkey)
		if verify_token is None:
			return {'success': 0, 'message': 'token 无效'}, 200
		else:
			verify_token = verify_token.decode('utf-8') if type(verify_token) == type(b'') else verify_token
			if verify_token != token:
				return {'success': 0, 'message': 'token 无效'}, 200
		sign = self.args['sign']
		if hash_sha256("{0},{1},{2}".format(ts, token, appkey)) != sign:
			return {'success': 0, 'message': 'sign 无效'}, 200
		query = Website.query.join(Token, Website.id==Token.website_id).filter(Token.appkey == appkey).first()
		domain = query.domain
		page = int(self.args['page'])
		size = int(self.args['size'])
		from_size = (page - 1) * size
		to_size = page * size
		origin = self.args['origin']
		channel = self.args['channel']
		category = self.args['category']
		author = self.args['author']
		editor = self.args['editor']
		begin = self.args['from']
		to = self.args['to']
		has_pic = self.args['has_pic']
		has_video = self.args['has_video']
		v1 = self.args['v1']
		v2 = self.args['v2']
		v3 = self.args['v3']
		v4 = self.args['v4']
		v5 = self.args['v5']
		v6 = self.args['v6']
		not_ = self.args['not']
		and_ = self.args['and']
		sort_field = self.args['s']
		o = self.args['o']
		f = self.args['f']
		l = self.args['l']
		l = [] if l is None else l.split(',')
		scope = self.args['scope']
		if scope in ['content', 'tag', 'title', 'description']:
			scope = [scope]
		else:
			scope = ['content', 'tag', 'title', 'description']
		keyword = self.args['keyword']
		try:
			s = Search(using=client, index='common', doc_type='search')
			s = s.filter('term', website=domain)
			if author:
				s = s.filter('term', author=author)
			if editor:
				s = s.filter('term', editor=editor)
			if origin:
				s = s.filter('term', origin=origin)
			if category:
				s = s.filter('term', category=category)
			if channel:
				s = s.filter('term', channel=channel)
			if v1:
				s = s.filter('term', reserved_1=v1)
			if v2:
				s = s.filter('term', reserved_2=v2)
			if v3:
				s = s.filter('term', reserved_3=v3)
			if v4:
				s = s.filter('term', reserved_4=v4)
			if v5:
				s = s.filter('term', reserved_5=v5)
			if v6:
				s = s.filter('term', reserved_6=v6)
			if has_pic == '0':
				s = s.filter('term', has_pic=False)
			elif has_pic == '1':
				s = s.filter('term', has_pic=True)
			if has_video == '0':
				s = s.filter('term', has_video=False)
			elif has_video == '1':
				s = s.filter('term', has_video=True)
			s = s.filter('range', pdate={'gte': begin, 'lte': to})

			s = s.query('multi_match', query=keyword, fields=scope)
			if not_:
				s = s.exclude('multi_match', query=not_, fields=scope)
			if and_ != None and and_.strip() != '':
				for word in split(r'\s+', and_.strip()):
					s = s.query('multi_match', query=word, fields=scope)
			s = s.highlight('title', fragment_size=50).highlight('content', fragment_size=100).highlight('tag', fragment_size=50).highlight('description', fragment_size=100)
			if f == 'title':
				s = s.exclude('terms', title__raw=l)
			elif f == 'url':
				s = s.exclude('terms', url=l)
			s = s.sort(o+sort_field)
			s = s[from_size:to_size]
			response = s.execute()
			related = related_search(client, keyword)
			return {'success': 1, 'data': response.to_dict(), 'related': related}, 200
		except Exception as e:
			return {'success': 0, 'message': e}, 200