# Search on nested failed if data is not updated

**URL:** <https://discuss.elastic.co/t/search-on-nested-failed-if-data-is-not-updated/31831>\
**Category:** Elasticsearch\
**Created:** [October 8, 2015, 9:19am UTC](https://discuss.elastic.co/t/search-on-nested-failed-if-data-is-not-updated/31831 "2015-10-08T09:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fregnier](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@fregnier](https://discuss.elastic.co/u/fregnier)\
**Post date:** [October 8, 2015, 9:19am UTC](https://discuss.elastic.co/t/search-on-nested-failed-if-data-is-not-updated/31831/1 "2015-10-08T09:19:19Z")

</div>

Hello,

I use this simple process to test elasticsearch on nested field:

1. curl -X PUT [http://localhost:9200/library](http://localhost:9200/library)
2. curl -X POST [http://localhost:9200/library/french/\_mapping](http://localhost:9200/library/french/_mapping) -d '{"french": {"\_all" : {"enabled" : true}, "properties": {"title":{"type":"string"},"cast":{"type":"nested","properties":{"firstname":{"type":"string"},"lastname":{"type":"string"}}}}}}'
3. curl -X POST [http://localhost:9200/library/french/2](http://localhost:9200/library/french/2) -d '{"doc":{"cast":[{"firstname":"John","lastname":"Bob"},{"firstname":"Bob","lastname":"John"}],"title":"The movie Two"}}'

Then i search my document with:  
curl -X GET [http://localhost:9200/library/french/\_search?size=10&from=0](http://localhost:9200/library/french/_search?size=10&from=0) -d '{"query":{"filtered":{"query":{"bool":{"must":[{"nested":{"path":"cast","query":{"bool":{"must":[{"match\_phrase":{"firstname":"Bob"}},{"match\_phrase":{"lastname":"John"}}]}}}}]}},"filter":{"not":{"regexp":{"\_data":".\*"}}}}}}'

It returns an empty hits ???

If i add a 4th step to update my data:  
4. curl -X POST [http://localhost:9200/library/french/2/\_update](http://localhost:9200/library/french/2/_update) -d '{"doc":{"cast":[{"firstname":"John","lastname":"Bob"},{"firstname":"Bob","lastname":"John"}],"title":"The movie Two"}}'

I've got my result ???

Does someone known why ?

Thanks

---

<div class="post-metadata">

**Author:** ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)\
**Post date:** [October 10, 2015, 7:45am UTC](https://discuss.elastic.co/t/search-on-nested-failed-if-data-is-not-updated/31831/2 "2015-10-10T07:45:46Z")

</div>

Your initial insert should not be nested inside a "doc" field. You were actually creating a document that looked like this:

```
 {"doc": {"cast": [...], "title": "The movie Two"}}

```

The query didn't return anything because everything was nested inside the "doc" field.

After the update it looked like the following (ie. it has a doc field and a cast field):

```
 {
     "cast": [...],"title": "The movie Two" },
     "doc": { "cast": [...] "title": "The movie Two"}
 }

```

The query worked after the update because the document had the data directly under "cast" now (as well as under "doc.cast".

Change your initial insert to the following and the query will work without you having to do an update:

```
curl -X POST http://localhost:9200/library/french/2 -d '{"cast":[{"firstname":"John","lastname":"Bob"},{"firstname":"Bob","lastname":"John"}],"title":"The movie Two"}'
```

---

<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 5, 2017, 11:45pm UTC](https://discuss.elastic.co/t/search-on-nested-failed-if-data-is-not-updated/31831/3 "2017-07-05T23:45:33Z")

</div>


