Skip to content

Commit

Permalink
Synthetic source: load text from stored fields (#87480)
Browse files Browse the repository at this point in the history
Adds support for loading `text` and `keyword` fields that have
`store: true`. We could likely load *any* stored fields, but I
wanted to blaze the trail using something fairly useful.
  • Loading branch information
nik9000 authored Aug 17, 2022
1 parent 2a08258 commit 79a8979
Show file tree
Hide file tree
Showing 27 changed files with 1,162 additions and 486 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.elasticsearch.index.mapper.MapperBuilderContext;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.mapper.SimpleMappedFieldType;
import org.elasticsearch.index.mapper.SortedNumericDocValuesSyntheticFieldLoader;
import org.elasticsearch.index.mapper.SourceLoader;
import org.elasticsearch.index.mapper.SourceValueFetcher;
import org.elasticsearch.index.mapper.TextSearchInfo;
Expand Down Expand Up @@ -705,7 +706,7 @@ public SourceLoader.SyntheticFieldLoader syntheticFieldLoader() {
"field [" + name() + "] of type [" + typeName() + "] doesn't support synthetic source because it declares copy_to"
);
}
return new NumberFieldMapper.NumericSyntheticFieldLoader(name(), simpleName()) {
return new SortedNumericDocValuesSyntheticFieldLoader(name(), simpleName()) {
@Override
protected void writeValue(XContentBuilder b, long value) throws IOException {
b.value(decodeForSyntheticSource(value, scalingFactor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unsupported:
body:
mappings:
_source:
synthetic: true
mode: synthetic
properties:
join_field:
type: join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,79 @@ force_synthetic_source_bad_mapping:
index: test
id: 1
force_synthetic_source: true

---
stored text:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
text:
type: text
store: true

- do:
index:
index: test
id: 1
refresh: true
body:
text: the quick brown fox

- do:
get:
index: test
id: 1
- match: {_index: "test"}
- match: {_id: "1"}
- match: {_version: 1}
- match: {found: true}
- match:
_source:
text: the quick brown fox

---
stored keyword:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
kwd:
type: keyword
store: true

- do:
index:
index: test
id: 1
refresh: true
body:
kwd: the quick brown fox

- do:
get:
index: test
id: 1
- match: {_index: "test"}
- match: {_id: "1"}
- match: {_version: 1}
- match: {found: true}
- match:
_source:
kwd: the quick brown fox
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,55 @@ keyword:
docs.1._source:
kwd: bar

---
stored text:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
text:
type: text
store: true

- do:
index:
index: test
id: 1
body:
text: the quick brown fox

- do:
index:
index: test
id: 2
body:
text: jumped over the lazy dog

- do:
mget:
index: test
body:
ids: [1, 2]
- match: {docs.0._index: "test"}
- match: {docs.0._id: "1"}
- match:
docs.0._source:
text: the quick brown fox

- match: {docs.1._index: "test"}
- match: {docs.1._id: "2"}
- match:
docs.1._source:
text: jumped over the lazy dog

---
force_synthetic_source_ok:
- skip:
Expand All @@ -60,22 +109,25 @@ force_synthetic_source_ok:
_source:
mode: stored
properties:
kwd:
type: keyword
obj:
properties:
kwd:
type: keyword

- do:
index:
index: test
id: 1
body:
kwd: foo
obj.kwd: foo

- do:
index:
index: test
id: 2
body:
kwd: bar
obj:
kwd: bar

# When _source is used in the fetch the original _source is perfect
- do:
Expand All @@ -85,10 +137,11 @@ force_synthetic_source_ok:
ids: [1, 2]
- match:
docs.0._source:
kwd: foo
obj.kwd: foo
- match:
docs.1._source:
kwd: bar
obj:
kwd: bar

# When we force synthetic source dots in field names get turned into objects
- do:
Expand All @@ -99,16 +152,18 @@ force_synthetic_source_ok:
ids: [ 1, 2 ]
- match:
docs.0._source:
kwd: foo
obj:
kwd: foo
- match:
docs.1._source:
kwd: bar
obj:
kwd: bar

---
force_synthetic_source_bad_mapping:
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0
version: " - 8.4.99"
reason: message changed in 8.5

- do:
indices.create:
Expand Down Expand Up @@ -157,5 +212,5 @@ force_synthetic_source_bad_mapping:
force_synthetic_source: true
body:
ids: [ 1, 2 ]
- match: {docs.0.error.reason: "field [text] of type [text] doesn't support synthetic source unless it has a sub-field of type [keyword] with doc values enabled and without ignore_above or a normalizer"}
- match: {docs.1.error.reason: "field [text] of type [text] doesn't support synthetic source unless it has a sub-field of type [keyword] with doc values enabled and without ignore_above or a normalizer"}
- match: {docs.0.error.reason: "field [text] of type [text] doesn't support synthetic source unless it is stored or has a sub-field of type [keyword] with doc values or stored and without ignore_above or a normalizer"}
- match: {docs.1.error.reason: "field [text] of type [text] doesn't support synthetic source unless it is stored or has a sub-field of type [keyword] with doc values or stored and without ignore_above or a normalizer"}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,123 @@ keyword:
hits.hits.0._source:
kwd: foo

---
stored text:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
text:
type: text
store: true

- do:
index:
index: test
id: 1
refresh: true
body:
text: the quick brown fox

- do:
search:
index: test
body:
query:
ids:
values: [1]
- match:
hits.hits.0._source:
text: the quick brown fox

---
stored keyword:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
kwd:
type: keyword
store: true

- do:
index:
index: test
id: 1
refresh: true
body:
kwd: the quick brown fox

- do:
search:
index: test
body:
query:
ids:
values: [1]
- match:
hits.hits.0._source:
kwd: the quick brown fox

---
stored keyword without sibling fields:
- skip:
version: " - 8.4.99"
reason: introduced in 8.5.0

- do:
indices.create:
index: test
body:
mappings:
_source:
mode: synthetic
properties:
kwd:
type: keyword
store: true

- do:
bulk:
refresh: true
index: test
body:
- '{"index": {}}'
- '{"kwd": "the quick brown fox", "s": 1, "n": 1}'
- '{"index": {}}'
- '{"kwd": "jumped over the lazy dog", "s": 2}'

- do:
search:
index: test
body:
sort: s
- match:
hits.hits.0._source:
kwd: the quick brown fox
s: 1
n: 1
- match:
hits.hits.1._source:
kwd: jumped over the lazy dog
s: 2

---
force_synthetic_source_ok:
- skip:
Expand Down
Loading

0 comments on commit 79a8979

Please sign in to comment.