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 field type for version strings #59773

Merged
merged 41 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
343310c
Add field type for version strings
Jul 14, 2020
397a7c0
Add yaml for search, ranges and scripting
Jul 20, 2020
08989e7
Add brute force regex query support
Jul 22, 2020
8a1acc2
Add fuzzy query support
Jul 23, 2020
bc972ad
Merge branch 'master' into add-version-field
Jul 23, 2020
5bac785
Remove some left-over commented code
Jul 23, 2020
8a7b43c
Adding tests
Jul 23, 2020
910616e
Make string_stats work
Jul 28, 2020
6d71b52
Merge branch 'master' into add-version-field
Jul 28, 2020
970085a
Adressing review comments
Jul 28, 2020
bdb89a2
Remove 'store_malformed' option
Jul 29, 2020
81bb114
Add tests for handling empty string
Jul 29, 2020
5fcf8a7
fix yaml test
Jul 30, 2020
1f3c995
Merge branch 'master' into add-version-field
Jul 30, 2020
cfe2dba
Change range query to points approximation and dv validation
Jul 31, 2020
c70e7c1
Register DocValueFormat via plugin
Jul 31, 2020
e45ef4a
Merge branch 'master' into add-version-field
Aug 17, 2020
24651d1
Merge branch 'master' into add-version-field
Aug 20, 2020
57b040f
Add valueFetcher() method
Aug 20, 2020
c321e0d
Merge branch 'master' of github.com:elastic/elasticsearch into add-ve…
Aug 21, 2020
5b902fb
Restrict field options by moving to ParametrizedFieldMapper
Aug 21, 2020
758b834
Merge branch 'master' into add-version-field
Aug 26, 2020
eaa1ef8
Change range query validation part
Aug 26, 2020
82a40ab
Small improvement to range query using points
Aug 31, 2020
dacf813
Merge branch 'master' into add-version-field
Aug 31, 2020
e4b0728
Add helper methods to versions ScriptDocValues to help with version p…
Sep 2, 2020
3c72862
Merge branch 'master' into add-version-field
Sep 2, 2020
4de9a2e
Merge branch 'master' into add-version-field
Sep 8, 2020
6c3266e
Removing points and subfields
Sep 3, 2020
b26a3f1
Add tests for new regex case-insensitivity option
Sep 8, 2020
c0c39bb
Adding docs
Sep 9, 2020
d56499e
Adressing review comments
Sep 14, 2020
7e2a4f5
Merge branch 'master' into add-version-field
Sep 14, 2020
94def20
Switch script method from isPreRelease to isRelease
Sep 15, 2020
67df1d3
Merge branch 'master' into add-version-field
Sep 15, 2020
ce51484
Another review iteration
Sep 15, 2020
04ac11d
Another round of reviews
Sep 17, 2020
ccebd6f
Moving docs under 'structured'
Sep 17, 2020
b0abc83
Merge branch 'master' into add-version-field
Sep 18, 2020
4c0b1e9
iter
Sep 18, 2020
6849550
Removing specialized script doc values functions
Sep 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/mapping/types/keyword.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ The following parameters are accepted by `keyword` fields:
include::constant-keyword.asciidoc[]

include::wildcard.asciidoc[]

include::version.asciidoc[]
cbuescher marked this conversation as resolved.
Show resolved Hide resolved
127 changes: 127 additions & 0 deletions docs/reference/mapping/types/version.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
[role="xpack"]
[testenv="basic"]
[discrete]
[[version-field-type]]
=== Version field type

The `version` field type is a specialization of the `keyword` field for
handling software version values and to support specialized precedence
rules for them. Precedence is defined following the rules outlined by
https://semver.org/[Semantic Versioning], which for example means that
major, minor and patch version parts are sorted numerically (i.e.
"2.1.0" < "2.4.1" < "2.11.2") and pre-release versions are sorted before
release versiond (i.e. "1.0.0-alpha" < "1.0.0").

You index a `version` field as follows

[source,console]
--------------------------------------------------
PUT my-index-000001
{
"mappings": {
"properties": {
"my_version": {
"type": "version"
}
}
}
}

--------------------------------------------------

The field offers the same search capabilities as a regular keyword field. It
can e.g. be searched for exact matches using `match` or `term` queries and
supports prefix and wildcard searches. The main benefit is that `range` queries
will honour Semver ordering, so a `range` query between "1.0.0" and "1.5.0"
will include versions of "1.2.3" but not "1.11.2" for example. Note that this
would be different when using a regular `keyword` field for indexing where ordering
is alphabetical.

Software versions are expected to follow the
https://semver.org/[Semantic Versioning rules] schema and precedence rules with
the notable exception that more or less than three main version identifiers are
allowed (i.e. "1.2" or "1.2.3.4" qualify as valid versions while they wouldn't under
strict Semver rules). Version strings that are not valid under the Semver definition
(e.g. "1.2.alpha.4") can still be indexed and retrieved as exact matches, however they
mayya-sharipova marked this conversation as resolved.
Show resolved Hide resolved
will all appear _after_ any valid version with regular alphabetical ordering. The empty
String "" is considered invalid and sorted after all valid versions, but before other
invalid ones.

[discrete]
[[version-params]]
==== Parameters for version fields

The following parameters are accepted by `version` fields:

[horizontal]

<<mapping-field-meta,`meta`>>::

Metadata about the field.

[discrete]
==== Limitations

This field type isn't optimized for heavy wildcard, regex or fuzzy searches. While those
type of queries work in this field, you should consider using a regular `keyword` field if
you strongly rely on these kind of queries.

==== Script support

The `version` fields offers some specialized access to detailed information derived from
valid version strings like the Major, Minor or Patch release number, whether the version value
is valid according to Semver or if it is a pre-release version. This can be helpful when e.g.
filtering for only released versions or running aggregations on parts of the version.
The following query, for example, filters for released versions and groups them by Major version
using a `terms` aggregation:

[source,console]
--------------------------------------------------
POST my-index-000001/_search
{
"query": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['my_version'].isRelease() == true"
}
}
}
]
}
},
"aggs": {
"group_major": {
"terms": {
"script": { "source": "doc['my_version'].getMajor()"},
"order": {
"_key": "asc"
}
}
}
}
}

--------------------------------------------------
// TEST[continued]

Functions available on via doc values in scripting are:
cbuescher marked this conversation as resolved.
Show resolved Hide resolved

[horizontal]

isValid()::
Returns `true` if the field contains a version thats legal according to the Semantic Versioning rules

isRelease()::
Returns `true` if the field contains a valid release version, `false` if it is a pre-release version or invalid.

getMajor()::
Returns an Integer value of the Major version if the version is valid, or null otherwise

getMinor()::
Returns an Integer value of the Minor version if the version is valid, or null otherwise.

getPatch()::
Returns an Integer value of the Patch version if the version is valid, or null otherwise.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@

/** Base {@link MappedFieldType} implementation for a field that is indexed
* with the inverted index. */
abstract class TermBasedFieldType extends SimpleMappedFieldType {
public abstract class TermBasedFieldType extends SimpleMappedFieldType {

TermBasedFieldType(String name, boolean isSearchable, boolean hasDocValues, TextSearchInfo textSearchInfo, Map<String, String> meta) {
public TermBasedFieldType(String name, boolean isSearchable, boolean hasDocValues, TextSearchInfo textSearchInfo,
Map<String, String> meta) {
super(name, isSearchable, hasDocValues, textSearchInfo, meta);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void collect(int doc, long bucket) throws IOException {
for (int i = 0; i < valuesCount; i++) {
BytesRef value = values.nextValue();
if (value.length > 0) {
String valueStr = value.utf8ToString();
String valueStr = (String) format.format(value);
int length = valueStr.length();
totalLength.increment(bucket, length);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Integration tests for the version field
#
---
setup:

- skip:
features: headers
version: " - 7.99.99"
reason: "version field is added to 8.0 first"

- do:
indices.create:
index: test_index
body:
mappings:
properties:
version:
type: version

- do:
bulk:
refresh: true
body:
- '{ "index" : { "_index" : "test_index", "_id" : "1" } }'
- '{"version": "1.1.0" }'
- '{ "index" : { "_index" : "test_index", "_id" : "2" } }'
- '{"version": "2.0.0-beta" }'
- '{ "index" : { "_index" : "test_index", "_id" : "3" } }'
- '{"version": "3.1.0" }'

---
"Store malformed":
- do:
indices.create:
index: test_malformed
body:
mappings:
properties:
version:
type: version

- do:
bulk:
refresh: true
body:
- '{ "index" : { "_index" : "test_malformed", "_id" : "1" } }'
- '{"version": "1.1.0" }'
- '{ "index" : { "_index" : "test_malformed", "_id" : "2" } }'
- '{"version": "2.0.0-beta" }'
- '{ "index" : { "_index" : "test_malformed", "_id" : "3" } }'
- '{"version": "v3.1.0" }'
- '{ "index" : { "_index" : "test_malformed", "_id" : "4" } }'
- '{"version": "1.el6" }'

- do:
search:
index: test_malformed
body:
query: { "match" : { "version" : "1.el6" } }

- do:
search:
index: test_malformed
body:
query: { "match_all" : { } }
sort:
version: asc

- match: { hits.total.value: 4 }
- match: { hits.hits.0._source.version: "1.1.0" }
- match: { hits.hits.1._source.version: "2.0.0-beta" }
- match: { hits.hits.2._source.version: "1.el6" }
- match: { hits.hits.3._source.version: "v3.1.0" }

---
"Basic ranges":
- do:
search:
index: test_index
body:
query: { "range" : { "version" : { "gt" : "1.1.0", "lt" : "9999" } } }

- match: { hits.total.value: 2 }

- do:
search:
index: test_index
body:
query: { "range" : { "version" : { "gte" : "1.1.0", "lt" : "9999" } } }

- match: { hits.total.value: 3 }

- do:
search:
index: test_index
body:
query: { "range" : { "version" : { "gte" : "2.0.0", "lt" : "9999" } } }

- match: { hits.total.value: 1 }

- do:
search:
index: test_index
body:
query: { "range" : { "version" : { "gte" : "2.0.0-alpha", "lt" : "9999" } } }

- match: { hits.total.value: 2 }
Loading