Elasticsearch基础

基础名词
es和mysql对应关系(容易理解)
索引 ==>数据库
类型 ==>数据表
文档 ==>一行记录

1.创建people索引 (postman创建)

PUT:127.0.0.1:9200/people

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":0
    },
    "mappings":{
          "man": {
            "properties": {
              "name": {
                "type": "text"
              },
              "country":{
                "type":"keyword"
              },
              "age":{
                "type":"integer"
              },
              "date":{
                "type":"date"
              }
            }
          },
           "woman": {}
    }
}

返回结果:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "people"
}
2.插入数据 ,people为索引,man为类型,1为指定的id

PUT:127.0.0.1:9200/people/man/1

{
    "name":"season",
    "country":"china",
    "age":22,
    "date":"1990-01-03"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "created": true
}
3.插入数据 ,people为索引,man为类型,自动ID

POST:127.0.0.1:9200/people/man

{
    "name":"王一",
    "country":"china",
    "age":26,
    "date":"1990-01-02"
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWOknfrbJy0Qc746PXrH",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

查看我们添加的数据:


image.png
4.根据ID修改数据(直接修改文档)

POST:127.0.0.1:9200/people/man/1/_update

{
    "doc":{
        "name":"我是season"   
    }
}

返回结果:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    }
}

查看数据:


image.png
5.使用脚本方式修改数据

POST:127.0.0.1:9200/people/man/1/_update

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age +=10"
    }
}

数据返回:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    }
}

查看数据:


image.png
6.删除文档

DELETE:127.0.0.1:9200/people/man/1
返回结果:

{
    "found": true,
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    }
}

查看数据:


image.png
7.删除索引(危险操作)

DELETE:127.0.0.1:9200/people

在做查询前,随便添加了些测试数据:


测试数据
7.简单查询

GET:127.0.0.1:9200/people/man/1
返回数据:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 3,
    "found": true,
    "_source": {
        "name": "苑畅",
        "country": "china",
        "age": 50,
        "date": "1990-01-03"
    }
}
8.条件查询
8.1查询全部 (默认显示前十条)

POST:127.0.0.1:9200/people/_search

{
    "query":{
        "match_all":{}
    }
}

或者(查询一条)

{
    "query":{
        "match_all":{}
    },
    "from":1,
    "size":1
}

返回数据:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWOknfrbJy0Qc746PXrH",
                "_score": 1,
                "_source": {
                    "name": "王一",
                    "country": "china",
                    "age": 26,
                    "date": "1990-01-02"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "苑畅",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                }
            },
           ..........
            }
        ]
    }
}

其中:took表示花费时间(毫秒),hits表示结果

8.2查询并排序

POST:127.0.0.1:9200/people/_search

{
    "query":{
        "match":{
            "name":"苑畅"
        }
    },
    "sort":[
        {"age":{"order":"desc"}}
    ]
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "2",
                "_score": null,
                "_source": {
                    "name": "苑畅",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                },
                "sort": [
                    50
                ]
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "1",
                "_score": null,
                "_source": {
                    "name": "苑畅",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                },
                "sort": [
                    50
                ]
            }
        ]
    }
}
9.聚合查询

POST:127.0.0.1:9200/people/_search

{
    "aggs":{
        "group_by_age":{
            "terms":{
                "field":"age"
            }
        }
    }
}

返回内容:

{
    "took": 33,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 1,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWOknfrbJy0Qc746PXrH",
                "_score": 1,
                "_source": {
                    "name": "王一",
                    "country": "china",
                    "age": 26,
                    "date": "1990-01-02"
                }
            },
          .......
            }
        ]
    },
    "aggregations": {
        "group_by_age": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 50,
                    "doc_count": 4
                },
                {
                    "key": 26,
                    "doc_count": 1
                }
            ]
        }
    }
}
9.高级查询
9.1子条件查询

a.模糊匹配
POST:127.0.0.1:9200/people/_search

{
    "query":{
        "match":{
            "name":"苑畅李"
        }
    }
}

返回结果

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.7600523,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "2",
                "_score": 1.7600523,
                "_source": {
                    "name": "苑畅",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "1",
                "_score": 1.2438203,
                "_source": {
                    "name": "苑畅",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "4",
                "_score": 0.88002616,
                "_source": {
                    "name": "李垦",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                }
            }
        ]
    }
}

如果我们改成"match_phrase":....结果就是空
b.多字段匹配

{
    "query":{
        "multi_match":{
            "query":"李",
            "fields":["name","country"]
        }
    }
}

返回结果:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.88002616,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "4",
                "_score": 0.88002616,
                "_source": {
                    "name": "李垦",
                    "country": "china",
                    "age": 50,
                    "date": "1990-01-03"
                }
            }
        ]
    }
}
9.2复合条件查询

附:
1.查看单节点Elasticsearch健康状态
http://127.0.0.1:9200/_cluster/health?pretty

image.png

2.单节点出现Unassigned,健康值为yellow问题:
单节点记得设置number_of_replicas为0,不然会出现下面这种情况.


image.png

出现上述情况的原因是因为:存在5个unassigned的分片,新建索引blog5的时候,分片数为5,副本数为1,新建之后集群状态成为yellow,其根本原因是因为集群存在没有启用的副本分片

你可能感兴趣的:(Elasticsearch基础)