Search code examples
elasticsearchnestelasticsearch-dsl

Are nested queries used for object mappings in ElasticSearch?


Say we have a document model in ElasticSearch, which has a string (registration) and an object datatype called currentname, which then has a value called name. I have added model JSON in the bottom.

How do you query this?

Since the metadata item is not using the nested keyword, I assume we can get the data without the nested. However, since the metadata do have a properties value, I am unsure.

My attempts:

I have tried the simple model (NEST):

        var res2 = client.Search<dynamic>(s =>
             s.Index("myindexname").AllTypes().
                Query(q => q.
                    Bool(b => b.
                        Must(mu => mu.
                            Term(te =>
                                te.Field("metadata.currentname.name").Value(query))))));

This doesn't return any documents.

However, if I have to use nested queries, I don't completely understand why (is it because these objects are basically a different index?). And how would the DSL (or NEST code) look to do the right call?

Document model:

properties: { 
    Company: {
        properties: {
           registration: {
              type: "string"
           },
           metadata: {
               currentname: {
                   properties: {
                      name: "string"
                    }
                }
            }
        }
    }
}

Solution

  • Nested query are used only with nested datatypes