def get(self, request): key_words = request.GET.get('s', '') s_type = request.GET.get('s_type', 'article') re_datas = [] if s_type == "article": s = ArticleType.search() elif s_type == "question": s = QuestionType.search() elif s_type == "job": s = JobType.search() else: s = ArticleType.search() if key_words: s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source["title"]) return HttpResponse(json.dumps(re_datas), content_type="application/json")
def get(self, request): key_words = request.GET.get("s", "") #获取搜索建议关键词 return_suggest_list = [] #返回结果列表 if key_words: s = ArticleType.search() """fuzzy模糊搜索, fuzziness 编辑距离, prefix_length前面不变化的前缀长度""" s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) #创建对es操作的对象 suggestions = s.execute() #获取搜索建议的title for match in suggestions.suggest.my_suggest[0].options[:10]: source = match._source return_suggest_list.append(source["title"]) #返回数据 return HttpResponse(json.dumps(return_suggest_list), content_type="application/json")
def get(self, request): #s_type = request.GET.get("s_type","article") key_words = request.GET.get('s', '') # index.html pass s, default is black re_datas = [] # multiple key words if key_words: s = ArticleType.search() s = s.suggest( 'my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2, #"transpositions": "False" }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source["title"]) return HttpResponse(json.dumps(re_datas), content_type="application/json")
def get(self, request): key_words = request.GET.get('s', '') s_type = request.GET.get('s_type', 'article') re_datas = [] if key_words: if (s_type == 'question'): zhihu_s = AnswerType.search() zhihu_s = zhihu_s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = zhihu_s.execute_suggest() elif (s_type == 'article'): jobbole_s = ArticleType.search() jobbole_s = jobbole_s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = jobbole_s.execute_suggest() else: mooc_s = MoocType.search() mooc_s = mooc_s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = mooc_s.execute_suggest() for match in suggestions.my_suggest[0].options: source = match._source if (s_type == 'mooc'): re_datas.append(source['class_title']) else: re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type="application/json")
def get(self, request): key_words = request.GET.get('s','') #输入的是s #默认值为空'' re_datas = [] if key_words: s = ArticleType.search() s = s.suggest('my_suggest', key_words, completion={ #与es中格式类似 "field":"suggest", "fuzzy":{ "fuzziness":2 }, "size": 10 #返回10个 }) suggestions = s.execute_suggest() #返回一个{} for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source["title"]) #取title return HttpResponse(json.dumps(re_datas), content_type="application/json") #将数据返回给前端
def get(self, request): key_words = request.GET.get('s','') #接收前台输入的搜索词,准备到数据库查找 re_datas = [] if key_words: s = ArticleType.search() s = s.suggest('my_suggest', key_words, completion={ 'field':'suggest','fuzzy':{ 'fuzziness':2, }, 'size':10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type='application/json')
def get(self,request): suggest_keyword = request.GET.get("keyword") print("=================================") print("搜索关键词:",suggest_keyword) print("=================================") response_data = [] if suggest_keyword: s = ArticleType.search() s= s.suggest("my_suggest",suggest_keyword,completion={"field":"suggest","fuzzy":{"fuzziness":2},"size": 10}) suggestions = s.execute_suggest() for match in getattr(suggestions,"my_suggest")[0].options: source = match._source response_data.append(source["article_title"]) if len(response_data)> 10: response_data = response_data[:10] return HttpResponse(json.dumps({"status":200,"response":response_data},ensure_ascii=False))
def get(self, request): key_words = request.GET.get('s', '') re_datas = [] if key_words: s = ArticleType.search() s = s.suggest("my_suggest", key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 1 }, "size": 10, }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type='application/json')
def get(self, request): key_words = request.GET.get("s", "") return_data = [] if key_words: s = ArticleType.search() s = s.suggest("article_suggest", key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.article_suggest[0].options: source = match._source return_data.append(source["title"]) return HttpResponse(json.dumps(return_data), content_type="application/json")
def get(self, request): key_words = request.GET.get('s', '') re_datas = [] if key_words: s = ArticleType.search() s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute() # for match in suggestions.hits: for match in suggestions.suggest.my_suggest[0].options[:10]: source = match._source re_datas.append(source["title"]) return HttpResponse(json.dumps(re_datas), content_type="application/json")
def get(self, request): key_words = request.GET.get('s', '') current_page = request.GET.get('s_type', '') if current_page == "article": re_datas = [] if key_words: s = ArticleType.search() # fuzzy模糊搜索。fuzziness 编辑距离 prefix_length前面不变化的前缀长度 s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options[:10]: source = match._source re_datas.append(source["title"]) # re_datas.append(source["content"]) return HttpResponse( json.dumps(re_datas), content_type="application/json")
def get(self, request): key_words = request.GET.get('s', '') re_data = [] if key_words: s = ArticleType.search() # es的fuzzy搜索,具体请看02-elasticsearch简单查询 s = s.suggest( 'my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 # 返回的个数 }) suggestions = s.excute_suggest() for match in suggestions.my_suggest[0].options: source = match._source re_data.append(source['title']) return HttpResponse(json.dumps(re_data), content_type='application/json')
def handle_technical_article(self, keywords): """ 处理技术文章相关搜索建议 :param request: :return:返回值为字典类型,html_object以及data_object,分别对应render函数的第二个和第三个参数 """ ret_data = list() s = ArticleType.search() s = s.suggest('my_suggest', keywords, completion={ 'field': 'suggest', 'fuzzy': { 'fuzziness': 2 }, 'size': 10 }) suggestion = s.execute_suggest() for suggestion_obj in suggestion.my_suggest[0].options: source = suggestion_obj._source ret_data.append(source['title']) return ret_data
def get(self, request): key_words = request.GET.get('s', '') current_page = request.GET.get('s_type', '') if current_page == "article": re_datas = [] if key_words: s = ArticleType.search() # fuzzy模糊搜索。fuzziness 编辑距离 prefix_length前面不变化的前缀长度 s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options[:10]: source = match._source re_datas.append(source["title"]) # re_datas.append(source["content"]) return HttpResponse( json.dumps(re_datas), content_type="application/json") elif current_page == "job": re_datas = [] if key_words: s = LagouType.search() # fuzzy模糊搜索。fuzziness 编辑距离 prefix_length前面不变化的前缀长度 s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() name_set = OrderedSet() for match in suggestions.my_suggest[0].options[:10]: source = match._source name_set.add(source["title"]) for name in name_set: re_datas.append(name) # re_datas.append(source["content"]) return HttpResponse( json.dumps(re_datas), content_type="application/json") elif current_page == "question": re_datas = [] if key_words: s = ZhiHuQuestionType.search() # fuzzy模糊搜索。fuzziness 编辑距离 prefix_length前面不变化的前缀长度 s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) name_set = OrderedSet() suggestions = s.execute_suggest() if suggestions: for match in suggestions.my_suggest[0].options[:10]: if match._type == "question": source = match._source re_datas.append(source["title"]) elif match._type == "answer": source = match._source name_set.add(source["author_name"]) for name in name_set: re_datas.append(name) return HttpResponse( json.dumps(re_datas), content_type="application/json")
def get(request): key_words = request.GET.get('s', '') current_type = request.GET.get('s_type', '') if current_type == "article": return_suggest_list = [] if key_words: s = ArticleType.search() """ fuzzy 模糊搜索, fuzziness 编辑距离, prefix_length 前面不变化的前缀长度 """ s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute() for match in suggestions.suggest.my_suggest[0].options[:10]: source = match._source return_suggest_list.append(source["title"]) return HttpResponse(json.dumps(return_suggest_list), content_type="application/json") elif current_type == "job": return_suggest_list = [] if key_words: s = LagouType.search() s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute() # 对于不同公司同名职位去重,提高用户体验 name_set = OrderedSet() for match in suggestions.suggest.my_suggest[0].options[:10]: source = match._source name_set.add(source["title"]) for name in name_set: return_suggest_list.append(name) return HttpResponse(json.dumps(return_suggest_list), content_type="application/json") elif current_type == "question": return_suggest_list = [] if key_words: s_question = ZhiHuQuestionType.search() """fuzzy模糊搜索, fuzziness 编辑距离, prefix_length前面不变化的前缀长度""" s_question = s_question.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions_question = s_question.execute() for match in suggestions_question.suggest.my_suggest[ 0].options[:10]: source = match._source return_suggest_list.append(source["title"]) return HttpResponse(json.dumps(return_suggest_list), content_type="application/json")
def s_type(search_type): if search_type == "job": s = JobType.search() else: s = ArticleType.search() return s
def get(self, request): key_words = request.GET.get('s', '') current_page = request.GET.get('s_type', '') # 搜索文章 if current_page == "article": suggest_list = [] if key_words: s = ArticleType.search() s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options[:10]: source = match._source suggest_list.append(source["title"]) return HttpResponse(json.dumps(suggest_list), content_type="application/json") # 搜索问答 elif current_page == "question": suggest_list = [] if key_words: s = ZhiHuQuestionType.search() s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() if suggestions: for match in suggestions.my_suggest[0].options[:10]: if match._type == "question": source = match._source suggest_list.append(source["title"]) return HttpResponse(json.dumps(suggest_list), content_type="application/json") # 搜索职位 elif current_page == "job": suggest_list = [] if key_words: s = LagouType.search() s = s.suggest('my_suggest', key_words, completion={ "field": "suggest", "fuzzy": { "fuzziness": 2 }, "size": 10 }) suggestions = s.execute_suggest() for match in suggestions.my_suggest[0].options[:10]: source = match._source suggest_list.append(source["title"]) return HttpResponse(json.dumps(suggest_list), content_type="application/json")
def get(self, request): key_words = request.GET.get('s', '') current_type = request.GET.get('s_type', '') if current_type == 'article': re_datas = [] if key_words: s = ArticleType.search() s = s.suggest('my_suggest', key_words, completion={ 'field': 'suggest', 'fuzzy': { "fuzziness": 3 }, 'size': 10 }) suggestions = s.execute().suggest for match in suggestions.my_suggest[0].options: source = match._source re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type='application/json') elif current_type == 'question': re_datas = [] if key_words: s = ZhiHuQuestionType.search() s = s.suggest('my_suggest', key_words, completion={ 'field': 'suggest', 'fuzzy': { "fuzziness": 3 }, 'size': 10 }) suggestions = s.execute().suggest for match in suggestions[0].options: source = match._source re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type='application/json') elif current_type == 'job': re_datas = [] if key_words: s = LagouType().search() s = s.suggest('my_suggest', key_words, completion={ 'field': 'suggest', 'fuzzy': { "fuzziness": 3 }, 'size': 10 }) suggestions = s.execute().suggest for match in suggestions[0].options: source = match._source re_datas.append(source['title']) return HttpResponse(json.dumps(re_datas), content_type='application/json')
def get(self, request): key_words = request.GET.get('s', '') re_datas = [] if key_words: s = ArticleType.search() s.suggest()