forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a
constant_keyword
field. (elastic#49713)
This field is a specialization of the `keyword` field for the case when all documents have the same value. It typically performs more efficiently than keywords at query time by figuring out whether all or none of the documents match at rewrite time, like `term` queries on `_index`. The name is up for discussion. I liked including `keyword` in it, so that we still have room for a `singleton_numeric` in the future. However I'm unsure whether to call it `singleton`, `constant` or something else, any opinions? For this field there is a choice between 1. accepting values in `_source` when they are equal to the value configured in mappings, but rejecting mapping updates 2. rejecting values in `_source` but then allowing updates to the value that is configured in the mapping This commit implements option 1, so that it is possible to reindex from/to an index that has the field mapped as a keyword with no changes to the source. Backport of elastic#49713
- Loading branch information
Showing
16 changed files
with
1,184 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 | ||
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 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 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 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
38 changes: 38 additions & 0 deletions
38
...lugin/core/src/main/java/org/elasticsearch/xpack/core/ConstantKeywordFeatureSetUsage.java
This file contains 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,38 @@ | ||
/* | ||
* 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.core; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.xpack.core.flattened.FlattenedFeatureSetUsage; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ConstantKeywordFeatureSetUsage extends XPackFeatureSet.Usage { | ||
|
||
public ConstantKeywordFeatureSetUsage(StreamInput input) throws IOException { | ||
super(input); | ||
} | ||
|
||
public ConstantKeywordFeatureSetUsage(boolean available, boolean enabled) { | ||
super(XPackField.CONSTANT_KEYWORD, available, enabled); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
FlattenedFeatureSetUsage that = (FlattenedFeatureSetUsage) o; | ||
return available == that.available && enabled == that.enabled; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(available, enabled); | ||
} | ||
|
||
} |
This file contains 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 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 |
52 changes: 52 additions & 0 deletions
52
...word/src/main/java/org/elasticsearch/xpack/constantkeyword/ConstantKeywordFeatureSet.java
This file contains 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,52 @@ | ||
/* | ||
* 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.ActionListener; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.xpack.core.ConstantKeywordFeatureSetUsage; | ||
import org.elasticsearch.xpack.core.XPackFeatureSet; | ||
import org.elasticsearch.xpack.core.XPackField; | ||
|
||
import java.util.Map; | ||
|
||
public class ConstantKeywordFeatureSet implements XPackFeatureSet { | ||
|
||
private final XPackLicenseState licenseState; | ||
|
||
@Inject | ||
public ConstantKeywordFeatureSet(XPackLicenseState licenseState) { | ||
this.licenseState = licenseState; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return XPackField.CONSTANT_KEYWORD; | ||
} | ||
|
||
@Override | ||
public boolean available() { | ||
return licenseState.isConstantKeywordAllowed(); | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> nativeCodeInfo() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void usage(ActionListener<Usage> listener) { | ||
listener.onResponse(new ConstantKeywordFeatureSetUsage(available(), enabled())); | ||
} | ||
|
||
} |
Oops, something went wrong.