Adding Mappings

Published on January 17, 2016 by

Now we are going to add a mapping to our ecommerce index. To do that, we are first going to remove the index and add it once again with the mapping. The reason why we are doing this, is that if you have data in an index, then you need to create a new index and add the data to it. If the index has no data, then there is no need to remove the index first.

DELETE /ecommerce

Now that the index has been removed, let’s create it again, but this time with mappings.

PUT /ecommerce
{
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "string"
        },
        "price": {
          "type": "double"
        },
        "description": {
          "type": "string"
        },
        "status": {
          "type": "string"
        },
        "quantity": {
          "type": "integer"
        },
        "categories": {
          "type": "nested",
          "properties": {
          	"name": {
          	  "type": "string"
          	}
          }
        },
        "tags": {
          "type": "string"
        }
      }
    }
  }
}

As you can see, mapping information is nested within a mapping property, where the product property is the name of the mapping type, which then contains its mapping information. Arrays are automatically detected in the JSON, so mapping fields as strings will support a string array.

That’s it! It is as easy as that to create an index with mapping information.

Meta Fields
Featured

Learn Elasticsearch today!

Take an online course and become an Elasticsearch champion!

Here is what you will learn:

  • The architecture of Elasticsearch
  • Mappings and analyzers
  • Many kinds of search queries (simple and advanced alike)
  • Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
  • ... and much more!
Elasticsearch logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.