Last active
February 3, 2020 16:13
-
-
Save gcsfred/9a49e0dfbf9ebe16390973372e1e029e to your computer and use it in GitHub Desktop.
Query Elasticsearch with boosts and weights given by Amazon Personalize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query_es(text_search, category_boost_pairs, product_id_weight_pairs): | |
client = Elasticsearch() | |
the_body = { | |
"query": { | |
"function_score": { | |
"query": { | |
"bool": { | |
"should": arrange_json_array( | |
transform_category_boost(category_boost_pairs), { | |
"match": { | |
"keywords": text_search | |
} | |
}) | |
} | |
}, | |
"boost": "5", # Configurable | |
"functions": transform_product_id_weights(product_id_weight_pairs), | |
"score_mode": "max", | |
"boost_mode": "multiply" | |
} | |
} | |
} | |
response = client.search( | |
index="bank", | |
body=the_body | |
) | |
if response is None or response['hits'] is None or response['hits']['hits'] is None\ | |
or len(response['hits']['hits']) == 0: | |
_log_info('No search results.') | |
else: | |
_log_info('There are ' + str(len(response['hits']['hits'])) + ' search results.') | |
i = 1 | |
for hit in response['hits']['hits']: | |
_log_info('#' + str(i) + ': Score:' + str(hit['_score']) + ', search result:' + str(hit['_source'])) | |
i = i + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment