3

I'm trying to use a json file to define the default mapping for each index. This is what I am trying to do:

/usr/share/elasticsearch/config/default-mapping.json

{
    "item": {
        "properties": {
            "uuid": {"type": "string", "store": "yes", "index": "no"},
            "title": {"type": "string", "store": "yes", "boost": 5,
                      "index": "analyzed", "analyzer": "english"},
            "description": {"type": "string", "store": "yes", "boost": 3,
                            "index": "analyzed", "analyzer": "english"},
}

When I try and query my index_test elasticsearch index I get this:

curl -XGET 'http://...:9200/index_test/_mapping'
{"index_test":{"mappings":{}}}

I used the documentation found on here.

https://www.found.no/foundation/elasticsearch-mapping-introduction/

halfer
  • 19,824
  • 17
  • 99
  • 186
Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • This one will help you http://stackoverflow.com/questions/18855318/where-do-i-put-the-mapping-files-for-elasticsearch – Rob May 17 '15 at 13:10
  • @Rob Thank you for the reply, and I did see that before, but I'm not sure how it will help me – Jimmy May 17 '15 at 13:16

3 Answers3

3

You can create index template with default configuration(index settings, mapping etc) for your indices.

  1. To do this, change content of default-mapping.json file to something like:

    {
        "template_1" : {
            "template" : "*",
            "mappings" : {
                "type" : {
                    "properties" : {
                        "uuid" : {
                            "type" : "string",
                            "store" : "yes",
                            "index" : "no"
                        },
                        "title" : {
                            "type" : "string",
                            "store" : "yes",
                            "boost" : 5,
                            "index" : "analyzed",
                            "analyzer" : "english"
                        },
                        "description" : {
                            "type" : "string",
                            "store" : "yes",
                            "boost" : 3,
                            "index" : "analyzed",
                            "analyzer" : "english"
                        }
                    }
                }
            }
        }
    }
    
  2. Move default-mapping.json file to /usr/share/elasticsearch/config/templates directory
  3. Create a new index

    POST /newindex
    
  4. Mapping of newly created index:

    {
        "newindex" : {
            "mappings" : {
                "type" : {
                    "properties" : {
                        "description" : {
                            "type" : "string",
                            "boost" : 3,
                            "store" : true,
                            "analyzer" : "english"
                        },
                        "title" : {
                            "type" : "string",
                            "boost" : 5,
                            "store" : true,
                            "analyzer" : "english"
                        },
                        "uuid" : {
                            "type" : "string",
                            "index" : "no",
                            "store" : true
                        }
                    }
                }
            }
        }
    }
    

Hope this helps you.

Rob
  • 9,664
  • 3
  • 41
  • 43
2

assuming you have create the index index_test already, you need to execute the following (as in your link):

$ curl -XPUT 'http://localhost:9200/index_test/my_type/_mapping' -d '
{
  "my_type": {
    "properties": {
      "uuid": {
        "type": "string",
        "store": "yes",
        "index": "no"
      },
      "title": {
        "type": "string",
        "store": "yes",
        "boost": 5,
        "index": "analyzed",
        "analyzer": "english"
      },
      "description": {
        "type": "string",
        "store": "yes",
        "boost": 3,
        "index": "analyzed",
        "analyzer": "english"
      }
    }
  }
}

'

The important bit is to note that the field in the payload corresponds to the type-name (generically I took my_type).

Verify with a GET on url:9200/index_test/_mapping/my_type?pretty.

regards fricke

fricke
  • 1,330
  • 1
  • 11
  • 21
  • Really good answer. I would like to add one thing. You might not find templates directory into elasticsearch/config. You should create one and put your mapping file into it. – vvs14 May 05 '16 at 06:04
0

your JSON document is not valid. it should be :

{
    "my_type": {
        "properties": {
            "uuid": {
                "type": "string",
                "store": "yes",
                "index": "no"
            },
            "title": {
                "type": "string",
                "store": "yes",
                "boost": 5,
                "index": "analyzed",
                "analyzer": "english"
            },
            "description": {
                "type": "string",
                "store": "yes",
                "boost": 3,
                "index": "analyzed",
                "analyzer": "english"
            }
        }
    }
}

i advise you to always check your JSON documents when you modify your mapping. ElasticSearch will just ignore your mapping and not prompt you any error when you send a non valid JSON document as mapping

JSON validator site: http://jsonlint.com/

Samy Elaiassi
  • 516
  • 3
  • 12