# Buggy code
def _convert_es_hit_to_document(
    self, hit: dict, adapt_score_for_embedding: bool = False
) -> Document:
    # We put all additional data of the doc into meta_data and return it in the API
    meta_data = {
        k: v
        for k, v in hit["_source"].items()
        if k not in (self.text_field, self.faq_question_field, self.embedding_field)
    }
    name = meta_data.pop(self.name_field, None)
    if name:
        meta_data["name"] = name

    score = hit["_score"] if hit["_score"] else None
    if score:
        if adapt_score_for_embedding:
            score -= 1
            probability = (score + 1) / 2  # scaling probability from cosine similarity
        else:
            probability = float(
                expit(np.asarray(score / 8))
            )  # scaling probability from TFIDF/BM25
    else:
        probability = None
    document = Document(
        id=hit["_id"],
        text=hit["_source"].get(self.text_field),
        meta=meta_data,
        score=score,
        probability=probability,
        question=hit["_source"].get(self.faq_question_field),
        embedding=hit["_source"].get(self.embedding_field),
    )
    return document


# Traceback (elasticsearch.exceptions.RequestError)
10/13/2020 06:43:06 - WARNING - elasticsearch -   POST http://localhost:9200/faq/_search [status:400 request:0.130s]
Traceback (most recent call last):
File "chatbots/haystack/faqbot.py", line 214, in <module>
main()
File "chatbots/haystack/faqbot.py", line 186, in main
faqbot.interact(question)
File "chatbots/haystack/faqbot.py", line 57, in interact
prediction = finder.get_answers_via_similar_questions(question=question, top_k_retriever=1)
File "/Users/aaronbriel/chatbots/haystack/.venv/src/farm-haystack/haystack/finder.py", line 95, in get_answers_via_similar_questions
documents = self.retriever.retrieve(question, top_k=top_k_retriever, filters=filters, index=index)
File "/Users/aaronbriel/chatbots/haystack/.venv/src/farm-haystack/haystack/retriever/dense.py", line 327, in retrieve
top_k=top_k, index=index)
File "/Users/aaronbriel/chatbots/haystack/.venv/src/farm-haystack/haystack/document_store/elasticsearch.py", line 465, in query_by_embedding
result = self.client.search(index=index, body=body, request_timeout=300)["hits"]["hits"]
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 152, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 1617, in search
body=body,
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/transport.py", line 392, in perform_request
raise e
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/transport.py", line 365, in perform_request
timeout=timeout,
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py", line 269, in perform_request
self._raise_error(response.status, raw_data)
File "/Users/aaronbriel/chatbots/haystack/.venv/lib/python3.7/site-packages/elasticsearch/connection/base.py", line 301, in _raise_error
status_code, error_message, additional_info
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'script_score script returned an invalid score [-3.4487913] for doc [41]. Must be a non-negative score!')
make: *** [ask_faq] Error 1

# Fixed code
def _convert_es_hit_to_document(
    self, hit: dict, adapt_score_for_embedding: bool = False
) -> Document:
    # We put all additional data of the doc into meta_data and return it in the API
    meta_data = {
        k: v
        for k, v in hit["_source"].items()
        if k not in (self.text_field, self.faq_question_field, self.embedding_field)
    }
    name = meta_data.pop(self.name_field, None)
    if name:
        meta_data["name"] = name

    score = hit["_score"] if hit["_score"] else None
    if score:
        if adapt_score_for_embedding:
            score -= 1000
            probability = (score + 1) / 2  # scaling probability from cosine similarity
        else:
            probability = float(
                expit(np.asarray(score / 8))
            )  # scaling probability from TFIDF/BM25
    else:
        probability = None
    document = Document(
        id=hit["_id"],
        text=hit["_source"].get(self.text_field),
        meta=meta_data,
        score=score,
        probability=probability,
        question=hit["_source"].get(self.faq_question_field),
        embedding=hit["_source"].get(self.embedding_field),
    )
    return document