-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Introduce a constant_keyword
field.
#49713
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
jpountz
merged 29 commits into
elastic:master
from
jpountz:feature/singleton_keyword_field
Mar 2, 2020
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
db1a079
Introduce a `singleton_keyword` field.
jpountz c253ce7
iter
jpountz abf27f1
Rename to `constant_keyword`.
jpountz 002fe96
iter
jpountz c6738d8
iter
jpountz b7b894e
iter
jpountz f5f288e
Merge branch 'master' into feature/singleton_keyword_field
jpountz a86ad8a
iter
jpountz a5949d6
iter
jpountz c9c43aa
iter
jpountz 6b907c2
iter
jpountz 779d128
unused imports
jpountz 51dc118
add javadocs
jpountz 8da5b6c
iter
jpountz af4a55a
Merge branch 'master' into feature/singleton_keyword_field
jpountz d1b71f4
More docs.
jpountz f8ddbf7
Merge branch 'master' into feature/singleton_keyword_field
jpountz d346303
address review comments
jpountz c378cdf
Fix failures.
jpountz 59fd6b0
Merge branch 'master' into feature/singleton_keyword_field
jpountz e228d04
Merge branch 'master' into feature/singleton_keyword_field
jpountz 9ea92e2
Merge branch 'master' into feature/singleton_keyword_field
jpountz 11e643d
iter
jpountz 97df169
iter
jpountz c384b07
Remove constant_keyword from usage API.
jpountz 460f395
iter
jpountz 35dca00
Merge branch 'master' into feature/singleton_keyword_field
jpountz ca6fb74
Merge branch 'master' into feature/singleton_keyword_field
jpountz 4f833f2
Add support for metadata.
jpountz 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
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,85 @@ | ||
[role="xpack"] | ||
[testenv="basic"] | ||
|
||
[[constant-keyword]] | ||
=== Constant keyword datatype | ||
++++ | ||
<titleabbrev>Constant keyword</titleabbrev> | ||
++++ | ||
|
||
Constant keyword is a specialization of the <<keyword,`keyword`>> field for | ||
the case that all documents in the index have the same value. | ||
|
||
[source,console] | ||
-------------------------------- | ||
PUT logs-debug | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"@timestamp": { | ||
"type": "date" | ||
}, | ||
"message": { | ||
"type": "text" | ||
}, | ||
"level": { | ||
"type": "constant_keyword", | ||
"value": "debug" | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------- | ||
|
||
`constant_keyword` supports the same queries and aggregations as `keyword` | ||
fields do, but takes advantage of the fact that all documents have the same | ||
value per index to execute queries more efficiently. | ||
|
||
It is both allowed to submit documents that don't have a value for the field or | ||
that have a value equal to the value configured in mappings. The two below | ||
indexing requests are equivalent: | ||
|
||
[source,console] | ||
-------------------------------- | ||
POST logs-debug/_doc | ||
{ | ||
"date": "2019-12-12", | ||
"message": "Starting up Elasticsearch", | ||
"level": "debug" | ||
} | ||
|
||
POST logs-debug/_doc | ||
{ | ||
"date": "2019-12-12", | ||
"message": "Starting up Elasticsearch" | ||
} | ||
-------------------------------- | ||
//TEST[continued] | ||
|
||
However providing a value that is different from the one configured in the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also mention how the field can 'autoconfigure' itself based on the first indexed document? |
||
mapping is disallowed. | ||
|
||
In case no `value` is provided in the mappings, the field will automatically | ||
configure itself based on the value contained in the first indexed document. | ||
While this behavior can be convenient, note that it means that a single | ||
poisonous document can cause all other documents to be rejected if it had a | ||
wrong value. | ||
|
||
The `value` of the field cannot be changed after it has been set. | ||
|
||
[[constant-keyword-params]] | ||
==== Parameters for constant keyword fields | ||
|
||
The following mapping parameters are accepted: | ||
|
||
[horizontal] | ||
|
||
<<mapping-field-meta,`meta`>>:: | ||
|
||
Metadata about the field. | ||
|
||
`value`:: | ||
|
||
The value to associate with all documents in the index. If this parameter | ||
is not provided, it is set based on the first document that gets indexed. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
evaluationDependsOn(xpackModule('core')) | ||
|
||
apply plugin: 'elasticsearch.esplugin' | ||
|
||
esplugin { | ||
name 'constant-keyword' | ||
description 'Module for the constant-keyword field type, which is a specialization of keyword for the case when all documents have the same value.' | ||
classname 'org.elasticsearch.xpack.constantkeyword.ConstantKeywordMapperPlugin' | ||
extendedPlugins = ['x-pack-core'] | ||
} | ||
archivesBaseName = 'x-pack-constant-keyword' | ||
|
||
dependencies { | ||
compileOnly project(path: xpackModule('core'), configuration: 'default') | ||
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts') | ||
} | ||
|
||
integTest.enabled = false |
44 changes: 44 additions & 0 deletions
44
...main/java/org/elasticsearch/xpack/constantkeyword/ConstantKeywordInfoTransportAction.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,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.constantkeyword; | ||
|
||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.core.XPackField; | ||
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; | ||
import org.elasticsearch.xpack.core.action.XPackInfoFeatureTransportAction; | ||
|
||
public class ConstantKeywordInfoTransportAction extends XPackInfoFeatureTransportAction { | ||
|
||
private final XPackLicenseState licenseState; | ||
|
||
@Inject | ||
public ConstantKeywordInfoTransportAction(TransportService transportService, ActionFilters actionFilters, | ||
Settings settings, XPackLicenseState licenseState) { | ||
super(XPackInfoFeatureAction.CONSTANT_KEYWORD.name(), transportService, actionFilters); | ||
this.licenseState = licenseState; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return XPackField.CONSTANT_KEYWORD; | ||
} | ||
|
||
@Override | ||
public boolean available() { | ||
return licenseState.isConstantKeywordAllowed(); | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return true; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...rd/src/main/java/org/elasticsearch/xpack/constantkeyword/ConstantKeywordMapperPlugin.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,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.constantkeyword; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.mapper.Mapper; | ||
import org.elasticsearch.plugins.ActionPlugin; | ||
import org.elasticsearch.plugins.MapperPlugin; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.xpack.constantkeyword.mapper.ConstantKeywordFieldMapper; | ||
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonMap; | ||
|
||
public class ConstantKeywordMapperPlugin extends Plugin implements MapperPlugin, ActionPlugin { | ||
|
||
public ConstantKeywordMapperPlugin(Settings settings) {} | ||
|
||
@Override | ||
public Map<String, Mapper.TypeParser> getMappers() { | ||
return singletonMap(ConstantKeywordFieldMapper.CONTENT_TYPE, new ConstantKeywordFieldMapper.TypeParser()); | ||
} | ||
|
||
@Override | ||
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() { | ||
return Arrays.asList( | ||
new ActionHandler<>(XPackInfoFeatureAction.CONSTANT_KEYWORD, ConstantKeywordInfoTransportAction.class)); | ||
} | ||
} |
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.
A suggestion for restructuring this section a bit: it could start with something like "Imagine that you have an index containing cycles. There are a large number of bicycles, and many searches perform a filter on
cycle_type: bicycle
." Then you could explain how to speed up the searches by splitting the index in two and switching toconstant_keyword
. The point aboutmatch_all
getting rewritten away could be saved for a later paragraph as detail about how the optimization works.This approach seems nice to me because it immediately introduces the set-up and suggested optimization (which is intuitive even without knowing the details of query rewriting). Leading with the
match_all
justification might make it harder to understand for people who aren't as familiar with the search DSL.