Creating index mappings with nested fields

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. :slight_smile:

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.

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