-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add support for 'flattened object' fields. #42541
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
93997ab
Add a simple JSON field mapper. (#33923)
jtibshirani 09b68e7
When parsing JSON fields, also create tokens prefixed with the field …
jtibshirani 9eb4bd1
Add support for querying JSON fields based on key. (#34621)
jtibshirani 7daf406
Add support for storing JSON fields. (#34942)
jtibshirani 624d9ca
Enforce a limit on the depth of the JSON object. (#35063)
jtibshirani 133f554
Disallow doc_values in the JSON field mapping. (#35282)
jtibshirani 9183cbd
Make sure stored JSON fields are properly decoded. (#35279)
jtibshirani 4507b85
Add tests for the supported query types. (#35319)
jtibshirani fbadb62
Add documentation for JSON fields. (#35281)
jtibshirani d0ec58a
Add a test around JSON fields and source filtering. (#35399)
jtibshirani 400bf0c
Prevent slow field lookups when JSON fields are present. (#39872)
jtibshirani f4d7929
Add doc values support for JSON fields. (#40069)
jtibshirani 42f6af6
Rename 'json' to 'embedded_json'. (#40712)
jtibshirani 78da6c5
Rename embedded_json.asciidoc for consistency.
jtibshirani 9126b04
Rebase keyed JSON ordinals to start from zero. (#41282)
jtibshirani 2b71ed8
Make sure to return keyed JSON field types in FieldTypeLookup#iterato…
jtibshirani bf5c457
Allow non-exact query types on the root JSON field. (#41290)
jtibshirani 7237b2b
Remove the 'experimental' tag.
jtibshirani 31d3a58
Address code review feedback.
jtibshirani c9420a3
Remove support for stored fields.
jtibshirani 74ebc19
Improve the field type documentation.
jtibshirani 9cefddf
Rename 'embedded JSON' to 'flattened object'.
jtibshirani 58533cf
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani ad35339
Fix full text queries, which were broken after the merge.
jtibshirani 8241627
Address code review feedback and remove leftover references to 'json'.
jtibshirani e47467b
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani daa200b
Ensure all JSON field types are available for lookup. (#41914)
jtibshirani c995f68
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani 232f494
Pull the flattened field type into a mapper plugin. (#43250)
jtibshirani 2c18194
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani 6f33b65
License the flattened mapper as basic. (#43690)
jtibshirani ac0a80b
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani c344200
Merge remote-tracking branch 'upstream/master' into object-fields
jtibshirani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
[role="xpack"] | ||
[testenv="basic"] | ||
|
||
[[flattened]] | ||
=== Flattened datatype | ||
|
||
By default, each subfield in an object is mapped and indexed separately. If | ||
the names or types of the subfields are not known in advance, then they are | ||
<<dynamic-mapping, mapped dynamically>>. | ||
|
||
The `flattened` type provides an alternative approach, where the entire | ||
object is mapped as a single field. Given an object, the `flattened` | ||
mapping will parse out its leaf values and index them into one field as | ||
keywords. The object's contents can then be searched through simple queries | ||
and aggregations. | ||
|
||
This data type can be useful for indexing objects with a large or unknown | ||
number of unique keys. Only one field mapping is created for the whole JSON | ||
object, which can help prevent a <<mapping-limit-settings, mappings explosion>> | ||
from having too many distinct field mappings. | ||
|
||
On the other hand, flattened object fields present a trade-off in terms of | ||
search functionality. Only basic queries are allowed, with no support for | ||
numeric range queries or highlighting. Further information on the limitations | ||
can be found in the <<supported-operations, Supported operations>> section. | ||
|
||
NOTE: The `flattened` mapping type should **not** be used for indexing all | ||
document content, as it treats all values as keywords and does not provide full | ||
search functionality. The default approach, where each subfield has its own | ||
entry in the mappings, works well in the majority of cases. | ||
|
||
An flattened object field can be created as follows: | ||
[source,js] | ||
-------------------------------- | ||
PUT bug_reports | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"title": { | ||
"type": "text" | ||
}, | ||
"labels": { | ||
"type": "flattened" | ||
} | ||
} | ||
} | ||
} | ||
|
||
POST bug_reports/_doc/1 | ||
{ | ||
"title": "Results are not sorted correctly.", | ||
"labels": { | ||
"priority": "urgent", | ||
"release": ["v1.2.5", "v1.3.0"], | ||
"timestamp": { | ||
"created": 1541458026, | ||
"closed": 1541457010 | ||
} | ||
} | ||
} | ||
-------------------------------- | ||
// CONSOLE | ||
// TESTSETUP | ||
|
||
During indexing, tokens are created for each leaf value in the JSON object. The | ||
values are indexed as string keywords, without analysis or special handling for | ||
numbers or dates. | ||
|
||
Querying the top-level `flattened` field searches all leaf values in the | ||
object: | ||
|
||
[source,js] | ||
-------------------------------- | ||
POST bug_reports/_search | ||
{ | ||
"query": { | ||
"term": {"labels": "urgent"} | ||
} | ||
} | ||
-------------------------------- | ||
// CONSOLE | ||
|
||
To query on a specific key in the flattened object, object dot notation is used: | ||
[source,js] | ||
-------------------------------- | ||
POST bug_reports/_search | ||
{ | ||
"query": { | ||
"term": {"labels.release": "v1.3.0"} | ||
} | ||
} | ||
-------------------------------- | ||
// CONSOLE | ||
|
||
[[supported-operations]] | ||
==== Supported operations | ||
|
||
Because of the similarities in the way values are indexed, `flattened` | ||
fields share much of the same mapping and search functionality as | ||
<<keyword, `keyword`>> fields. | ||
|
||
Currently, flattened object fields can be used with the following query types: | ||
|
||
- `term`, `terms`, and `terms_set` | ||
- `prefix` | ||
- `range` | ||
- `match` and `multi_match` | ||
- `query_string` and `simple_query_string` | ||
- `exists` | ||
|
||
When querying, it is not possible to refer to field keys using wildcards, as in | ||
`{ "term": {"labels.time*": 1541457010}}`. Note that all queries, including | ||
`range`, treat the values as string keywords. Highlighting is not supported on | ||
`flattened` fields. | ||
|
||
It is possible to sort on an flattened object field, as well as perform simple | ||
keyword-style aggregations such as `terms`. As with queries, there is no | ||
special support for numerics -- all values in the JSON object are treated as | ||
keywords. When sorting, this implies that values are compared | ||
lexicographically. | ||
|
||
Flattened object fields currently cannot be stored. It is not possible to | ||
specify the <<mapping-store, `store`>> parameter in the mapping. | ||
|
||
[[flattened-params]] | ||
==== Parameters for flattened object fields | ||
|
||
The following mapping parameters are accepted: | ||
|
||
[horizontal] | ||
|
||
<<mapping-boost,`boost`>>:: | ||
|
||
Mapping field-level query time boosting. Accepts a floating point number, | ||
defaults to `1.0`. | ||
|
||
`depth_limit`:: | ||
|
||
The maximum allowed depth of the flattened object field, in terms of nested | ||
inner objects. If a flattened object field exceeds this limit, then an | ||
error will be thrown. Defaults to `20`. | ||
|
||
<<doc-values,`doc_values`>>:: | ||
|
||
Should the field be stored on disk in a column-stride fashion, so that it | ||
can later be used for sorting, aggregations, or scripting? Accepts `true` | ||
(default) or `false`. | ||
|
||
<<eager-global-ordinals,`eager_global_ordinals`>>:: | ||
|
||
Should global ordinals be loaded eagerly on refresh? Accepts `true` or | ||
`false` (default). Enabling this is a good idea on fields that are | ||
frequently used for terms aggregations. | ||
|
||
<<ignore-above,`ignore_above`>>:: | ||
|
||
Leaf values longer than this limit will not be indexed. By default, there | ||
is no limit and all values will be indexed. Note that this limit applies | ||
to the leaf values within the flattened object field, and not the length of | ||
the entire field. | ||
|
||
<<mapping-index,`index`>>:: | ||
|
||
Determines if the field should be searchable. Accepts `true` (default) or | ||
`false`. | ||
|
||
<<index-options,`index_options`>>:: | ||
|
||
What information should be stored in the index for scoring purposes. | ||
Defaults to `docs` but can also be set to `freqs` to take term frequency | ||
into account when computing scores. | ||
|
||
<<null-value,`null_value`>>:: | ||
|
||
A string value which is substituted for any explicit `null` values within | ||
the flattened object field. Defaults to `null`, which means null sields are | ||
treated as if it were missing. | ||
|
||
<<similarity,`similarity`>>:: | ||
|
||
Which scoring algorithm or _similarity_ should be used. Defaults | ||
to `BM25`. | ||
|
||
`split_queries_on_whitespace`:: | ||
|
||
Whether <<full-text-queries,full text queries>> should split the input on | ||
whitespace when building a query for this field. Accepts `true` or `false` | ||
(default). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
server/src/main/java/org/elasticsearch/index/mapper/DynamicKeyFieldMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.elasticsearch.common.settings.Settings; | ||
|
||
/** | ||
* A field mapper that supports lookup of dynamic sub-keys. If the field mapper is named 'my_field', | ||
* then a user is able to search on the field in both of the following ways: | ||
* - Using the field name 'my_field', which will delegate to the field type | ||
* {@link DynamicKeyFieldMapper#fieldType()} as usual. | ||
* - Using any sub-key, for example 'my_field.some_key'. In this case, the search is delegated | ||
* to {@link DynamicKeyFieldMapper#keyedFieldType(String)}, with 'some_key' passed as the | ||
* argument. The field mapper is allowed to create a new field type dynamically in order | ||
* to handle the search. | ||
* | ||
* To prevent conflicts between these dynamic sub-keys and multi-fields, any field mappers | ||
* implementing this interface should explicitly disallow multi-fields. The constructor makes | ||
* sure to passes an empty multi-fields list to help prevent conflicting sub-keys from being | ||
* registered. | ||
* | ||
* Note: we anticipate that 'flattened' fields will be the only implementation of this | ||
* interface. Flattened object fields live in the 'mapper-flattened' module. | ||
*/ | ||
public abstract class DynamicKeyFieldMapper extends FieldMapper { | ||
|
||
public DynamicKeyFieldMapper(String simpleName, | ||
MappedFieldType fieldType, | ||
MappedFieldType defaultFieldType, | ||
Settings indexSettings, | ||
CopyTo copyTo) { | ||
super(simpleName, fieldType, defaultFieldType, indexSettings, MultiFields.empty(), copyTo); | ||
} | ||
|
||
public abstract MappedFieldType keyedFieldType(String key); | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could we move it next to the
object
andkeyword
fields that it relates to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these includes are just alphabetized. For the actual links to individual field types, I put it under "Specialised datatypes" to encourage users to think through whether it's appropriate for their data.