Skip to content
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

Better validation of mapping JSON #7534

Merged
merged 1 commit into from
Nov 12, 2014
Merged

Better validation of mapping JSON #7534

merged 1 commit into from
Nov 12, 2014

Conversation

colings86
Copy link
Contributor

The parsers for the mappers now remove each setting as they parse it and an error will be thrown if any settings are left after parsing is complete

Closes #7205

// Because this mapper extends LongFieldMapper the null_value field will be added to the JSON when transferring cluster state
// between nodes so we have to remove the entry here so that the validation doesn't fail
// TODO should murmur3 support null_value? at the moment if a user sets null_value it has to be silently ignored since we can't
// determine whether the JSON is the original JSON from the user or if its the serialised cluster state being passed between nodes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to get people's thoughts on this in particular as I am not sure which way to go on it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null_value is undocumented for murmur3 and also it seemed to have never worked at all (was not parsed and the set value ~0L was never used). I think we should remove it. Replacing the ~0L in ctor with null?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arbitrary null values don't really make sense for this mapper so I think it makes sense to remove support for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However I think it is important that the default configuration emits an empty json

@colings86 colings86 self-assigned this Sep 2, 2014
@brwe
Copy link
Contributor

brwe commented Sep 2, 2014

Was the intention of this to also work for root mappers like _all, _boost etc? I thought so because you changed AllFieldMapper but the change as is does not help detecting typos in fields of root mappers.

@brwe
Copy link
Contributor

brwe commented Sep 2, 2014

For the context suggester, one can still put arbitrary fields into the context, for example:

PUT testidx
{
  "mappings": {
    "service": {
      "properties": {
        "suggest_field": {
          "type": "completion",
          "context": {
            "location": {
              "type": "geo",
              "precision": "5m",
              "neighbors": true,
              "default": "u33",
              "foo": "bar"
            }
          }
        }
      }
    }
  }
}

@brwe
Copy link
Contributor

brwe commented Sep 2, 2014

This does not seem to work work for multi_fields yet, example below.
Maybe we need to check for each field directly after parsing if the mapping that is passed to the parser is empty instead of checking at the very end?

This should throw an exception but does not:

PUT testidx
{
  "mappings": {
    "doc": {
      "properties": {
        "text": {
          "type": "string",
          "fields": {
            "another_text": {
              "type": "string",
              "store": true,
              "foo": "bar"
            }
          }
        }
      }
    }
  }
}

docBuilder.put(typeParser.parse(fieldName, fieldNodeMap, parserContext));
fieldNodeMap.remove("type");
if (!fieldNodeMap.isEmpty()) {
throw new MapperParsingException("Field mapping for [" + fieldName + "] not empty after parsing! Remaining fields: " + getRemainingFields(fieldNodeMap));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this message go back to the end user? If so the fact that a map must be empty is more of an implementation detail than an meaningful error message for the end user. Something like "Mapping definition for field X has unsupported parameters: foo, bar" would be more appropriate.

@brwe
Copy link
Contributor

brwe commented Oct 17, 2014

@colings86 I left some comments but I love this change already so much!

One thing I noticed is that if I add a faulty dynamic_mapping, the parser will still not complain:

DELETE testidx

DELETE _template/my_template
PUT _template/my_template
{
  "template": "test*",
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "string_fields": {
            "mapping": {
              "type": "string",
              "foo":"bar"
            },
            "match": "*"
          }
        }
      ]
    }
  }
}
POST testidx/doc/1
{
  "text":1
}

GET _mapping

Should we do something about that as well?

@colings86
Copy link
Contributor Author

@brwe I have implemented most of your comments and left some replies on some others. I agree that the dynamic mapping should also be fixed but maybe we should open a new issue for this so that this one can be merged first?

@clintongormley
Copy link
Contributor

@colings86 please could you confirm whether this PR would fix #8317?

@colings86
Copy link
Contributor Author

@clintongormley yes this PR will fix #8317

@colings86
Copy link
Contributor Author

Suggestion from @brwe and @jpountz: Make it so the strict validation is only turned on if the index version is on or after the version that releases this change. This will eliminate the possibility of having a mapping that was created in a previous version that cannot be read by the current version. This is a precaution rather than a necessity

@clintongormley
Copy link
Contributor

@brwe and @jpountz - please could you review?

@clintongormley clintongormley added the :Search Foundations/Mapping Index mappings, including merging and defining field types label Nov 11, 2014
if (indexVersionCreated.onOrAfter(Version.V_2_0_0)) {
throw new MapperParsingException(message + getRemainingFields(fieldNodeMap));
} else {
logger.debug(message + "{}", getRemainingFields(fieldNodeMap));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just message + getRemainingFields(fieldNodeMap)?

@jpountz
Copy link
Contributor

jpountz commented Nov 11, 2014

@clintongormley @colings86 As far as I am concerned, I think it's ready, I just left minor notes. There is an issue with fielddata and norms settings, but this PR solves most of the issue so I think we should not try to solve it in this PR and merge soon.

@brwe
Copy link
Contributor

brwe commented Nov 12, 2014

agree with @jpountz

@colings86 colings86 merged commit 972afe6 into elastic:master Nov 12, 2014
@colings86 colings86 deleted the fix/7205 branch November 12, 2014 15:19
@colings86 colings86 removed the v1.5.0 label Nov 19, 2014
@clintongormley clintongormley changed the title Mappers: Better validation of mapping JSON Better validation of mapping JSON Jun 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search Foundations/Mapping Index mappings, including merging and defining field types v2.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation of mappings request to reject unsupported fields
5 participants