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

Add runtime_mappings to search request #64374

Merged
merged 46 commits into from
Nov 10, 2020
Merged

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Oct 29, 2020

This adds a way to specify the runtime_mappings on a search request
which are always "runtime" fields. It looks like:

curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -XPOST -uelastic:password -HContent-Type:application/json 'localhost:9200/test/_bulk?pretty&refresh' -d'
{"index": {}}
{"animal": "cat", "sound": "meow"}
{"index": {}}
{"animal": "dog", "sound": "woof"}
{"index": {}}
{"animal": "snake", "sound": "hisssssssssssssssss"}
'

curl -XPOST -uelastic:password -HContent-Type:application/json localhost:9200/test/_search?pretty -d'
{
  "runtime_mappings": {
    "animal.upper": {
      "type": "keyword",
      "script": "for (String s : doc[\"animal.keyword\"]) {emit(s.toUpperCase())}"
    }
  },
  "query": {
    "match": {
      "animal.upper": "DOG"
    }
  }
}'

NOTE:
If we have to send a search request with runtime mappings to a node that
doesn't support runtime mappings at all then we'll fail the search
request entirely. The alternative would be to not send those runtime
mappings and let the node fail the search request with an "unknown field"
error. I believe this is would be hard to surprising because you defined
the field in the search request.

NOTE:
It isn't obvious but you can also use runtime_mappings to override fields
inside objects by naming the runtime fields with . in them. Like this:

curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_bulk?refresh -d'
{"index":{}}
{"name": {"first": "Andrew", "last": "Wiggin"}}
{"index":{}}
{"name": {"first": "Julian", "last": "Delphiki", "suffix": "II"}}
'

curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_search?pretty -d'{
  "runtime_mappings": {
    "name.first": {
      "type": "keyword",
      "script": "if (\"Wiggin\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Ender\");} else if (\"Delphiki\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Bean\");}"
    }
  },
  "query": {
    "match": {
      "name.first": "Bean"
    }
  }
}'

Relates to #59332

@nik9000 nik9000 requested a review from javanna October 29, 2020 18:19
@nik9000
Copy link
Member Author

nik9000 commented Oct 29, 2020

Draft because I expect @javanna and I to do a fair bit of iteration on this before we get it in.

This doesn't work with the fields option and, I think, it doesn't work with the fetch phase in general. But it seems to work for queries! So that is a start.

This adds a way to specify the `runtime_mappings` on a search request
which are always "runtime" fields. It looks like:
```
curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -XPOST -uelastic:password -HContent-Type:application/json 'localhost:9200/test/_bulk?pretty&refresh' -d'
{"index": {}}
{"animal": "cat", "sound": "meow"}
{"index": {}}
{"animal": "dog", "sound": "woof"}
{"index": {}}
{"animal": "snake", "sound": "hisssssssssssssssss"}
'

curl -XPOST -uelastic:password -HContent-Type:application/json localhost:9200/test/_search?pretty -d'
{
  "runtime_mappings": {
    "animal.upper": {
      "type": "keyword",
      "script": "for (String s : doc[\"animal.keyword\"]) {emit(s.toUpperCase())}"
    }
  },
  "query": {
    "match": {
      "animal.upper": "DOG"
    }
  }
}'
```
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.

thanks @nik9000 I think that this is how it should look. It requires a bit more refactoring to make sure that when we lookup field types in the fetch phase, we use the method that knows about runtime mappings. I am on it, I think.

if (oldRuntimeType != null) {
throw new ElasticsearchParseException("use [type] in [runtime_mappings] instead of [runtime_type]");
}
runtimeMappings.put(field, buildFieldType("runtime", field, node, parserContextSupplier.get(), indexSettings));
Copy link
Member

Choose a reason for hiding this comment

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

nice tricks! ;)

@@ -233,7 +235,8 @@ protected MultiSearchResponse shardOperation(Request request, ShardId shardId) t
shardId.id(),
searcher,
() -> { throw new UnsupportedOperationException(); },
null
null,
emptyMap() // NOCOMMIT is it right not to use the runtime mappings?
Copy link
Member

Choose a reason for hiding this comment

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

When I looked I think that this one gets a search source builder, so it seemed that runtime mappings can be specified, hence we should use them?

@javanna javanna added :Search/Search Search-related issues that do not fall into other categories >enhancement v8.0.0 v7.11.0 labels Oct 30, 2020
@nik9000
Copy link
Member Author

nik9000 commented Nov 3, 2020

@javanna and I talked about this offline - I believe we'll have to modify the method that looks like simpleMatchToFullName to pick up runtime fields as well.

@nik9000
Copy link
Member Author

nik9000 commented Nov 5, 2020

I've resolved the simpleMatchToFullName issues and pushed more on the tests. I'm down to 10 failures when running the core tests with search-time runtime fields.

@nik9000
Copy link
Member Author

nik9000 commented Nov 9, 2020

What do we expect to happen when folks do this:

curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_search?pretty -d'{
  "runtime_mappings": {
    "name": {
      "type": "object",
      "properties": {
        "first": {
          "type": "keyword",
          "script": "script"
        }
      }
    }
  },
  "query": {
    "match": {
      "name.first": "val"
    }
  }
}'

Right now you get a kind of silly error. I can make the error nicer. Or I could try and make it work?

@javanna
Copy link
Member

javanna commented Nov 9, 2020

@nik9000 users won't be able to create objects in the runtime mappings, but rather only runtime fields, eventually with fields in their names. If you throw an error now, that's good enough. With the introduction of the runtime section the parsers for runtime fields will be separate from the others hence this will be handled there.

@javanna
Copy link
Member

javanna commented Nov 9, 2020

@nik9000 maybe also mention overriding/shadowing in the description of the PR? For instance an example of shadowing a field within an object would help I think

@nik9000
Copy link
Member Author

nik9000 commented Nov 9, 2020

@nik9000 users won't be able to create objects in the runtime mappings, but rather only runtime fields, eventually with fields in their names. If you throw an error now, that's good enough. With the introduction of the runtime section the parsers for runtime fields will be separate from the others hence this will be handled there.

🤘 I'll make the error message reasonable and we can see what happens when we merge the runtime sections.

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.

LGTM thanks @nik9000 !

@nik9000 nik9000 merged commit a08b52f into elastic:master Nov 10, 2020
nik9000 added a commit that referenced this pull request Nov 10, 2020
In #64374 I broke a test that serializes `SearchRequest` with a random
version. I'm unsure how we didn't catch this in the PR tests but
computers are tricky. This fixes the test by removing runtime mappings
for versions that don't support it.
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Nov 10, 2020
This adds a way to specify the `runtime_mappings` on a search request
which are always "runtime" fields. It looks like:
```
curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -XPOST -uelastic:password -HContent-Type:application/json 'localhost:9200/test/_bulk?pretty&refresh' -d'
{"index": {}}
{"animal": "cat", "sound": "meow"}
{"index": {}}
{"animal": "dog", "sound": "woof"}
{"index": {}}
{"animal": "snake", "sound": "hisssssssssssssssss"}
'

curl -XPOST -uelastic:password -HContent-Type:application/json localhost:9200/test/_search?pretty -d'
{
  "runtime_mappings": {
    "animal.upper": {
      "type": "keyword",
      "script": "for (String s : doc[\"animal.keyword\"]) {emit(s.toUpperCase())}"
    }
  },
  "query": {
    "match": {
      "animal.upper": "DOG"
    }
  }
}'
```

NOTE:
If we have to send a search request with runtime mappings to a node that
doesn't support runtime mappings at all then we'll fail the search
request entirely. The alternative would be to not send those runtime
mappings and let the node fail the search request with an "unknown field"
error. I believe this is would be hard to surprising because you defined
the field in the search request.

NOTE:
It isn't obvious but you can also use `runtime_mappings` to override fields
inside objects by naming the runtime fields with `.` in them. Like this:
```
curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_bulk?refresh -d'
{"index":{}}
{"name": {"first": "Andrew", "last": "Wiggin"}}
{"index":{}}
{"name": {"first": "Julian", "last": "Delphiki", "suffix": "II"}}
'

curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_search?pretty -d'{
  "runtime_mappings": {
    "name.first": {
      "type": "keyword",
      "script": "if (\"Wiggin\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Ender\");} else if (\"Delphiki\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Bean\");}"
    }
  },
  "query": {
    "match": {
      "name.first": "Bean"
    }
  }
}'
```

Relates to elastic#59332
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Nov 10, 2020
Prepare to backport elastic#64374 by updating some versions constants so we can
send `runtime_mappings` to 7.11.0. Also disable bwc tests so they don't
fail until we finish the backport.
nik9000 added a commit that referenced this pull request Nov 11, 2020
Prepare to backport #64374 by updating some versions constants so we can
send `runtime_mappings` to 7.11.0. Also disable bwc tests so they don't
fail until we finish the backport.
nik9000 added a commit that referenced this pull request Nov 11, 2020
* Add `runtime_mappings` to search request (backport of #64374)

This adds a way to specify the `runtime_mappings` on a search request
which are always "runtime" fields. It looks like:
```
curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -XPOST -uelastic:password -HContent-Type:application/json 'localhost:9200/test/_bulk?pretty&refresh' -d'
{"index": {}}
{"animal": "cat", "sound": "meow"}
{"index": {}}
{"animal": "dog", "sound": "woof"}
{"index": {}}
{"animal": "snake", "sound": "hisssssssssssssssss"}
'

curl -XPOST -uelastic:password -HContent-Type:application/json localhost:9200/test/_search?pretty -d'
{
  "runtime_mappings": {
    "animal.upper": {
      "type": "keyword",
      "script": "for (String s : doc[\"animal.keyword\"]) {emit(s.toUpperCase())}"
    }
  },
  "query": {
    "match": {
      "animal.upper": "DOG"
    }
  }
}'
```

NOTE:
If we have to send a search request with runtime mappings to a node that
doesn't support runtime mappings at all then we'll fail the search
request entirely. The alternative would be to not send those runtime
mappings and let the node fail the search request with an "unknown field"
error. I believe this is would be hard to surprising because you defined
the field in the search request.

NOTE:
It isn't obvious but you can also use `runtime_mappings` to override fields
inside objects by naming the runtime fields with `.` in them. Like this:
```
curl -XDELETE -uelastic:password -HContent-Type:application/json localhost:9200/test
curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_bulk?refresh -d'
{"index":{}}
{"name": {"first": "Andrew", "last": "Wiggin"}}
{"index":{}}
{"name": {"first": "Julian", "last": "Delphiki", "suffix": "II"}}
'

curl -uelastic:password -XPOST -HContent-Type:application/json localhost:9200/test/_search?pretty -d'{
  "runtime_mappings": {
    "name.first": {
      "type": "keyword",
      "script": "if (\"Wiggin\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Ender\");} else if (\"Delphiki\".equals(doc[\"name.last.keyword\"].value)) {emit(\"Bean\");}"
    }
  },
  "query": {
    "match": {
      "name.first": "Bean"
    }
  }
}'
```

Relates to #59332
nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Nov 11, 2020
Now that elastic#64374 has been backported to 7.x we can run the bwc tests
again without wire incompatibilities.
nik9000 added a commit that referenced this pull request Nov 11, 2020
Now that #64374 has been backported to 7.x we can run the bwc tests
again without wire incompatibilities.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v7.11.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants