Elastic search: simple addition, deletion, modification and search

Keywords: Programming Spring less

Today, let's talk about the addition, deletion, modification and query of ES, which is helpful to understand the code of spring boot integration es next week.

First of all, we create an index of EST first, and then if all the screenshots are taken, the reading fluency is too low, so we will use the shorthand form [request type URL] next. The following diagram. To abbreviate as

PUT localhost:9200/estest
RqeustBody:
    { "settings": { "number_of_shards": 1 }}
ResponseBody:
 {
   "acknowledged": true,
   "shards_acknowledged": true,
   "index": "estest"
 }

Then we add a piece of data to a user's type. The first way is to let ES automatically create an ID primary key. If we want to use our own primary key, we can take a look at the second way.

Method 1:
POST localhost:9200/estest/user/
RqeustBody:
{
    "name": "Zhang San",
    "age": 17,
    "name_and_age": "Zhang San 17",
    "is_man": true,
    "birthday": "2019-01-01"
}
ResponseBody:
{
    "_index":"estest",
    "_type":"user",
    "_id":"-plY_HABLAPQJei9q-lc",      //Return to ID focus
    "_version":1,
    "result":"created",
    "_shards":{
        "total":2,
        "successful":1,
        "failed":0
    },
    "_seq_no":0,
    "_primary_term":1
}

//Method two:
POST localhost:9200/estest/user/
RqeustBody:
{
    "name": "Li Si",
    "age": 17,
    "name_and_age": "Li Si 17",
    "is_man": true,
    "birthday": "2019-01-01"
}

ResponseBody:
{
    "_index": "estest",
    "_type": "user",
    "_id": "1",                 //Return to ID focus
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

Now I want to DELETE Li Si. We use DELETE request, add ID after index and type to finish.

DELETE localhost:9200/estest/user/1

However, if we want to delete the data with the specified name, for example, we want to delete the data of Zhang San, we can use the following request.

POST localhost:9200/estest/user/_delete_by_query
RqeustBody:
{
  "query": {
    "match": {
      "name": "Zhang San"
    }
  }
}

Let's insert three pieces of data again. Now, if you want to update the age of Zhang San, you can use method 1, add "update by query" after the index and type, and then add the query criteria.. The second way is to update the data according to the ID.

Method 1:
POST localhost:9200/estest/user/_update_by_query
RequestBody: 
{
  "script": {
        "source":"ctx._source.age=18;"
  },
  "query": {
    "match": {
    "name":"Zhang San"
    }
  }
}
//Method two
POST localhost:9200/estest/user/_5lp_HABLAPQJei9cuke
RequestBody:
{
    "birthday": "2019-01-01",
    "name_and_age": "Zhang San 17",
    "name": "Zhang San",
    "is_man": true,
    "age": 18
}

Finally, the addition, deletion and modification are over. Although I just introduced the simple usage, I feel tired. Ah, this query is the key, hateful.

Now we need to query the user Zhang San. How can we do that?

GET localhost:9200/estest/user/_search
RequestBody: 
{
  "query": {
    "match": {
    "name":"Zhang San"
    }
  }
}

Well, finally, match, match. Match means match, which is very interesting, because there is a word breaker in ES, which can fuzzy match. Now we put in some other Zhang San data. It can be seen that the matching degree of the name Zhang San 123123 is the lowest, only more than 0.12, and the others are 0.15.

Then if we want to match exactly, we can use trim. In the previous paragraph, we introduced the word segmentation system. Because the index was not mapped at the beginning, ES automatically segmented the name field, which made it impossible for us to accurately match the data named Zhang San. So use age to test term, although match will have the same effect. There is no difference between match and term in terms of documents. Match is more applicable.

GET localhost:9200/estest/user/_search
RequestBody: 
{
  "query": {
    "match": {
    "age": 18
    }
  }
}

Now check with people over 15. Because it is in the form of request body, we can't use the symbols of <, < =, > =, > instead of these symbols.

gt

greater than

gte

Greater than or equal to

lt

less than

lte

Less than or equal to

GET localhost:9200/estest/user/_search
RequestBody: 
{
   "query": {
     "range": {
      "age":{
       "gt": 15
      }
     }
   }
}

If you want to query whether a field is empty, you can use exists. missing has been removed from ES5.

GET localhost:9200/estest/user/_search
RequestBody: 
{
   "query": {
     "missing": {
       "field":    "age"
     }
   }
}

Finally, I've finished writing this ES query. It took me two nights to finish it on Wednesday. Then I'll take a look at the Spring Security source code today and analyze it. It's estimated that it will be delayed to next Wednesday. I'm here to say that DSL is really hard to use!

Posted by escabiz on Sat, 21 Mar 2020 06:17:18 -0700