Repository: equinor/fmu-sumo
Pull request #381: fix: improved grid property linkage
Author: login: rwiker
Created: 2025-03-03T08:31:23Z | Merged: 2025-03-03T11:18:04Z

`cpgrid` and `cpgrid_property` objects are supposed to be linked via the attributes `data.geometry.relative_path` (`cpgrid_property`)  and `file.relative_path` (`cpgrid`), with the additional requirement that the objects need to be of the correct classes and that case, iteration and realization should also match.

This requires that the sumo upload is correctly configured, which it mostly isn't. At some later time, it may be possible to update the objects already stored in Sumo and use just this linkage mechanism.

The alternative linkage uses `data.tagname` (`cpgrid_property`) and `data.name` (`cpgrid`), with the same addiotional requirements as above.


Diff:
diff --git a/src/fmu/sumo/explorer/objects/_child.py b/src/fmu/sumo/explorer/objects/_child.py
index 8b1be030..404995bf 100644
--- a/src/fmu/sumo/explorer/objects/_child.py
+++ b/src/fmu/sumo/explorer/objects/_child.py
@@ -12,6 +12,7 @@
     ("dataname", "data.name", "Object name"),
     ("classname", "class.name", "Object class name"),
     ("casename", "fmu.case.name", "Object case name"),
+    ("caseuuid", "fmu.case.uuid", "Object case uuid"),
     ("content", "data.content", "Content"),
     ("tagname", "data.tagname", "Object tagname"),
     ("columns", "data.spec.columns", "Object table columns"),
diff --git a/src/fmu/sumo/explorer/objects/_search_context.py b/src/fmu/sumo/explorer/objects/_search_context.py
index a0236718..2216384f 100644
--- a/src/fmu/sumo/explorer/objects/_search_context.py
+++ b/src/fmu/sumo/explorer/objects/_search_context.py
@@ -125,6 +125,7 @@ def _fn(value):
     "time": [_gen_filter_time, None],
     "name": [_gen_filter_name, None],
     "uuid": [_gen_filter_gen, "fmu.case.uuid.keyword"],
+    "relative_path": [_gen_filter_gen, "file.relative_path.keyword"],
     "tagname": [_gen_filter_gen, "data.tagname.keyword"],
     "dataformat": [_gen_filter_gen, "data.format.keyword"],
     "iteration": [_gen_filter_gen, "fmu.iteration.name.keyword"],
diff --git a/src/fmu/sumo/explorer/objects/cpgrid.py b/src/fmu/sumo/explorer/objects/cpgrid.py
index e885fc8e..eda3e697 100644
--- a/src/fmu/sumo/explorer/objects/cpgrid.py
+++ b/src/fmu/sumo/explorer/objects/cpgrid.py
@@ -59,26 +59,23 @@ def grid_properties(self):
         Returns:
             GridProperties: a SearchContext that holds the linked CPGridProperty instances.
         """
-        sc = SearchContext(self._sumo)
+        sc = SearchContext(self._sumo).grid_properties.filter(
+            uuid=self.caseuuid,
+            iteration=self.iteration,
+            realization=self.realization,
+        )
         return sc.filter(
             complex={
                 "bool": {
-                    "must": [
-                        {
-                            "term": {
-                                "data.geometry.relative_path.keyword": self._metadata[
-                                    "file"
-                                ]["relative_path"]
-                            }
-                        },
+                    "minimum_should_match": 1,
+                    "should": [
                         {
                             "term": {
-                                "fmu.case.uuid.keyword": self._metadata["fmu"][
-                                    "case"
-                                ]["uuid"]
+                                "data.geometry.relative_path.keyword": self.relative_path
                             }
                         },
-                    ]
+                        {"term": {"data.tagname.keyword": self.name}},
+                    ],
                 }
-            }
+            },
         )
diff --git a/src/fmu/sumo/explorer/objects/cpgrid_property.py b/src/fmu/sumo/explorer/objects/cpgrid_property.py
index b1c57503..b133e24f 100644
--- a/src/fmu/sumo/explorer/objects/cpgrid_property.py
+++ b/src/fmu/sumo/explorer/objects/cpgrid_property.py
@@ -59,25 +59,27 @@ def grid(self):
         Returns:
             Grid: a Grid object (an instance of class CPGrid).
         """
-        sc = SearchContext(self._sumo).filter(
+        sc = SearchContext(self._sumo).grids.filter(
+            uuid=self.caseuuid,
+            iteration=self.iteration,
+            realization=self.realization,
+        )
+        should = [
+            {"term": {"data.name.keyword": self.tagname}},
+        ]
+        dgrp = (
+            self.metadata.get("data", {})
+            .get("geometry", {})
+            .get("relative_path", None)
+        )
+        if dgrp is not None:
+            should.append({"term": {"file.relative_path.keyword": dgrp}})
+            pass
+        sc = sc.filter(
             complex={
                 "bool": {
-                    "must": [
-                        {
-                            "term": {
-                                "file.relative_path.keyword": self._metadata[
-                                    "data"
-                                ]["geometry"]["relative_path"]
-                            }
-                        },
-                        {
-                            "term": {
-                                "fmu.case.uuid.keyword": self._metadata["fmu"][
-                                    "case"
-                                ]["uuid"]
-                            }
-                        },
-                    ]
+                    "minimum_should_match": 1,
+                    "should": should,
                 }
             }
         )


Reviews:
user: login: equinor-ruaj
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-03T10:23:05Z
user: login: rwiker
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-03T10:25:24Z
user: login: equinor-ruaj
body: 👍 
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-03T11:16:32Z

Review comments:
diff_hunk: @@ -59,25 +59,35 @@ def grid(self):
         Returns:
             Grid: a Grid object (an instance of class CPGrid).
         """
-        sc = SearchContext(self._sumo).filter(
+        sc = SearchContext(self._sumo).grids.filter(
+            uuid=self.caseuuid,
+            iteration=self.iteration,
+            realization=self.realization,
+        )
+        should = [
+            {"term": {"data.name.keyword": self.tagname}},
+        ]
+        if (
+            self.metadata.get("data", {})
+            .get("geometry", {})
+            .get("relative_path", None)
+            is not None
+        ):
+            should.append(
+                {
+                    "term": {
+                        "file.relative_path.keyword": self._metadata["data"][
path: src/fmu/sumo/explorer/objects/cpgrid_property.py
user: login: equinor-ruaj
body: What's the difference between `self.metadata` and `self._metadata`?
created_at: 2025-03-03T10:23:05Z
author_association: CONTRIBUTOR
diff_hunk: @@ -59,25 +59,35 @@ def grid(self):
         Returns:
             Grid: a Grid object (an instance of class CPGrid).
         """
-        sc = SearchContext(self._sumo).filter(
+        sc = SearchContext(self._sumo).grids.filter(
+            uuid=self.caseuuid,
+            iteration=self.iteration,
+            realization=self.realization,
+        )
+        should = [
+            {"term": {"data.name.keyword": self.tagname}},
+        ]
+        if (
+            self.metadata.get("data", {})
+            .get("geometry", {})
+            .get("relative_path", None)
+            is not None
+        ):
+            should.append(
+                {
+                    "term": {
+                        "file.relative_path.keyword": self._metadata["data"][
path: src/fmu/sumo/explorer/objects/cpgrid_property.py
user: login: rwiker
body: None; `self.metadata` is an accessor for `self._metadata`... we (I) should be consistent about this... I'll update the PR.
created_at: 2025-03-03T10:25:24Z
author_association: CONTRIBUTOR

PR comments:
user: login: perolavsvendsen
created_at: 2025-03-03T08:45:10Z
body: > This requires that the sumo upload is correctly configured, which it mostly isn't.

I don't think "sumo upload" play any roles here. The configuration in question is related to `fmu-dataio` and the data export (and the associated generation of metadata).
author_association: MEMBER
user: login: rwiker
created_at: 2025-03-03T09:39:50Z
body: ~~This requires that the sumo upload is correctly configured, which it mostly isn't.~~

This requires that the data export (via `fmu-dataio`) is correctly configured, which it mostly isn't, for these specific data types.
author_association: CONTRIBUTOR
user: login: rwiker
created_at: 2025-03-28T13:15:15Z
body: Closes #354 
author_association: CONTRIBUTOR