API usage of ElasticSearch
Article directory
-
- API usage of ElasticSearch
- 1. Index operation
- 2. Document operation
-
- 2.1 New document (automatically generate ID)
- 2.2 Add new document (specify ID)
- 2.3 Modify the document (overwrite modification)
- 2.4 Modify the document (partial modification)
- 2.5 Delete documents
- **2.5 Conditional query document**
-
- 2.5.1 Query all documents under the document
- 2.5.2 Query index data according to id
- 2.5.3 Word segmentation matching
- 2.5.4 Result highlighting
- 2.5.5 Exact match (no word segmentation)
- 2.5.6 Match query
- 2.5.7 match_all query all
- 2.5.7 Pagination condition query
- 2.5.8 Partial display of query fields
- 2.5.9 Sorting by field
- **2.6 Multi-condition query document**
- **2.7 Aggregate query**
1. Index operation
The ones in ES 索引
can be understood as the ones in MySQL数据库
1.1 Create an index
By PUT
requesting the interface of ES
Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}
The newly created index shopping2 can be seen in ES-head
1.2 Get index information
By GET
requesting the interface of ES
Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}
1.3 Get all index information
By GET
requesting the interface of ES
Format: http://127.0.0.1:9200/_cat/indices
1.4 Delete index
By DELETE
requesting the interface of ES
Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}
At this time, when you go to ES-head to check, you can no longer see this index.
1.5 Create index mapping
The process of creating index mapping can be understood as DML in SQL (creating table structure)
By PUT
requesting the interface of ES
Format: http://127.0.0.1:9200/{index}/_mapping
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
2. Document operation
Each data unit in ES is called a document (doc), but it is actually similar to data in a database.
2.1 New document (automatically generate ID)
By POST
requesting the interface of ES
Format: http://127.0.0.1:9200/{index}/_doc http://127.0.0.1:9200/{index name}/_doc
{
"uuid":"mac book pro",
"price":16000
}
At this point, the newly added data can be seen on the ES-head.
2.2 Add new document (specify ID)
By POST
requesting the interface of ES
Format: http://127.0.0.1:9200/{index}/_doc /{id}
At this point, you can see the newly added data with the specified ID on the ES-head.
2.3 Modify the document (overwrite modification)
Request PUT
the interface of ES (override the entire id by id)
Format: http://127.0.0.1:9200/{index}/_doc/{id}
ES-headView Results
2.4 Modify the document (partial modification)
Request PUT
the interface of ES (overridden by id part)
Format: http://127.0.0.1:9200/{index}/_update/{id}
ES-head view results
2.5 Delete documents
DELETE
Request the ES interface through (delete by id and index)
Format: http://127.0.0.1:9200/{index}/_doc/{id}
At this time, you can see that this piece of data has been deleted in ES-head
2.5 Conditional query document
2.5.1 Query all documents under the document
By GET
requesting the interface of ES
Format: http://127.0.0.1:9200/{index}/__doc/_search
2.5.2 Query index data according to id
By GET
requesting the interface of ES
Format: http://127.0.0.1:9200/{index}/_doc/{id}
2.5.3 Word segmentation matching
By GET
requesting the interface of ES
(It will find out all the participle content that contains the search)
Format: http://127.0.0.1:9200/{index}/_search
2.5.4 Result highlighting
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
(It will highlight all the participle content that contains the search)
2.5.5 Exact match (no word segmentation)
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
(It will find out all the documents containing the search content in the query field, and the continuous and uninterrupted occurrence is called an exact match)
2.5.6 Match query
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
2.5.7 match_all query all
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
2.5.7 Pagination condition query
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
Through from and size, from is from which item, and how many items is size
2.5.8 Partial display of query fields
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
Pass "__source":["uuid"] "__source":["field 1", "field 2", "field 3"]
2.5.9 Sorting by field
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
//通过 sort
"sort":{
"price":{
//字段名
"order":"desc" //正序还是倒序
}
}
2.6 Query documents with multiple conditions
2.6.1 Multiple conditions and
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
Construct a bool query condition, must be multi-condition and
//查询uuid包含iphone 同时 price为8799 的数据
{
"query": {
"bool": {
"must": [
{
"match": {
"uuid": "iphone"
}
},
{
"match": {
"price": "8799"
}
}
]
}
},
"from": 0,
"size": 2
}
2.6.2 Multiple conditions or
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
Construct bool query condition, should is multi-condition and
//查询uuid包含mac 或者 price为16000 的数据
{
"query": {
"bool": {
"should": [
{
"match": {
"uuid": "mac"
}
},
{
"match": {
"price": "16000"
}
}
],
}
}
}
2.6.3 Specified range filtering
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
Construct bool query conditions, filter keywords to write filter conditions
gt: greater than gte: greater than or equal to lt: less than lte: less than or equal to
//过滤出价格大于等于16000 并且 价格小于18000 的数据
{
"query": {
"bool": {
"filter": {
"range": {
"price": {
"gte": 16000,
"lt": 18000
}
}
}
}
}
}
2.7 Aggregation query
2.7.1 group query
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
aggs is the aggregation operation keyword, if the original data does not need to be displayed here, add the condition of size:0
{
"aggs": {
//聚合操作
"price_group": {
//分组名称,随意起名
"terms": {
//分组
"field": "price" //分组字段
}
}
},
"size": 0 //不需要展示原始数据
}
It will be grouped according to price, and will count out the quantity in the group by default
2.7.2 avg average
GET
Request the interface of ES through http://127.0.0.1:9200/{index}/_search
aggs is the aggregation operation keyword, if the original data does not need to be displayed here, add the condition of size:0
{
"aggs": {
//聚合操作
"price_avg": {
//分组名称,随意起名
"avg": {
//求平均
"field": "price" //分组字段
}
}
},
"size": 0 //不需要展示原始数据
}
//结果
"aggregations": {
"price_avg": {
"value": 7999.375
}
}
It will be averaged based on price