0

I have a JSON file which looks like this:

{'name':'John Doe',
 'work':[{
 'company':'Google',
 'duration':'2 years',
 'desc':'some long text'},
 {
 'company':'Yahoo',
 'duration':'3 years',
 'desc':'some long text'}]}

There can be a number of entries in work from 0 to unknown. How do I define the elasticsearch mapping such that it gets stored automatically.

I tried something like this:

from elasticsearch import elasticsearch
mapping:{    
 "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
},
"man":{
   "properties": {
       "name":{"type":"text"},
       "work":{
           "properties": {
               "company" : {"type":"text"},
               "duration": {"type":"text"},
               "desc": {"type":"text"}
               }
            }
        }
   }
}

es = Elasticsearch()

 if es.indices.exists("test"):
    es.indices.delete(index="test")

 es.indices.create("test")
 es.indices.put_mapping(index="test", doc_type="man", body=mapping)

Please help me on this. Thanks.

anshaj
  • 293
  • 1
  • 6
  • 24
  • Your mapping looks fine. Did you try it? What happened? – Val May 08 '17 at 09:55
  • 1
    Could you give more info on what the problem is? Are you trying to create nested object? Elastic creates mapping automatically you don't need create them explicitly if you are not using special analyzers or fields? – Filip Cordas May 08 '17 at 09:58
  • @FilipCordas- yes I'm trying to create a nesting object – anshaj May 08 '17 at 10:14
  • @Val - I tried it and it ended up manipulating the mapping – anshaj May 08 '17 at 10:14
  • @Val - I want to know if there are multiple values for "man" and inside that , there can be multiple companies. How do i do that ? – anshaj May 08 '17 at 12:14

1 Answers1

1

In elasticsearch every field can be multi valued. So you don't have to do anything specifically to hold list of values. It will merged automatically with other fields.
Only thing that you should consider is Array of objects are flattened at index time. So you will lose any correlation. Your data will be indexed as work.company=["google","yahoo"] for all fields in your object. So you can not query for people who worked in google for certain period. If you have that use case then you should go for nested objects. Just make the type nested in mapping and it will do. Nothing needed to specify array type.

Krrish Raj
  • 1,505
  • 12
  • 28