Example #1
0
def _suggest_batch(client, texts: Iterable[str], **kwargs) -> List[Dict]:
    suggester_name = "autocorrect"
    queries = (generate_suggest_query(text, name=suggester_name, **kwargs)
               for text in texts)
    body = generate_msearch_body(settings.ELASTICSEARCH_PRODUCT_INDEX, queries)
    response = client.msearch(body=body, doc_type=settings.ELASTICSEARCH_TYPE)

    suggestions = []

    for r in response["responses"]:
        if r["status"] != 200:
            root_cause = response["error"]["root_cause"][0]
            error_type = root_cause["type"]
            error_reason = root_cause["reason"]
            print("Elasticsearch error: {} [{}]"
                  "".format(error_reason, error_type))
            continue

        suggestions.append(r["suggest"][suggester_name][0])

    return suggestions
Example #2
0
 def suggest_batch(self, texts: Iterable[str]) -> List[Dict]:
     queries = [self.__generate_query(text) for text in texts]
     body = generate_msearch_body(self.index_name, queries)
     response = self.client.msearch(body=body)
     suggestions = self.__postprocess_response(response)
     return suggestions