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

Teach _search to read search time mappings #59316

Closed

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Jul 9, 2020

This adds support for search time mappings to the _search API like so:

curl -XPOST -HContent-Type:application/json -uelastic:password 'localhost:9200/test/_search?pretty&error_trace' -d'{
  "runtime_mappings": {
    "cat": {
      "type": "script",
      "runtime_type": "keyword",
      "script": "value(\"cat\")"
    }
  },
  "docvalue_fields": ["cat"]
}'

It just lifts the mappings field from the create index and mappings
update requests and drops it into the _search request. It then gives
each mapped field a chance to convert itself into a runtime field. Most
mapping can not and will throw an exception that is translated into an
HTTP 400 response. The script converts itself properly and is
available at runtime.

Relates to #59332

This adds support for search time mappings to the _search API like so:
```
curl -XPOST -HContent-Type:application/json -uelastic:password 'localhost:9200/test/_search?pretty&error_trace' -d'{
  "mappings": {
    "properties": {
      "cat": {
        "type": "script",
        "runtime_type": "keyword",
        "script": "value(\"cat\")"
      }
    }
  },
  "docvalue_fields": ["cat"]
}'
```

It just lifts the `mappings` field from the create index and mappings
update requests and drops it into the `_search` request. It then gives
each mapped field a chance to convert itself into a runtime field. Most
mapping can not and will throw an exception that is translated into an
HTTP 400 response. The `script` converts itself properly and is
available at runtime.
@nik9000
Copy link
Member Author

nik9000 commented Jul 9, 2020

"mappings": {
"properties": {

@javanna and I talked about changing it to just runtime_mappings. I used this syntax here because it is convenient, but it is, like the TODO in the code says, too big.

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

I left some comments, I guess one thing to look at is whether we could parse/validate the new section in the coordinating node so that we could fail early and once if something is wrong or if a non-runtime field is defined as part of the search request. Could we also validate the script once? I'm afraid it will be tough because MapperService is an index component, but there may be other ways to only validate?

@@ -239,6 +244,9 @@ public SearchSourceBuilder(StreamInput in) throws IOException {
sliceBuilder = in.readOptionalWriteable(SliceBuilder::new);
collapse = in.readOptionalWriteable(CollapseBuilder::new);
trackTotalHitsUpTo = in.readOptionalInt();
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
Copy link
Member

Choose a reason for hiding this comment

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

add a TODO to update the version once the branch is merged?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
out.writeMap(extraMapping);
} else {
throw new IllegalArgumentException(
Copy link
Member

Choose a reason for hiding this comment

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

why throw exception? shouldn't we rather not write the map, and that's it?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we don't write the map then nodes that can't understand will silently ignore the fields which feels bad!

Copy link
Member

Choose a reason for hiding this comment

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

oh true, so only some nodes may have the runtime fields which messes up the results? This is pretty important indeed. I wonder what effect does throwing here makes from a user's point of view.

Copy link
Member

Choose a reason for hiding this comment

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

also is not completely the truth. it depends on the nodes that get hit?

Copy link
Member Author

Choose a reason for hiding this comment

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

The last I checked it'll cause the request to 400 with the message. It does depend on which node gets hit, yeah.

I think it'll also come up in CCS where I expect the remote cluster could be at an older version for longer. I'll update the error message a little.

@nik9000 nik9000 requested a review from javanna July 13, 2020 20:07
@nik9000
Copy link
Member Author

nik9000 commented Jul 14, 2020

Just added an integration test! Yay!

@javanna javanna added the :Search/Search Search-related issues that do not fall into other categories label Jul 14, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/Search)

@elasticmachine elasticmachine added the Team:Search Meta label for search team label Jul 14, 2020
Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

I left some comments/questions

Mapper.Builder<?> builder = parser.parse(runtimeEntry.getKey(), definition, documentMapperParser().parserContext());
Mapper mapper = builder.build(builderContext);

MapperUtils.collect(mapper, objectMappers, fieldMappers, fieldAliasMappers);
Copy link
Member

Choose a reason for hiding this comment

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

Can you expand on why we need this? I was expecting that alias or objects would be caught anyways by your checks below, that only runtime fields can be defined.

Copy link
Member Author

Choose a reason for hiding this comment

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

This should allow us to collect the sub-mappers under objects. I do need to make sure there is a test for though.

@javanna javanna mentioned this pull request Jul 15, 2020
30 tasks
@javanna
Copy link
Member

javanna commented Aug 13, 2020

I marked this stalled as we decided to postpone allowing to define runtime fields as part of the search request. We will get back to this later.

@javanna javanna closed this Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Search/Search Search-related issues that do not fall into other categories stalled Team:Search Meta label for search team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants