ElasticSearch study notes (2)-detailed explanation of addition, deletion, modification, and search operations

Develop a habit, like first and then watch!!!

1. Increase data manipulation

Add an index, type and document:

PUT /movie_index/movie/1
{
    
     "id":1,
  "name":"operation red sea",
  "doubanScore":8.5,
  "actorList":[  
{
    
    "id":1,"name":"zhang yi"},
{
    
    "id":2,"name":"hai qing"},
{
    
    "id":3,"name":"zhang han yu"}
]
}

Insert picture description here

After running, we will see these results:

Insert picture description here

Storing data in ElasticSearch is to store data in a decentralized manner, which is what we said 分片. Here, you may ask what is the role of sharding. Here we use a simple example to understand the role of sharding.

The first point is 安全that if we store data in a distributed manner, if the data in a certain shard fails, then obviously we can at least guarantee that the data of other shards is still available. This can guarantee to a certain extent Data security

Another point is 效率, for example, if we now have 1 ton = 1000 kg of fruit, there are apples, pears, bananas, etc., we will now sort it, put a bunch of apples, and a bunch of bananas. Heap, if I ask you to directly separate the 1000 catties of fruits, then it is obviously difficult, but if we first divide him into 100 portions, each 10 kg, then it is relatively simple to divide this way. Our efficiency has improved a lot.

It is precisely for the above two reasons that ElasticSearch chooses to store data in sharding.

After that, we will add two more pieces of information:

PUT /movie_index/movie/2
{
    
    
  "id":2,
  "name":"operation meigong river",
  "doubanScore":8.0,
  "actorList":[  
{
    
    "id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/3
{
    
    
  "id":3,
  "name":"incident red sea",
  "doubanScore":5.0,
  "actorList":[  
{
    
    "id":4,"name":"zhang chen"}
]
}

2. Delete data operation

The deletion of this operation is also very simple, only through the del keyword can be achieved.

DELETE movie_index/movie/2

Here we choose to delete the record with id 2 directly.
Insert picture description here
After the test is completed, we can indeed see that the data with id 2 has been deleted, and the data can be deleted. There is another point to note here is that the version of ElasticSearch 5.0 will 不再支持删除单个type了, If we execute the command, we will see the following error

Insert picture description here
It tells us that type cannot be deleted directly, but we can pass it POST命令来删除type下面所有的数据或者是直接删除index之后再重新创建type即可.

Here we will briefly look at how the post command performs the delete operation.

POST movie_index/movie/_delete_by_query
{
    
    
  "query":{
    
    
    "match":{
    
    
      "name":"red"
    }
  }
}

The "query" here represents our deletion condition. The entire code means to delete all records with the red keyword in the name of the movie type under the movie_index index node. Let's execute it here.

Insert picture description here
It can be seen that after the execution is completed, the two data containing the red keyword under our type have been deleted. In this way, we have already introduced the deletion operation.

3. Modify data operation

Finished delete, modify operation elasticSearch we look at, we all know elasticSearch is text-based, so the nature of the text is modified覆盖 , so here we put through to operation.

Before performing the modification operation, we first add the previously deleted data to the type of our index node.

After the addition, let's take a look at our modification operation, but before performing the modification operation, let's take a look at what we have said before about elasticSearch6 and the previous version. There is a difference between it and the previous version elasticSearch6只支持一个索引节点下面有且只能有一个type. Here we will test it. .

Insert picture description here

After testing, we can find that we can indeed find that this is indeed the case, and there can only be and only one type under one index node.

After verifying this again, let's take a look at our specific modification operation.

PUT /movie_index/movie_test/1
{
    
     "id":1,
  "name":"红海行动",
  "doubanScore":8.5,
  "actorList":[  
  {
    
    "id":1,"name":"张译"},
  {
    
    "id":2,"name":"海清"},
  {
    
    "id":3,"name":"张涵予"}
 ]
}

In fact, the essence of the operation is the same as the code of our previous add operation. Just pay attention to the id a little. So let's take a look at the result after we execute it.

Insert picture description here

4. Query data operation

After reading the modification operation, only our last query operation is left. Next, let's query the data

GET movie_index/_search

The operation of this statement means to find all the information under the movie_index node

search result:

Insert picture description here

Because we do not add any filter criteria after, so we calculate the correlation data points are each 1, after our inquiry process, we look back we finished adding conditions can detect changes in the correlation of the count points.

Then let's take a look at the various commands of elasticSearch about the query

GET movie_index/movie/_search
{
    
    
  "query": {
    
    }
  ,"from": 0
  ,"size": 20
  ,"highlight": {
    
    }
  ,"aggs": {
    
    
    "NAME": {
    
    
      "AGG_TYPE": {
    
    }
    }
  }
}

Insert picture description here

4.1-Keyword query

Let’s test the keyword query first

GET movie_index/movie/_search
{
    
    
  "query": {
    
    
"match": {
    
    
  "name": "red"
}
  }
}

Insert picture description here

Here we can see that the Field is similar to the field in our database, the latter is the content we need to match, and we can see that the correlation score of the two data queried is actually the same. Here we guess it may be passed The frequency of keywords in this field is used to calculate the relevance score.

Here we need to pay attention to one thing that is that the keyword in the keyword query cannot be empty. You can know what it means through the following picture
Insert picture description here
. You can understand through this picture. The keyword query does not support keywords as For an empty query, if the keyword is empty, then the query will directly report an error. Therefore, we must add the corresponding keyword. If there is no query without a keyword, we need to write it as follows, so There will be no errors.

Insert picture description here
The added piece of code means full match, and everyone should be able to understand it.

After the keyword query test is over, let's test the paging query again.

4.2-Paging query

There is essentially no difference between the paging query here and the paging query operation in mysql.

Just specify our starting point and page size.

GET movie_index/movie/_search
{
    
    
  "query": {
    
    
     "match_all":{
    
    }
  }
  , "from": 0
  , "size": 2
}

Insert picture description here

You can see that the total amount of data is three, but here we query it only shows the first two data, obviously the paging query has been executed successfully

4.3- The query content is highlighted

After testing the paging query operation, let's test the highlighting again. In fact, the highlighting here is to match the keyword query above. It is the same as our normal highlighting. Think about us. What will be highlighted? Obviously, it is what we need to find. So highlighting is generally matched with keyword queries.

code show as below:

GET movie_index/movie/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "name": "red"
    }
  }
  ,"highlight": {
    
    
    "fields": {
    
    
      "name": {
    
    }
    }
  }
}

The value in highlight needs to be filled in. We can fill in the name of the field in the keyword query. You can fill in additional fields, but we don't know what needs to be highlighted in the field, so it will not take effect, but it will not Reported an error.

Here we run it to see:

Insert picture description here
can be seen 高亮显示并不是直接将匹配的内容颜色发生改变,而是像HTML语言一样,添加了一个标记而已.

4.4- Aggregate functions

After testing the highlighting, let's finally look at the aggregate function.

Before looking at aggregate functions, we need to understand two concepts. One is that , and the other is 指标, so you may not understand it, and if we correspond to our usual relational database, everyone will know what they mean.

  • Buckets, similar to the group by in the database , are like dividing our data into Shanghai buckets, Jiangsu buckets, etc., or divided into male buckets and female buckets according to gender. The main function is to divide
  • Indicators are similar to various data analysis functions in the database , such as count, max, min, avg and other functions. The main function is to analyze the data.

At this point, our analysis of the basic operations of ElasticSearch addition, deletion, modification and query has been completed. If you think the article is okay, you can scan the QR code of the official account below to follow my official account. Newcomers need your support!!!

Insert picture description here

If you don't look at it, you look good!

Keep watching, you look better!

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/111880095