Skip to content

Commit

Permalink
Make typeless APIs usable with indices whose type name is different f…
Browse files Browse the repository at this point in the history
…rom `_doc`.

This commit makes `document`, `update`, `explain`, `termvectors` and `mapping`
typeless APIs work on indices that have a type whose name is not `_doc`.
Unfortunately, this needs to be a bit of a hack since I didn't want calls with
random type names to see documents with the type name that the user had chosen
upon type creation.

The `explain` and `termvectors` do not support being called without a type for
now so the test is just using `_doc` as a type for now, we will need to fix
tests later but this shouldn't require further changes server-side since passing
`_doc` as a type name is what typeless APIs do internally anyway.

Relates elastic#35190
  • Loading branch information
jpountz committed Nov 21, 2018
1 parent 7b2d547 commit d376d9d
Show file tree
Hide file tree
Showing 19 changed files with 522 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
"DELETE with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
index:
index: index
type: not_doc
id: 1
body: { foo: bar }

- do:
catch: bad_request
delete:
index: index
type: some_random_type
id: 1

- do:
delete:
index: index
id: 1

- match: { _index: "index" }
- match: { _type: "_doc" }
- match: { _id: "1"}
- match: { _version: 2}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
"Explain with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
index:
index: index
type: not_doc
id: 1
body: { foo: bar }

- do:
indices.refresh: {}

- do:
catch: missing
explain:
index: index
type: some_random_type
id: 1
body:
query:
match_all: {}

- match: { _index: "index" }
- match: { _type: "some_random_type" }
- match: { _id: "1"}
- match: { matched: false}

- do:
explain:
index: index
type: _doc #todo: make _explain typeless and remove this
id: 1
body:
query:
match_all: {}

- match: { _index: "index" }
- match: { _type: "_doc" }
- match: { _id: "1"}
- is_true: matched
- match: { explanation.value: 1 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
"GET with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
index:
index: index
type: not_doc
id: 1
body: { foo: bar }

- do:
catch: missing
get:
index: index
type: some_random_type
id: 1

- match: { _index: "index" }
- match: { _type: "some_random_type" }
- match: { _id: "1"}
- match: { found: false}

- do:
get:
index: index
id: 1

- match: { _index: "index" }
- match: { _type: "_doc" }
- match: { _id: "1"}
- match: { _version: 1}
- match: { _source: { foo: bar }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
"Index with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
index:
index: index
id: 1
body: { foo: bar }

- match: { _index: "index" }
- match: { _type: "_doc" }
- match: { _id: "1"}
- match: { _version: 1}

- do:
get: # not using typeless API on purpose
index: index
type: not_doc
id: 1

- match: { _index: "index" }
- match: { _type: "not_doc" } # the important bit to check
- match: { _id: "1"}
- match: { _version: 1}
- match: { _source: { foo: bar }}


- do:
index:
index: index
body: { foo: bar }

- match: { _index: "index" }
- match: { _type: "_doc" }
- match: { _version: 1}
- set: { _id: id }

- do:
get: # using typeful API on purpose
index: index
type: not_doc
id: '$id'

- match: { _index: "index" }
- match: { _type: "not_doc" } # the important bit to check
- match: { _id: $id}
- match: { _version: 1}
- match: { _source: { foo: bar }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"GET mapping with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: include_type_name was introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
indices.get_mapping:
include_type_name: false
index: index

- match: { index.mappings.properties.foo.type: "keyword" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
"PUT mapping with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: include_type_name was introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
indices.put_mapping:
include_type_name: false
index: index
body:
properties:
bar:
type: "long"

- do:
indices.get_mapping:
include_type_name: false
index: index

- match: { index.mappings.properties.foo.type: "keyword" }
- match: { index.mappings.properties.bar.type: "long" }

- do:
catch: bad_request
indices.put_mapping:
index: index
body:
some_other_type:
properties:
bar:
type: "long"
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
"Term vectors with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "text"
term_vector: "with_positions"

- do:
index:
index: index
type: not_doc
id: 1
body: { foo: bar }

- do:
indices.refresh: {}

- do:
termvectors:
index: index
type: _doc # todo: remove when termvectors support typeless API
id: 1

- is_true: found
- match: {_type: _doc}
- match: {term_vectors.foo.terms.bar.term_freq: 1}

- do:
termvectors:
index: index
type: some_random_type
id: 1

- is_false: found
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
"Update with typeless API on an index that has types":

- skip:
version: " - 6.99.99"
reason: Typeless APIs were introduced in 7.0.0

- do:
indices.create: # not using include_type_name: false on purpose
index: index
body:
mappings:
not_doc:
properties:
foo:
type: "keyword"

- do:
index:
index: index
type: not_doc
id: 1
body: { foo: bar }

- do:
update:
index: index
id: 1
body:
doc:
foo: baz

- do:
get:
index: index
type: not_doc
id: 1

- match: { _source.foo: baz }
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.search.SearchService;
Expand Down Expand Up @@ -109,10 +111,8 @@ protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId
SearchContext context = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
Engine.GetResult result = null;
try {
Term uidTerm = context.mapperService().createUidTerm(request.type(), request.id());
if (uidTerm == null) {
return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false);
}
// No need to check the type, IndexShard#get does it for is
Term uidTerm = new Term(IdFieldMapper.NAME, Uid.encodeId(request.id()));
result = context.indexShard().get(new Engine.Get(false, false, request.type(), request.id(), uidTerm));
if (!result.exists()) {
return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false);
Expand Down
Loading

0 comments on commit d376d9d

Please sign in to comment.