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/

2
  • This one will help you stackoverflow.com/questions/18855318/… Commented May 17, 2015 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 Commented May 17, 2015 at 13:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.