ElasticSearch Note Series (4) - ElasticSearch Cluster Index Creation

RESTFUL-style API for ES

The API composition structure of ES is encapsulated in RESTFUL style API.

Let's take a look at the basic format of his API.

http://<ip>:<port>/<index>/<type>/<document id>

Every element in the API's URL is its concept. That is, its noun.

Verbs are distinguished by the http method:

GET/POST/PUT/DELETE

 

How to create an index

First, according to the introduction in the previous blog, open all ES one master and two slaves plus head.

You will find that the computer memory is exhausted immediately.

 

unstructured creation

We choose to click [Index], and then click [New Index]

We give the index a name, called house. As you can see, the number of shards and replicas can also be adjusted here. Here I choose 5 shards and 1 replica by default.

Click [OK], the creation is successful here.

We can see that the overview of the index has changed to the house index we just added. house is the index name, remember, this should be lowercase and cannot have double dashes (underscore and minus)

Then let's take a look at the overview. Here we can see that 0, 1, 2, 3, and 4 refer to the 5 shards of the house index, which are scattered on the master and two slaves.

You will notice that each shard number is two, because we just set the number of replicas to 1. Careful you must have noticed that in each shard, there is a thick box and a thin box. The thick square represents that this shard is the primary shard, and the thin square represents the backup of the shard, that is, the thin square is the backup shard of the thick square.

Here is another detail. We click the information menu button of the house index on the head plugin page and select [Index Information]

You can see the following:

Here we need to pay attention to one, the mappings attribute. This attribute represents structured information. If it is empty, it means that the secondary index is an unstructured index.

{
"state": "open",
"settings": {
"index": {
"creation_date": "1523543127792",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "yAZIunJJS_-OrN5JtCHuqA",
"version": {
"created": "5060899"
},
"provided_name": "house"
}
},
"mappings": { },
"aliases": [ ],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"KgGw0XboSK-iVa0M-ZYjIA"
,
"cKHd13tERbKD4czontrruQ"
],
"1": [
"V7rqZ1aERyyqaZQjE44WUA"
,
"2xwY2SNUQnOhDcyfnQZCwg"
],
"2": [
"yLi3b1-aT2mCR9wnRtJ64Q"
,
"HYw9u5W1SXWSe9ka5_oA4Q"
],
"3": [
"MlYordT7T_ys_s6EB1L9_w"
,
"vssLnGOqQ7K8YO3yFTHW1g"
],
"4": [
"OWATPEyLR0eLwG5PWUBkAg"
,
"XJYOtK68RraVQDXhtzYwbw"
]
}
}

So how to build a structured index?

(This article is from the blog post of oschina blogger happybks: https://my.oschina.net/happyBKs/blog/1795508)

Structured Index Creation

Click on [Match Query] on the page

Then we build a url to call an API of ES.

We add a type to the house index: apartment;

Then add a keyword _mappings to specify a mapping. (See why index names can't have double underscores, because some special-purpose keywords start with underscores)

The above is the API call format for adding a structured index to ES. Next, let's define a specific mapping structure in the format of json.

Let's enter json to map a text type for the property apartmentname of the apartment index.

We select the [Easy to read] checkbox and click [Validate JSON]

json is also attached:

{
  "apartment": {
    "properties": {
      "apartmentname": {
        "type": "text"
      }
    }
  }
}

We click [Submit Request] to prompt us that the creation was successful, as shown in the figure

Then we click back to [Overview].

Note: Be sure to click the [Refresh] button in the upper right corner, otherwise you will not be able to see the update effect of the structured mapping we just added.

We click [Index Information] in [Information] again, and then we can see the mapping information of the structured index.

{
"state": "open",
"settings": {
"index": {
"creation_date": "1523543127792",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "yAZIunJJS_-OrN5JtCHuqA",
"version": {
"created": "5060899"
},
"provided_name": "house"
}
},
"mappings": {
"apartment": {
"properties": {
"apartmentname": {
"type": "text"
}
}
}
},
"aliases": [ ],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"KgGw0XboSK-iVa0M-ZYjIA"
,
"cKHd13tERbKD4czontrruQ"
],
"1": [
"V7rqZ1aERyyqaZQjE44WUA"
,
"2xwY2SNUQnOhDcyfnQZCwg"
],
"2": [
"yLi3b1-aT2mCR9wnRtJ64Q"
,
"HYw9u5W1SXWSe9ka5_oA4Q"
],
"3": [
"MlYordT7T_ys_s6EB1L9_w"
,
"vssLnGOqQ7K8YO3yFTHW1g"
],
"4": [
"OWATPEyLR0eLwG5PWUBkAg"
,
"XJYOtK68RraVQDXhtzYwbw"
]
}
}

 

 

Use POSTMAN to call ES API to create an index

Friends who have written crawlers must have used postman, which is really a good tool for simulating various http requests.

We need to create an index here, please follow the service address and port of ES (note: it is the address and port of ES, not the head plugin)

The restfult api style API uses the http put protocol to create indexes.

Then we need to submit a request for body position json. So choose raw for body, then choose JSON format. In postman, the software will automatically monitor for you whether the json you will enter conforms to the grammar rules. In the head plugin, you also need to pay attention to the format yourself.

 

What we create is the rental housing index. Structured indexes of course need to define a mapping. Of course, we first define the number of shards and replicas of the index in the settings, we just write 3 and 1 by default here.

Then we do the index mapping definition mappings. The format is as follows.

Here we create its mapping information for the community type. The community cell has the communityname cell name mapping attribute, and the attribute value type is text; the value type of the city where the attribute city is located is the keyword type keyword.

The difference between text and keyword:

Keyword: When storing data, it will not be indexed by word segmentation

text: When storing data, it will automatically segment words and generate an index (this is very smart, but it is useless in some fields, so using text for some fields is a waste of space).

The age property is an integer, note that it is lowercase.

Build Date The creationdate property is of type date.

Attributes of type date, you can specify the format format.

If multiple formats are included, the different types of format description expressions in the attribute value can be separated by double vertical bars ||.

It also supports timestamp format, here we use epoch_mills to represent.

{
	"settings":{
		"number_of_shards":3,
		"number_of_replicas":1
	},
	"mappings":{
		"community":{
			"properties":{
				"communityname":{
					"type":"text"
				},
				"city":{
					"type":"keyword"
				},
				"age":{
					"type":"integer"
				},
				"creationdate":{
					"type":"date",
					"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}
		},
		"shop":{
			
		}
	}
}

We created a shop type for the rent index, here we don't define any mappings.

After that we submit the request.

Note that it is a put protocol request, and here json submits various types of mappings for an index, so please bring the index name rent in the requested url, otherwise an error will be reported as follows:

 

The request was successful, and the structured index was successfully created:

 

We refresh the head home page at this time and check:

We found that the rent index has been established, and a structured index with a strategy of 3 shards and 1 replica has been created on our ES cluster with one master and two slaves.

We click [Information] to view [Index Information]:

 

{
"state": "open",
"settings": {
"index": {
"creation_date": "1523632189666",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "lUrZ_KHtQcuG33MFNivX-A",
"version": {
"created": "5060899"
},
"provided_name": "rent"
}
},
"mappings": {
"shop": { },
"community": {
"properties": {
"creationdate": {
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type": "date"
},
"city": {
"type": "keyword"
},
"communityname": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
},
"aliases": [ ],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1
},
"in_sync_allocations": {
"0": [
"CCMRsr2uSQmBj4a4GQisFQ"
,
"0RFsy8yXT6WM25oL8n5gew"
],
"1": [
"2hf4wI1BThSOHCfNqxIFQw"
,
"EUt4DmXeQa-OOFTZC9jP4g"
],
"2": [
"8z1oyQ4WSui8MZvQn7xYTg"
,
"vEbuzvxzTW2y_mokZWQSig"
]
}
}

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324432114&siteId=291194637