1. Index
1.1 Create Index
Creating an index in ElasticSearch is like creating a database in a relational database.
First we open postman to send the request.
http://127.0.0.1:9200/shopping

If you get this response, it has been successfully created.

1.2 View Index
To view the shopping index, the command remains the same, except that you have to change the request method to a GET request.


1.3 View All Indexes
Send GET request:
http://127.0.0.1:9200/_cat/indices?v
Since we have only created one index, shopping, only one index will be displayed.

where the meaning of the table header:
Header | Meaning |
health | Current server health status. Green–Clusters are complete; Yellow–Single point is normal, but cluster is incomplete; Red–Single point server not working properly. |
status | Index open, closed status |
index | Index name |
uuid | Uniform index number |
pri | Number of master splits |
rep | Number of replicas |
docs.count | Number of availiable documents |
docs.deleted | Document deletion status (logic deletion) |
store.size | Size of the overall space occupied by the master and slave splits |
pri.store.size | Size of the primary split |
1.4 Delete Index
The path remains the same and change the request method to DELETE to remove the shopping index.

2. Document Operation
2.1 Create Document
To create documents, you need to use POST requests. Why is this different from creating indexes? Because creating a document generates a random id each time, and the id is different each time, whereas PUT requests are idempotent, so you can’t use PUT requests.
http://127.0.0.1:9200/shopping/_doc
OR
http://127.0.0.1:9200/shopping/_created
request body:
{
"title":"Apple",
"category":"Mobile Phone",
"images":"http://www.gulixueyuan.com/apple.jpg",
"price":1999.00
}
Sometimes it is very inconvenient to use automatically generated ids. So when creating it, we can customize the id for it.
http://127.0.0.1:9200/shopping/_doc/1001
If this works, the id will be the same each time, so we can also use a PUT request.
2.2 Query Document
The command remains the same, just change it to a GET request.
2.3 Update Document
http://127.0.0.1:9200/shopping/_update/1001
request body:
{
"doc":{
"price":3000.00
}
}
2.4 Delete Document
The command is the same as Create Document and just change the request method to DELETE.
3. Query
3.1 Full Query
GET request
http://127.0.0.1:9200/shopping/_search
3.2 Conditional Query
http://127.0.0.1:9200/shopping/_search?q=Apple
http://127.0.0.1:9200/shopping/_search?q=title:Apple
conditions followed after q
Or, you can put condition in the request body.
{
"query":{
"match":{
"title":"Apple"
}
}
}
3.3 Paged Query
{
"query":{
"match_all":{
}
},
"from":0,
"size":2
}
from means starting from which page and size means how many pages of data are displayed.
3.4 Multi-Condition Query
{
"query" : {
"bool" : {
"must" : [
{
"match" : {
"title":"Apple"
}
},
{
"match" : {
"price":1999.00
}
}
]
}
}
}
Must will seek for data where all conditions are met (equivalent to AND).
If you change must to should, you will find data that satisfies one or more conditions (equivalent to OR).
0 Comments