Dynamic Mapping for Nested json documents in elasticsearch


(Malini) #1

Hi All,

We have a requirement to store nested json objects ( depth can be upto 5 levels) in elastic search. I have the current mapping

 "dynamic_templates": [
    {
        "nested_feature": {
            "match": "payload",
            "mapping": {
                "type": "object"
            }
        }
    },
    {
        "nested_template": {
            "path_match": "payload*.*", - match everything under payload
            "match_mapping_type": "object", - if the dynamic mapping type is 'object' , make it 'nested'
            "mapping": {
                "type": "nested"
            }
        }
    },

What I have in the dynamic templates will match ANY object and map it to a nested type. While it would preserve relationships of objects in arrays, it also means non-array objects will be treated the same as array objects and nested queries would be required for everything in the json object hierarchy.

Can anyone suggest a mapping to make non array objects as "Object" datatype and array objects as "Nested" datatype.

Note:All the fields inside json are dynamic.

Ex :json data

   {
     "ts": "2014-07-30T07:14:06.840",
     "pid": 2104,
     "tid": "1e88",
     "sev": "info",
     "req": "U9j9rgqgYVgAAA@kHr8AAAHs",
     "sess": "B84F19DEC88D4362B9164D87A687CFBC-0:1",
     "site": "Default",
     "user": "nsawant",
     "k": "begin-query",
     "v": {
        "protocol": "c4561a0",
        "query": "(restrict (aggregate (select (project (table [Extract].[Extract]) (([yr:Date:ok] (year [Date])) ([none:Region:nk] [Region]) ([none:Country / Region:nk] [Country / Region]))) (= [none:Region:nk] \"Asia\")) (([none:Country / Region:nk] [none:Country / Region:nk])) (([avg:P: Population (count):ok] (average [P: Population (count)])))) ([none:Country / Region:nk] [avg:P: Population (count):ok]))"
     }
  }

Ex:json data

{
"data": [
{
  "id": "926",
  "type": "news-items",
  "attributes": {
    "title": "Teenager missing",
    "subtitle": "",
    "body": "The Police Service is seeking the assistance of the public in locating a missing teen.\r\n<br>Fourteen year-old Mariah Modeste of Purcell Street in Vistabella, was last seen on Monday 2nd January at Salvia Lane in Pleasantville.\r\nShe is of mixed descent, 5 feet 5 inches tall and of slim build.\r\n<br>Anyone with information on her whereabouts is asked to contact 800-TIPS or any police station.",
    "created-at": 1483726807,
    "photo-thumb-url": "http:\/\/s3.amazonaws.com\/ttrn-api-photos\/news_items\/photos\/000\/000\/926\/thumb\/Screen_Shot_2017-01-06_at_2.14.12_PM.png?1483726806",
    "photo-dominant-color": "#2f2119",
    "photo-url": "http:\/\/s3.amazonaws.com\/ttrn-api-photos\/news_items\/photos\/000\/000\/926\/original\/Screen_Shot_2017-01-06_at_2.14.12_PM.png?1483726806",
    "straphead": "",
    "photo-caption": "",
    "category": "News"
  }
},
{
  "id": "925",
  "type": "news-items",
  "attributes": {
    "title": "National Security Minister jeered at crime march",
    "subtitle": "",
    "body": "<div>Loud chants of \u201cDo your job\u201d and &nbsp;\u201cmust go\u201d greeted &nbsp;National Security Minister Edmund Dillon &nbsp;when he &nbsp;stopped by to show solidarity with &nbsp;the Side by Side We Stand &nbsp;initiative at the Port of Spain Waterfront on Friday afternoon where citizens were encouraged to come together to stand up for the safety of women and children.<\/div><div>As the angry citizens lashed out at Mr Dillon they called on him to answer questions. But, he simply spoke with the media to explain his decision to support the move saying \u201c I expect we all have to do whatever we can to fight crime and not just depend on the police to do it.<\/div>",
    "created-at": 1483723980,
    "photo-thumb-url": "http:\/\/s3.amazonaws.com\/ttrn-api-photos\/news_items\/photos\/000\/000\/925\/thumb\/Screen_Shot_2017-01-06_at_1.25.26_PM.png?1483723980",
    "photo-dominant-color": "#2a231e",
    "photo-url": "http:\/\/s3.amazonaws.com\/ttrn-api-photos\/news_items\/photos\/000\/000\/925\/original\/Screen_Shot_2017-01-06_at_1.25.26_PM.png?1483723980",
    "straphead": "",
    "photo-caption": "",
    "category": "News"
  }
}
]
}

(James Baiera) #2

Moving this to Elasticsearch forum as it seems more appropriate for this post.


(system) #3

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