now.executor.indexer.elastic.es_query_building module#

now.executor.indexer.elastic.es_query_building.generate_score_calculation(docs_map, encoder_to_fields)[source]#

Generate score calculation from document mappings.

Parameters
  • docs_map (Dict[str, DocumentArray]) – dictionary mapping encoder to DocumentArray.

  • encoder_to_fields (Dict[str, Union[str, List[str]]]) – dictionary mapping encoder to fields.

Return type

List[List]

Returns

a list of score calculation, each of which is a tuple of (query_field, document_field, encoder, linear_weight). score calculation would then be for example: [(‘query_text’, ‘title’, ‘clip’, 1.0)]

now.executor.indexer.elastic.es_query_building.build_es_queries(docs_map, get_score_breakdown, score_calculation, metric='cosine', filter={}, query_to_curated_ids={})[source]#

Build script-score query used in Elasticsearch. To do this, we extract embeddings from the query document and pass them in the script-score query together with the fields to search on in the Elasticsearch index. The query document will be returned with all of its embeddings as tags with their corresponding field+encoder as key.

Parameters
  • docs_map – dictionary mapping encoder to DocumentArray.

  • get_score_breakdown (bool) – whether to return the score breakdown for matches. For this function, this parameter determines whether to return the embeddings of a query document.

  • score_calculation (List[Tuple]) – list of nested lists containing (query_field, document_field, matching_method, linear_weight) which define how to calculate the score. Note, that the matching_method is the name of the encoder or bm25.

  • metric (Optional[str]) – metric to use for vector search.

  • filter (dict) – dictionary of filters to apply to the search.

  • query_to_curated_ids (Dict[str, list]) – dictionary mapping query text to list of curated ids.

Return type

Dict

Returns

a dictionary containing query and filter.

now.executor.indexer.elastic.es_query_building.get_default_query(doc, score_calculation, filter={})[source]#
now.executor.indexer.elastic.es_query_building.get_pinned_query(doc, query_to_curated_ids={})[source]#
Return type

Dict

now.executor.indexer.elastic.es_query_building.process_filter(filter)[source]#
Return type

List[Dict[str, Any]]

now.executor.indexer.elastic.es_query_building.get_scores(encoder, score_calculation)[source]#