# Creating index mappings with nested fields

**URL:** https://discuss.elastic.co/t/creating-index-mappings-with-nested-fields/133224
**Category:** Elasticsearch
**Created:** [May 24, 2018, 8:54pm UTC](https://discuss.elastic.co/t/creating-index-mappings-with-nested-fields/133224 "2018-05-24T20:54:58Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mehdi-gital](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mehdi-gital](https://discuss.elastic.co/u/mehdi-gital)
#### Post date: [May 24, 2018, 8:54pm UTC](https://discuss.elastic.co/t/creating-index-mappings-with-nested-fields/133224/1 "2018-05-24T20:54:58Z")

</div>

```
Hey guys, 

```

I've been using Elastic for about two weeks and today I ran into some issues that don't make sense.  
I'm going to ask multiple questions to build up toward my main question. Warning: long question!

I write my queries in Kibana under Elastic 5. Questions:

1. What's the deal with underscore? I believe in the OOP context, underscore indicates that the method is internal. What does it mean in a key in a JSON object? This is copied from the documentation.

Query:

```
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}

```

Response:

```
{
  "error": {
    "root_cause": [
      {
        "type": "invalid_type_name_exception",
        "reason": "mapping type name [_doc] can't start with '_'"
      }
    ],
    "type": "invalid_type_name_exception",
    "reason": "mapping type name [_doc] can't start with '_'"
  },
  "status": 400
}

```

1. Tried without underscore and it worked. 🙂

Query:

```
PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "data": {
          "type": "nested" 
        }
      }
    }
  }
}

```

1. Gave it some data:

Query:

```
PUT my_index/doc/1
{
  "group" : "doc",
  "data" : [
    { 
      "first" : "John",
      "last" : "Smith",
      "rating": 2,
      "limit": 3
      
    },
    { 
      "first" : "Alice",
      "last" : "White",
      "rating": 3,
      "limit": 3
    }
  ]
}

PUT my_index/doc/2
{
  "group" : "doc",
  "data" : [
    { 
      "first" : "Average",
      "last" : "Joe",
      "rating": 3,
      "limit": 3
      
    },
    { 
      "first" : "Mr",
      "last" : "Champ",
      "rating": 5,
      "limit": 3
    }
  ]
}

```

Now, time to give it a real query:

```
Get my_index/doc/_search
{
  "query": {
    "nested": {
      "path": "data",
      "query": {
        "bool": {
          "must": [
            {
              "wildcard": {
                "data.first": {
                  "value": "Aaaaaaa?"
                }
              }
            },
            {
              "range": {
                "vs.rating": {
                  "gt": 5
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

Response:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "group": "doc",
          "data": [
            {
              "first": "Average",
              "last": "Joe",
              "rating": 3,
              "limit": 3
            },
            {
              "first": "Mr",
              "last": "Champ",
              "rating": 5,
              "limit": 3
            }
          ]
        }
      },
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "group": "doc",
          "data": [
            {
              "first": "John",
              "last": "Smith",
              "rating": 2,
              "limit": 3
            },
            {
              "first": "Alice",
              "last": "White",
              "rating": 3,
              "limit": 3
            }
          ]
        }
      }
    ]
  }
}

```

I don't understand why it returns everything when it should, in fact, return nothing.

1. My real question is:  
What's the syntax for defining the fields that go inside my data field?

Here:

```
"data": {
          "type": "nested" 
        }

```

I was thinking something like this but it won't work:

```
"data": {
          "type": "nested",
          "properties": {
            "first": "text",
            "last": "text",
            "gender": "text,
            "rating": "integer",
            "limit": "integer"
          }
        }

```

Sorry for the super long question but most of the above is contrived code. What I really want in my work is to run a query as follows:

```
{
  "query": {
    "nested": {
      "path": "data",
      "query": {
        "bool": {
          "must": [
            {
             "wildcard": { "data.first": "A?"} 
            },
            {
             "range": {
               "data.value": {
                 "gt": "data.limit"
               }
             } 
            }
          ]
        }
      }
    }
  },
  "_source": ["data.first", "data.last", "data.value"]
}

```

Please note, I have no issues do that when giving it as a constant. Eg:  
"gt": 3  
However, I can't seem to reach "data.limit" to put on the right-hand side of "gt".

If you read this far, I truly appreciate that.

---

<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: [June 21, 2018, 8:55pm UTC](https://discuss.elastic.co/t/creating-index-mappings-with-nested-fields/133224/2 "2018-06-21T20:55:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
