Search on nested failed if data is not updated

Hello,

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

  1. curl -X PUT http://localhost:9200/library
  2. curl -X POST 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 -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 -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 -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

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"}'