1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| def suggest(request,key_words): re_datas = [] if key_words: s = QAType.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["question"]) return HttpResponse(json.dumps(re_datas), content_type="application/json")
def search(request,key_words): client = Elasticsearch(hosts=["127.0.0.1"]) response = client.search( index="qa", body={ "query": { "multi_match": { "query": key_words, "fields": ["question", "topic", "answer"] } }, "from": 0, "size": 10 } )
hit_list = [] for hit in response["hits"]["hits"]: hit_dict = {} hit_dict["question"] = hit["_source"]["question"] hit_dict["topic"] = hit["_source"]["topic"] hit_dict["md5"] = hit["_source"]["md5"] hit_dict["score"] = hit["_score"] hit_dict["answer"] = hit["_source"]["answer"] hit_dict["expand"] = hit["_source"]["expand"]
hit_list.append(hit_dict)
return HttpResponse(json.dumps(hit_list), content_type="application/json")
|