highlights and weights

This commit is contained in:
2023-04-13 09:46:53 +02:00
parent d95fa436eb
commit 4c92e00899
2 changed files with 17 additions and 8 deletions

View File

@@ -24,17 +24,25 @@ def es_query(query:str):
query ={ query ={
"multi_match":{ "multi_match":{
"query": query, "query": query,
"fields": ["title^20","title.ngrams^10","text","text.ngrams"], "fields": ["title^20","title.ngrams^10","text^5","text.ngrams"],
"type": "most_fields" "tie_breaker": 0.3
#"type": "most_fields"
} }
} }
return query
def es_highlight(): def es_highlight():
highlight = { highlight = {
"fields": { "fields": {
"title": {}, "title": {},
"text": {} "text": {"fragment_size" : 150},
"title.ngrams": {},
"text.ngrams": {"fragment_size" : 150},
} }
} }
return highlight
def es_search(query:str): def es_search(query:str):
with es_client() as client: with es_client() as client:
@@ -57,4 +65,5 @@ if __name__ =="__main__":
hit.get("_source",{}).get("title",""), hit.get("_source",{}).get("title",""),
" ".join(hit.get("highlight",{}).get("title",[""])), " ".join(hit.get("highlight",{}).get("title",[""])),
" ".join(hit.get("highlight",{}).get("text",[""])) " ".join(hit.get("highlight",{}).get("text",[""]))
)) ))

View File

@@ -4,7 +4,7 @@ import elasticsearch
settings = { settings = {
"index": "index":
{"max_ngram_diff": 3 {"max_ngram_diff": 7
}, },
"analysis": { "analysis": {
"analyzer": { "analyzer": {
@@ -19,10 +19,10 @@ settings = {
"my_tokenizer": { "my_tokenizer": {
"type": "ngram", "type": "ngram",
"min_gram": 3, "min_gram": 3,
"max_gram": 6, "max_gram": 10,
"token_chars": [ "token_chars": [
"letter", "letter",
"digit" "digit", "symbol"
] ]
} }
} }
@@ -55,7 +55,7 @@ mapping = {
"published": {"type": "date", "format": "date_optional_time"}, "published": {"type": "date", "format": "date_optional_time"},
"updated_at": {"type": "date", "format": "date_optional_time"}, "updated_at": {"type": "date", "format": "date_optional_time"},
"raw": { "raw": {
"type": "text", "type": "text",
"index": False "index": False
}, },
} }