Search code examples
elasticsearchopensearchamazon-opensearch

Filter with XY queries in AWS OpenSearch


I am an SQL developer but I have been assigned some work on OpenSearch. I need your help with this task.

Below is my JSON and I want to filter on X1 and X2 boundingBox.

a relation is intersects and the type is an envelope.

    {
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 33.830433,
    "hits": [
      {
        "_index": "device-events-enrich",
        "_id": "H84L-ooBK5cFPKZ8lVSR",
        "_score": 33.830433,
        "_ignored": [
          "event.original.keyword"
        ],
        "_source": {
          "v": 1,
          "src": {
            "type": "device",
            "deviceId": "f1abe4d8-dd27-412c-8f54-6ad0254cdc1b",
            "properties": {
              "swVersion": "1.5",
              "macAddr": "02:00:00:e8:7b:f3"
            }
          },
          "eventMeta": {
            "eventId": "cc569850-8952-42db-9c8c-ef611d7fa05f",
            "events": [
              {
                "event": [
                  {
                    "eventClass": "person",
                    "eventType": "ObjectClass",
                    "eventProps": {
                      "likelihood": "43",
                      "boundingBox": [
                        {
                          "x1": 4.43,
                          "y1": 4.27
                        },
                        {
                          "x2": 3.04,
                          "y2": 1.61
                        }
                      ],
                      "res": "1920x1080"
                    }
                  },
                  {
                    "eventClass": "glasses",
                    "eventType": "ObjectPattern"
                  },
                  {
                    "eventClass": "bottom",
                    "eventType": "ObjectPattern",
                    "eventProps": {
                      "color": "orange"
                    }
                  }
                ]
              }
            ]
          },
          "@version": "1",
          "t": "October 04 2023 15:06:17",
          "@timestamp": "2023-10-04T09:36:17.699542Z",
          "event": {
            "original": """{"v": 1, "t": "October 04 2023 15:06:17", "src": {"deviceId": "f1abe4d8-dd27-412c-8f54-6ad0254cdc1b", "type": "device", "properties": {"swVersion": "1.5", "macAddr": "02:00:00:e8:7b:f3"}}, "eventMeta": {"eventId": "cc569850-8952-42db-9c8c-ef611d7fa05f", "events": [{"event": [{"eventType": "ObjectClass", "eventClass": "person", "eventProps": {"likelihood": "43", "res": "1920x1080", "boundingBox": [{"x1": 4.43, "y1": 4.27}, {"y2": 1.61, "x2": 3.04}]}}, {"eventType": "ObjectPattern", "eventClass": "glasses"}, {"eventType": "ObjectPattern", "eventClass": "bottom", "eventProps": {"color": "orange"}}]}]}}"""
          }
        }
      }
    ]
  }
}

I have tried with below solutions but it is not working and gives error.

GET /device-events-enrich/_search
{
  "_source": ["eventMeta.events.event.eventProps.boundingBox.x1", "eventMeta.events.event.eventProps.boundingBox.x2"],
  "query": {
     "geo_shape": {
      "eventMeta.events.event.eventProps.boundingBox": {
        "shape": {
          "type": "envelope",
          "coordinates": [[
            [1.0, 4.0], 
            [6.0, 8.0]
          ]]
        },
        "relation": "intersects"
      }
    }
  }
}

Please let me know if I have missed something.


Solution

  • In order to use a xy_shape query (not geo_shape), the boundingBox field must be properly declared as type xy_shape in your mapping. It doesn't seem to be the case.

    So in your mapping you need to have this:

    {
      "mappings": {
        ...
           "boundingBox": {
             "type": "xy_shape"
           }
      }
    }
    

    Then, the bounding box data needs to be indexed like this. Note that the enveloppe must be specified with the top-left and bottom-right corners, in your case you have top-right and bottom-left, so you need to rework your coordinates to top-left, i.e. [minX, maxY] and bottom-right, i.e. [maxX, minY]

    "boundingBox": {
       "type" : "envelope",
       "coordinates" : [[3.04, 4.27], [4.43, 1.61]]
    }
    

    Finally, you'll be able to run your xy_shape query like this:

    {
      "query": {
        "xy_shape": {
          "geometry": {
            "eventMeta.events.event.eventProps.boundingBox": {
              "type": "envelope",
              "coordinates": [ [ 0.0, 6.0], [ 4.0, 2.0] ]
            },
            "relation": "INTERSECTS"
          }
        }
      }
    }