-
Notifications
You must be signed in to change notification settings - Fork 65
Fix fetching of parent doc properties during OpenSearch read #1148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,7 +123,9 @@ def to_docs(self, query_params: "BaseDBReader.QueryParams") -> list[Document]: | |
} | ||
) | ||
doc.properties[DocumentPropertyTypes.SOURCE] = DocumentSource.DB_QUERY | ||
doc.properties["score"] = data["_score"] | ||
doc.properties["score"] = ( | ||
data["_score"] if doc.properties.get("score") is None else doc.properties["score"] | ||
) | ||
result.append(doc) | ||
else: | ||
assert ( | ||
|
@@ -326,7 +328,6 @@ def _to_parent_doc(self, slice_query: dict[str, Any]) -> List[dict[str, Any]]: | |
break | ||
|
||
for hit in hits: | ||
# print(f"Element index: {hit['_source']['properties']['_element_index']}") | ||
if ( | ||
"parent_id" in hit["_source"] | ||
and hit["_source"]["parent_id"] is not None | ||
|
@@ -346,7 +347,6 @@ def _to_parent_doc(self, slice_query: dict[str, Any]) -> List[dict[str, Any]]: | |
client.close() | ||
|
||
ret = [doc["_source"] for doc in results] | ||
# logging.info(f"Sample: {ret[:5]}") | ||
return ret | ||
|
||
@timetrace("OpenSearchReader") | ||
|
@@ -402,14 +402,14 @@ def _to_doc(self, slice_query: dict[str, Any]) -> List[dict[str, Any]]: | |
return [{"doc": doc.serialize()} for doc in docs] | ||
|
||
def map_reduce_parent_id(self, group: pd.DataFrame) -> pd.DataFrame: | ||
# logger.info(f"Applying on {group} ({type(group)}) ...") | ||
parent_ids = set() | ||
for row in group["parent_id"]: | ||
# logging.info(f"Row: {row}: {type(row)}") | ||
parent_ids.add(row) | ||
if row not in parent_ids: | ||
parent_ids.add(row) | ||
|
||
logger.info(f"Parent IDs: {parent_ids}") | ||
return pd.DataFrame([{"_source": {"doc_id": parent_id, "parent_id": parent_id}} for parent_id in parent_ids]) | ||
# logger.info(f"Parent IDs: {parent_ids}") | ||
return pd.DataFrame([{"_source": {"doc_id": parent_id}} for parent_id in parent_ids]) | ||
|
||
def reconstruct(self, doc: dict[str, Any]) -> dict[str, Any]: | ||
# logging.info(f"Applying on {doc} ({type(doc)}) ...") | ||
|
@@ -419,8 +419,21 @@ def reconstruct(self, doc: dict[str, Any]) -> dict[str, Any]: | |
raise ValueError("Target is not present\n" f"Parameters: {self._query_params}\n") | ||
|
||
os_client = client._client | ||
records = OpenSearchReaderQueryResponse([doc], os_client) | ||
# doc["_source"]["properties"] = json.loads(doc["_source"]["properties"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
doc_id = doc["_source"]["doc_id"] | ||
assert isinstance( | ||
self._query_params, OpenSearchReaderQueryParams | ||
), f"Wrong kind of query parameters found: {self._query_params}" | ||
|
||
parent_doc = os_client.get( | ||
index=self._query_params.index_name, id=doc_id | ||
) # , _source_includes=["properties"])["_source"]["properties"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comments |
||
records = OpenSearchReaderQueryResponse([parent_doc], os_client) | ||
docs = records.to_docs(query_params=self._query_params) | ||
|
||
# properties[DocumentPropertyTypes.SOURCE] = DocumentSource.DOCUMENT_RECONSTRUCTION_PARENT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
# docs[0].update(parent_doc["_source"]) # json.loads(doc["_source"]["properties"]) | ||
# docs[0]["properties"] = json.loads(doc["_source"]["properties"]) | ||
return {"doc": docs[0].serialize()} | ||
|
||
def execute(self, **kwargs) -> "Dataset": | ||
|
@@ -570,6 +583,8 @@ def _execute_pit(self, **kwargs) -> "Dataset": | |
ds.flat_map(self._to_parent_doc, **self.resource_args) | ||
.groupby("parent_id") | ||
.map_groups(self.map_reduce_parent_id) | ||
# TODO use map_batches to improve 'get_all_elements_for_doc_ids' performance | ||
# by making fewer requests to OpenSearch | ||
.map(self.reconstruct) | ||
) | ||
else: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you delete