Elasticsearch create index example

On June 6th, 2013, posted in: Blog, Dev by

Tags: , ,

We have started using elasticsearch for indexing data to provide fast search for our webapps. It is a fantastic piece of software, but documentation is a little on the sparse side at the moment. After much learning and trial and error, here is the request we ended up sending with curl to create out index. Our index includes two types, person and company and some custom mappings. We thought it might be helpful for others trying to get started:

curl -XPOST localhost:9200/richtest2 -d '{

    "settings": {
    
        "number_of_shards": 5,
        
        "number_of_replicas": 2,
        
        "analysis": {
        	"filter": {
        	    "jnr_ngram_filter": {
            	    "type": "edgeNGram",
                	"side": "front",
                	"min_gram": 1,
                	"max_gram": 25
            	}
        	},	
            "analyzer": {
                "jnr_autocomplete_index_analyzer": {
                    "tokenizer": "icu_tokenizer",
                    "filter": ["icu_folding","jnr_ngram_filter"]
                },
                "jnr_autocomplete_search_analyzer": {
                    "tokenizer": "icu_tokenizer",
                    "filter": "icu_folding"
                }
            }
        }        
    },

    "mappings": {
    
        "person": {
            "_source": {
                "enabled": true
            },
            "properties": {
                "fullname": { 
                    "index_name" : "name",                   
               	 	"type" : "multi_field",
              	  	"fields" : {
                    	"autocomplete" : {
                    		"type" : "string", 
                    		"index" : "analyzed", 
                    		"index_analyzer": "jnr_autocomplete_index_analyzer",
                    		"search_analyzer": "jnr_autocomplete_search_analyzer"
                   	 	},
                    	"name" : {
                    		"type" : "string", 
                    		"index" : "not_analyzed"
                    	}
                    }
                },
                "city": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name" : "city"
                },
                "user_tags": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name" : "user_tag"
                }
            }
        },
        
        "company": {
            "_source": {
                "enabled": true
            },
            "properties": {
                "com_name": {
                    "index_name" : "name",
               	 	"type" : "multi_field",
              	  	"fields" : {
                    	"autocomplete" : {
                    		"type" : "string", 
                    		"index" : "analyzed", 
                    		"index_analyzer": "jnr_autocomplete_index_analyzer",
                    		"search_analyzer": "jnr_autocomplete_search_analyzer"
                   	 	},
                    	"name" : {
                    		"type" : "string", 
                    		"index" : "not_analyzed"
                    	}
                    }
                },
                "city": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name" : "city"
                },
                "industry": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name" : "industry"
                },
                "user_tags": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name" : "user_tag"
                }
            }
        }
    }
}'

Comments are closed.