def suggest_query_full_example(mapping, input_doc, query_text, analyzer=None): """ no mapping provided """ INDEX = "pytest" es = create_index(INDEX, mapping=mapping, analyzer=analyzer) # update_index_mapping(INDEX, es, mapping) insert_single_doc(INDEX, es, input_doc) query_index_mapping(INDEX, es) if analyzer is None: body = {"text": query_text, "suggest-1": {"term": {"field": "tags"}}} else: analyzer_name = get_analyzer_name(analyzer) query_analyzer(INDEX, es, analyzer_name, query_text) body = { "text": query_text, "suggest-1": {"term": {"field": "tags", "analyzer": analyzer_name}}, } res = suggest_query(INDEX, es, body=body) res = res["suggest-1"] for r in res: inp = r["text"] opt = [o["text"] for o in r["options"]] print(f"input text : {inp}\noptions :") pp.pprint(opt) print() delete_index(INDEX, es) return opt
def analyze_text(txt, analyzer): INDEX = "pytest" input_doc = {"tags": [txt]} delete_index(INDEX) es = create_index(INDEX, analyzer=analyzer) insert_single_doc(INDEX, es, input_doc) res = query_analyzer(INDEX, es, get_analyzer_name(analyzer), txt) delete_index(INDEX, es) tokens = [e["token"] for e in res["tokens"]] return tokens
def suggest_phrase_query_full_example(mapping, input_doc, query_text, analyzer=None): INDEX = "pytest" es = create_index(INDEX, mapping=mapping, analyzer=analyzer) insert_single_doc(INDEX, es, input_doc) query_index_mapping(INDEX, es) if analyzer is None: body = { "text": query_text, "suggest-1": { "phrase": { "field": "tags", "size": 5, "gram_size": 3, "direct_generator": [{"field": "tags", "suggest_mode": "always"}], "highlight": {"pre_tag": "<em>", "post_tag": "</em>"}, } }, } else: analyzer_name = get_analyzer_name(analyzer) query_analyzer(INDEX, es, analyzer_name, query_text) body = { "text": query_text, "suggest-1": { "phrase": { "field": "tags", "analyzer": analyzer_name, "size": 5, "gram_size": 3, "direct_generator": [{"field": "tags", "suggest_mode": "always"}], "highlight": {"pre_tag": "<em>", "post_tag": "</em>"}, } }, } res = suggest_query(INDEX, es, body=body) res = res["suggest-1"] for r in res: inp = r["text"] opt = [o["text"] for o in r["options"]] print(f"input text : {inp}\noptions :") pp.pprint(opt) print() delete_index(INDEX, es) return opt
def suggest_phrase_query_full_example_mimacom(input_doc, query_text, body=None, mapping=None, analyzer=None, fuzziness=None): # https://blog.mimacom.com/autocomplete-elasticsearch-part3/ INDEX = "pytest" if mapping is None: mapping = {"tags": {"type": "completion"}} es = create_index(INDEX, mapping=mapping, analyzer=analyzer) insert_single_doc(INDEX, es, input_doc) query_index_mapping(INDEX, es) if body is None: body = { "suggest-1": { "prefix": query_text, "completion": { "field": "tags" }, } } if not (fuzziness is None): for k in body.keys(): d = body[k]["completion"] d.update({"fuzzy": {"fuzziness": 1}}) body[k]["completion"] = d res = suggest_query(INDEX, es, body=body) # pp.pprint(res) opt = {} for k, v in res.items(): for r in v: inp = r["text"] opt[k] = [o["text"] for o in r["options"]] print(f"input text : {inp}\noptions :") pp.pprint(opt) print() delete_index(INDEX, es) return opt
def query_disease_example(input_doc, query_text, body=None, mapping=None, analyzer=None, fuzziness=None): INDEX = "pytest" if mapping is None: mapping = {"diseases": {"type": "text"}} es = create_index(INDEX, mapping=mapping, analyzer=analyzer) insert_single_doc(INDEX, es, input_doc) query_index_mapping(INDEX, es) if body is None: body = { "query": { "bool": { "must": [{ "match": { "diseases": { "query": query_text, "fuzziness": 0 if fuzziness is None else 1, } } }] } } } res = query_index(INDEX, es, body) delete_index(INDEX, es) try: return res[0]["_source"]["diseases"] except IndexError: return []
inp = r["text"] opt[k] = [o["text"] for o in r["options"]] print(f"input text : {inp}\noptions :") pp.pprint(opt) print() delete_index(INDEX, es) return opt ################################################################################ if __name__ == "__main__": if False: INDEX = "pytest" delete_index(INDEX) input_doc = {"tags": ["Bactéries", "Appareil génital féminin"]} query_text = "appar" suggest_phrase_query_full_example_mimacom(input_doc, query_text) if False: INDEX = "pytest" delete_index(INDEX) input_doc = {"tags": ["Bactéries", "Appareil génital féminin"]} query_text = "apareil gén" suggest_phrase_query_full_example_mimacom(input_doc, query_text, fuzziness=1) if False: # accent # old version of ES !!
return opt ################################################################################ if __name__ == "__main__": if False: suggest_query_full_example( {"tags": {"type": "keyword"}}, {"tags": ["Bactéries"]}, "Bactéri" ) if False: print(get_analyzer_name(FR_ANALYZER)) if False: # test analyzer INDEX = "pytest" mapping = {"tags": {"type": "keyword"}} input_doc = {"tags": ["Bactéries", "bact", "Appareil génital féminin"]} delete_index(INDEX) for analyzer in [ STD_ANALYZER, FR_ANALYZER, TRI_GRAM_ANALYZER, REVERSE_ANALYZER, ACCENT_ANALYZER, ]: for query_text in input_doc["tags"]: es = create_index(INDEX, mapping=mapping, analyzer=analyzer) insert_single_doc(INDEX, es, input_doc) query_analyzer(INDEX, es, get_analyzer_name(analyzer), query_text) delete_index(INDEX, es)