Author: Medcl

Turn the link

Basic concept of Elasticsearch

Original Medcl search 2019-06-27

Zhang Fei brought his notebook to the meeting room because Guan Yu is going to talk about Elasticsearch. Other members of Guan Yu’s department also came, in fact, only Guan Ping and Zhou Cang (next door village hard pull) two people. There is no way, at the beginning of the business, xuzhou straw sandals net altogether so few guns, this general manager Liu today went to the gate of the city to set up stalls to do offline direct selling business, because the online website is not still under development.

Guan yu the projector with good, the opening: “brothers, the eldest brother, wing, and I met Mr Zhuge of thatched cottage a few days ago, Mr Zhuge gave us a very good tool is introduced, called Elasticsearch (hereinafter referred to as: ES), which can be used to improve our site search, very promising, and today I introduce you to everybody.”

Zhang Fei nodded: “Brother, can you tell us the difference between Elasticsearch and our database?”

“Three younger brother, you do not follow routine card, I have not begun to speak, you ask questions, I have not prepared PPT yao?”

“Second elder brother, don’t whole these fancy of, eldest brother is not here again today, I drank with brothers last night drink big, headache of want to die, simple point, can simple point.”

“Get the fuck out of here.” Guan Yu gas of blush neck thick, zhang Fei a listen to honest, “be be be, I don’t talk line.”

ES compared to a database

“The difference between Elasticsearch and our existing database, let’s start with our database.”

“Our database is mainly used to store the business data of the straw shoe network, such as orders, commodity introduction, membership and other information, the database is composed of the structure of the table, a table contains a number of records, a table has a number of columns, each column is a field. There are all kinds of relationships between them, so traditional databases of this kind are also called relational databases, because we can do joint queries of multiple tables. Guan Ping you last write hundreds of lines of SQL still remember, very slip ah, you are writing prose in SQL ah.

“Just a little bit slow, ha ha.” Guan Ping scratched his head and said sheepishly.

“Oh, funny, that’s pretty slow. Let’s see what Elasticsearch does first. It is a special search engine, the Han search that everybody uses cao aman company home commonly, it is the technology of search engine, principle of underlying technology is same. Select * from Elasticsearch; select * from Elasticsearch; select * from Elasticsearch; select * from Elasticsearch; select * from Elasticsearch;

Elasticsearch has a similar concept to a database table, but it is called an Index. Each record in an Index is called a document, and each document contains multiple fields. Elasticsearch does not support arbitrary Join queries between indexes. It also does not support transaction operations of the database, which cannot meet scenarios requiring strong data consistency.”

Elasticsearch and database are not the same thing, but these transaction consistency are basic requirements, Elasticsearch does not support.

“Yes, we didn’t introduce Elasticsearch to replace our current database, just to improve our search query performance. Elasticsearch is a JAVA program that runs on top of the JVM virtual machine and is cross-platform.”

RESTful + HTTP + JSON

“JAVA ah, we don’t know JAVA ah!” Zhang Fei shouted again, “Learning a new language, isn’t it cheating?”

“What’s the rush?” “Even though Elasticsearch is written in JAVA, it provides a RESTful API, so using the standard HTTP interface to access Elasticsearch, You can do this without using the Java language. It’s kind of cross-linguistic.”

“Wow, I’m so busy with all the new stuff on the front end. React, Angular, and Vue are exhausting me. Don’t make me learn JAVA anymore.” Zhang Fei was relieved at last.

“In addition, Elasticsearch SDK is available for various languages, so you don’t have to worry about language calls, just encapsulate an API call, splicing out an HTTP request. The input and output of Elasticsearch are JSON data structures, which is a common JavaScript data structure, and I think He’s familiar with it.”

“JSON of course skilled, tonight who ask me to drink, I will teach him” zhang Fei one face satisfied.

“Well, everyone knows that, and you have to teach it, let me show you an example of creating an index in Elasticsearch.” Guan Yu quickly switched a slide that had been prepared:

POST index/doc / 1 {" name ":" guan yu ", "age" : 32, "address" : "201 xuzhou avenue", "date" : "2018-11-11"}Copy the code
Zhang Fei looked excited: "Well, JSON this good, concise and easy to understand, but also support complex structure expression, an object contains several attributes, attributes can be another JSON object, used to describe business objects is really very convenient. This example of double Eleven is also more consistent with the identity of the second brother."Copy the code

Distributed and scalable

“Looking for smoke is, you are not the same”, finished talking about the difference between ES and database, give up the misgiving of language call, Guan Yu continued to say.

“Our business has just started and we don’t have much data, but our brother’s goal is to make our xuzhou straw shoes nationwide. You think about how many feet we have in the country, if everyone’s feet are wearing our straw sandals, how much business ah, that order, that customer information is not the sea to go, so we have to consider the follow-up expansion and upgrading.

At this time from the door came in a cleaning aunt, “come to come, lift feet, let a let”.

Affected by the sudden interference, Guan Yu’s face a little displeased, helpless, cleaning aunt is his mother.

“Elasticsearch supports cluster deployment. Each process of Elasticsearch is called a Node. If you start another Node, they automatically form a cluster with each other.”

“Cows have heard of flocks of chickens and ducks.” The old woman looked puzzled. “Xiao Guan, today call brothers at home to have dinner and then go.”

“This big niang, you sweep over withdraw good, really of, what also don’t understand.” “Son of a bitch, I’ll get you later.”

Guan yu continued, “Clustering means that servers work together to accomplish a task, that is, they can share server resources and communicate with each other. In Elasticsearch cluster, you can access the index information of the entire cluster by accessing the RESTful API port of any node, and you don’t need to worry about the distribution of the data. In addition, you can automatically distribute the data and handle the failure of one node. Other nodes can also continue to provide external services, but also to ensure that the data is not affected by failure, do not lose data, isn’t it amazing?”

“Well, superior, superior! Several men spoke in unison.

“Elasticsearch indexes are organized by shards. There are multiple shards at the bottom of an index, distributed by shard in a cluster. Such distribution mode, with database Sharding, the same principle, the purpose is to avoid a single index is too large, split into several fragments can solve the problem of a single large index.

If we want to support search growth in the future, we only need to add Elasticsearch nodes to add resources to the cluster. Elasticsearch will also automatically migrate some data from the old machine to the new node to make full use of the hardware resources and increase the processing power. I’ve heard of hundreds or thousands of nodes being deployed in the West, on such a scale, boy.”

QueryDSL, queries, and aggregation analysis

“How is Elasticsearch going to work?” “I just heard so much about it,” he said. “It feels awesome. Zhang Fei said a little anxiously.

“Elasticsearch is a RESTful API so you don’t have to worry about the details of the bottom layer. To create an index, you POST a PIECE of JSON data into it, and to query, you write a GET request for the query. In Elasticsearch, there is a special query expression called QueryDSL, which is also described in JSON and supports many query types.

In addition, we can also perform statistical analysis on our website visitors, such as the time of day they visited, state and county, and which pages they clicked on. This is done in Elasticsearch via a feature called “aggregation”.

The result of Elasticsearch is a JSON file, so you can display it on the front end. This piece is mainly your work of the third brother.”

“So very good, this small case, to an, the front end for an that is not a minute of things.” Zhang Fei said, patting her chest.

Plug-ins and Extensions

“There are a lot of Elasticsearch features, so you guys need to check them out. We don’t have all the things we need, but Elasticsearch has a very good plugin mechanism, and it has a lot of features that come with it.

You can also write plug-ins to extend the network communication from the previous API to the bottom layer. Some requirements that Elasticsearch does not provide, you can also write plug-ins to achieve this, but the plug-in needs to be written in JAVA. The other thing is the deployment and installation of Elasticsearch.

“Yes, General Kwan!” Zhou Cang and Guan Ping nodded together.

“Second elder brother, I seem a little hungry, your home have what eat of not.”

“Fists or not?”

“Two, please.”

“Oh, no.”

.

After selling his fifth pair of straw sandals at the gate of the city, Liu Bei looked up at the round moon in xuzhou and thought that tomorrow would be another fine day.

If you want to know what happened next, listen next time.