inveniosoftware / domapping

Package generating elasticsearch mapping from jsonschemas
https://domapping.readthedocs.io
GNU General Public License v2.0
14 stars 8 forks source link

global: management of circular references #13

Open JavierDelgadoFernandez opened 8 years ago

JavierDelgadoFernandez commented 8 years ago

I am writing this issue here because domapping is one of the affected tools.

The case is very simple. Right now, we have a circular reference in one of our schemas and it will occur more times in the near future, so trying to avoid this case doesn't look to be possible.

An example could be something like this: An owner has a car which has an owner. In this case, you can denormalize the schemas, but not in our real case. In our case, someone is trying to create a composite pattern using schemas.

Here is a little example:

Owner:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "car": {
      "$ref": "car.json#"
    }
  }
}

Car:

 {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "owner": {
      "$ref": "owner.json#"
    }
  }
}

The solution in this case is easy: denormalize the data. However, we can not apply the same solution to our case.

We have been thinking about several solutions:

1) We can set the type of the inner item to object when we are generating the mapping and we detect the circular reference. It is not mapped properly but we avoid the problem.

2) Set the inner field as a number in order to use it as a external identifier. In this way, you need to normalize the data from the client and denormalize again to send it to the client.

3) Try to avoid somehow the dependencies (Check in our case (CERN Analysis Preservation) all the schemas looking for a solution to these issues.) but it will not be reasonable.

Thanks a lot in advance.

JavierDelgadoFernandez commented 8 years ago

Ping.

tiborsimko commented 8 years ago

Ping.

CC @nharraud @inveniosoftware/triagers can be used to alert people

jirikuncar commented 8 years ago

@JavierDelgadoFernandez you can use definitions:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "owner": {
      "type": "object",
      "properties": {
      "car": {
        "$ref": "#/definitions/car"
      }
    },
    "car": {
      "type": "object",
      "properties": {
        "owner": {
          "$ref": "#/definitions/owner"
        }      
     }
  },

if you want only one schema:

  "type": "object",
  "oneOf": [
    {"$ref": "#/definitions/owner"},
    {"$ref": "#/definitions/car"}
  ]
}

otherwise create new schemas with references:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$ref": "base.json#/definitions/owner or car"
}
nharraud commented 8 years ago

@jirikuncar I think the issue is that domapping works in a recursive manner.

But the problem might be more generic. I don't think that elasticsearch mappings provides any easy way to support recursive mappings. At least parent-child relationships cannot be used for that, see elastic/elasticsearch#8100 and elastic/elasticsearch#11432.

It might be possible with dynamic templates. @JavierDelgadoFernandez could you check if you can use it to create by hand a mapping matching your needs? If it is possible by hand then domapping can do the same.