ElasticSearch (3) API usage

API usage of ElasticSearch


1. Index operation

​ The ones in ES 索引can be understood as the ones in MySQL数据库

1.1 Create an index

​ By PUTrequesting the interface of ES

​ Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}
insert image description here

The newly created index shopping2 can be seen in ES-head

insert image description here

1.2 Get index information

​ By GETrequesting the interface of ES

​ Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}insert image description here

1.3 Get all index information

​ By GETrequesting the interface of ES

​ Format: http://127.0.0.1:9200/_cat/indices

insert image description here

1.4 Delete index

​ By DELETErequesting the interface of ES

​ Format: http://127.0.0.1:9200/shopping http://127.0.0.1:9200/{index name}

insert image description here

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 PUTrequesting 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
    }
  }
}

insert image description here

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 POSTrequesting 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
}

insert image description here

At this point, the newly added data can be seen on the ES-head.

insert image description here

2.2 Add new document (specify ID)

​ By POSTrequesting the interface of ES

​ Format: http://127.0.0.1:9200/{index}/_doc /{id}insert image description here

At this point, you can see the newly added data with the specified ID on the ES-head.

insert image description here

2.3 Modify the document (overwrite modification)

​ Request PUTthe interface of ES (override the entire id by id)

​ Format: http://127.0.0.1:9200/{index}/_doc/{id}

insert image description here

ES-headView Results

insert image description here

2.4 Modify the document (partial modification)

​ Request PUTthe interface of ES (overridden by id part)

​ Format: http://127.0.0.1:9200/{index}/_update/{id}

insert image description here

​ ES-head view results

insert image description here

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}

insert image description here

At this time, you can see that this piece of data has been deleted in ES-head

insert image description here

2.5 Conditional query document

2.5.1 Query all documents under the document

​ By GETrequesting the interface of ES

​ Format: http://127.0.0.1:9200/{index}/__doc/_search

insert image description here

2.5.2 Query index data according to id

​ By GETrequesting the interface of ES

​ Format: http://127.0.0.1:9200/{index}/_doc/{id}

insert image description here

2.5.3 Word segmentation matching

​ By GETrequesting the interface of ES

​ (It will find out all the participle content that contains the search)

​ Format: http://127.0.0.1:9200/{index}/_searchinsert image description here

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)

insert image description here

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)

insert image description here

2.5.6 Match query

GET​ Request the interface of ES through http://127.0.0.1:9200/{index}/_search

insert image description here

2.5.7 match_all query all

GET​ Request the interface of ES through http://127.0.0.1:9200/{index}/_search

insert image description here

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

insert image description here

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"]

insert image description here

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" //正序还是倒序
        }
    }

insert image description here

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

Guess you like

Origin blog.csdn.net/qq_27331467/article/details/125843973