# Can't specify a subobject field in a path as the \_id field

**URL:** https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083
**Category:** Elasticsearch
**Created:** [September 20, 2012, 9:28pm UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083 "2012-09-20T21:28:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [September 20, 2012, 9:28pm UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/1 "2012-09-20T21:28:51Z")

</div>

I was designing an ES document and thought I'd use a \_id : { path : {  
... } } specification to pick a value I thought was really unique.

I have confidence that values are all unique. It is a SHA-256 based on  
the original file. We have used this in our previous system to Identify  
duplicate files.  
I decided to index the hash field within an object, so the definition  
looks like:  
But this doesn't work.

"myDocument": {  
\_id : { path : _"Text.hash"_ }  
,properties: {  
"originalPath": { type:"string", store:1,  
index:"not\_analyzed" }  
...  
,_Text_ : {  
,properties: {  
_hash: { type:"string", store:1, index:"not\_analyzed" }_  
,length: { type:"long", store:1 }  
...  
}  
}  
}  
}

When I specify the field to use as one within the Text object, as above,  
the index ends up with 1 document in it! And it's \_id is "[".  
[http://localhost:9200/\_search?pretty=1](http://localhost:9200/_search?pretty=1)

{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"failed" : 0  
},  
"hits" : {

- 

```
"total" : 1,*

```

"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "dragon0",  
"\_type" : "myDocument",  
_"\_id" : "["_,  
"\_score" : 1.0  
} ]  
}  
}

If I move the hash field outside of the subobject Text, I get unique  
documents.  
So This does work.  
"myDocument": {  
\_id : { path : _"hash"_ }  
,properties: {  
"originalPath": { type:"string", store:1,  
index:"not\_analyzed" }  
_,hash: { type:"string", store:1, index:"not\_analyzed" }_  
...  
,Text : {  
properties: {  
...  
,length: { type:"long", store:1 }  
...  
}  
}  
}  
}

If I again search for everything with.  
[http://localhost:9200/\_search?pretty=1](http://localhost:9200/_search?pretty=1)  
I get all the 200 documents I inserted.

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 3,  
"successful" : 3,  
"failed" : 0  
},  
"hits" : {

- 

```
"total" : 200,*

```

"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "dragon0",  
"\_type" : "myDocument",  
_"\_id" : "FjhwleNbQhyDh4XzB7k7BA",_  
"\_score" : 1.0  
}, {  
"\_index" : "dragon0",  
"\_type" : "myDocument",  
_"\_id" : "OVqLwly7RKmQ6R59q8JQOA",_  
"\_score" : 1.0  
}, {  
...

I think this is a bug. If not please  
Is this enough information to recreate the problem? I didn't have time  
to gist it today.  
-Paul

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [September 21, 2012, 9:11am UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/2 "2012-09-21T09:11:48Z")

</div>

Hi Paul

> I think this is a bug. If not please  
> Is this enough information to recreate the problem? I didn't have time  
> to gist it today.

It's quite difficult to follow when the JSON isn't formatted properly  
etc. Please can you gist a curl recreation that we can copy and paste  
to run, so that we can examine it locally?

ta

clint

> -Paul

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [September 21, 2012, 9:19am UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/3 "2012-09-21T09:19:21Z")

</div>

Hi Paul

> > I think this is a bug. If not please  
> > Is this enough information to recreate the problem? I didn't have time  
> > to gist it today.
> 
> It's quite difficult to follow when the JSON isn't formatted properly  
> etc. Please can you gist a curl recreation that we can copy and paste  
> to run, so that we can examine it locally?

Actually, it looks like your \_id path may be putting to an array? I  
would say that you are doing something illegal, but that ES should catch  
the problem and throw an error.

Opened as [The `_id` path should not allow arrays · Issue #2275 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2275)

curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
{  
"mappings" : {  
"test" : {  
"\_id" : {  
"path" : "foo.bar"  
}  
}  
}  
}  
'

### THIS WORKS

curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"foo" : {  
"bar" : 1,  
"baz" : "xx"  
}  
}  
'

# [Fri Sep 21 11:14:21 2012] Response:

# {

# "ok" : true,

# "\_index" : "test",

# "\_id" : "1",

# "\_type" : "test",

# "\_version" : 1

# }

### THIS DOESN'T

curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{  
"foo" : {  
"bar" : [  
2  
],  
"baz" : "xx"  
}  
}  
'

# [Fri Sep 21 11:14:23 2012] Response:

# {

# "ok" : true,

# "\_index" : "test",

# "\_id" : "[",

# "\_type" : "test",

# "\_version" : 1

# }

--

---

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [September 21, 2012, 5:10pm UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/4 "2012-09-21T17:10:09Z")

</div>

After looking at this more, my assumption now is that the real rule may  
be that the \_id path should point at a numeric value ready for direct  
use as an ID.  
I think your are right, it should complain about my naive, but illegal  
choice of fields to map to the ID, instead of whatever silent  
alternative path it took.

-Paul

On 9/21/2012 2:19 AM, Clinton Gormley wrote:

> Hi Paul
> 
> > > I think this is a bug. If not please  
> > > Is this enough information to recreate the problem? I didn't have time  
> > > to gist it today.  
> > > It's quite difficult to follow when the JSON isn't formatted properly  
> > > etc. Please can you gist a curl recreation that we can copy and paste  
> > > to run, so that we can examine it locally?  
> > > Actually, it looks like your \_id path may be putting to an array? I  
> > > would say that you are doing something illegal, but that ES should catch  
> > > the problem and throw an error.
> 
> Opened as [The `_id` path should not allow arrays · Issue #2275 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2275)
> 
> curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
> {  
> "mappings" : {  
> "test" : {  
> "\_id" : {  
> "path" : "foo.bar"  
> }  
> }  
> }  
> }  
> '
> 
> ### THIS WORKS
> 
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> {  
> "foo" : {  
> "bar" : 1,  
> "baz" : "xx"  
> }  
> }  
> '
> 
> # [Fri Sep 21 11:14:21 2012] Response:
> 
> # {
> 
> # "ok" : true,
> 
> # "\_index" : "test",
> 
> # "\_id" : "1",
> 
> # "\_type" : "test",
> 
> # "\_version" : 1
> 
> # }
> 
> ### THIS DOESN'T
> 
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> {  
> "foo" : {  
> "bar" : [  
> 2  
> ],  
> "baz" : "xx"  
> }  
> }  
> '
> 
> # [Fri Sep 21 11:14:23 2012] Response:
> 
> # {
> 
> # "ok" : true,
> 
> # "\_index" : "test",
> 
> # "\_id" : "[",
> 
> # "\_type" : "test",
> 
> # "\_version" : 1
> 
> # }

--

---

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [September 27, 2012, 7:31pm UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/5 "2012-09-27T19:31:01Z")

</div>

Followup to this post, so as not to throw anyone else off. You CAN use  
a non-numeric field as the \_id, just not one nested withing another  
(sub)object at this time, so the bug is either to complain or use the  
field properly.

-Paul

On 9/21/2012 10:11 AM, P. Hill wrote:

> After looking at this more, my assumption now is that the real rule  
> may be that the \_id path should point at a numeric value ready for  
> direct use as an ID.  
> I think your are right, it should complain about my naive, but illegal  
> choice of fields to map to the ID, instead of whatever silent  
> alternative path it took.

--

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 3:11am UTC](https://discuss.elastic.co/t/cant-specify-a-subobject-field-in-a-path-as-the--id-field/9083/6 "2017-07-06T03:11:07Z")

</div>


