Elasticsearch基本语句

  • 一、概述
  • 二、使用
  • 1.索引-创建
  • 2.索引-查询
  • 3.文档-创建
  • 4.主键查询&全查询
  • 5.条件查询
  • 6.多条件查询
  • 7.全文检索
  • 8.聚合查询


一、概述

Elasticsearch,简称为 ES, ES 是一个开源的高扩展的分布式全文搜索引擎,同时也是面向文档型数据库,在ES中一条数据就是一个文档。在学习ES之前需要先了解一些概念:

Index(索引):不同于Mysql的索引,ES中的索引相当于Mysql的数据库,当我们向ES插入数据的时候,需要指定要插入到哪个索引之下。

Document(文档):ES中存储的就是一条条文档,ES可以通过关键字检索到相应的文档。

Fields(字段):与MySQL中的字段相同

二、使用

1.索引-创建

在Postman中,向ES服务器发送PUT请求:

localhost:9200/lol

请求后,服务器返回响应:

{
    "acknowledged": true,		//响应结果
    "shards_acknowledged": true,//分片结果
    "index": "lol"				//索引名称
}

2.索引-查询

在 Postman 中,向 ES 服务器发 GET 请求 :

localhost:9200/_cat/indices?v

3.文档-创建

在Postman中,向ES服务器发送POST请求:

localhost:9200/lol/_doc

请求体JSON内容为:

{
    "名字":"卡沙",
    "称号":"虚空之女",
    "定位":"下路、中路"
}

4.主键查询&全查询

全查询:在 Postman 中,向 ES 服务器发 GET 请求 :

localhost:9200/lol/_search

响应:

{
    "took": 442,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "uI6dVoEB32Jj3QrMO2F-",
                "_score": 1.0,
                "_source": {
                    "名字": "卡沙",
                    "称号": "虚空之女",
                    "定位": "下路、中路"
                }
            },
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "vY4HV4EB32Jj3QrMIGHs",
                "_score": 1.0,
                "_source": {
                    "名字": "易",
                    "称号": "无极剑圣",
                    "定位": "打野"
                }
            },
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "vo4HV4EB32Jj3QrMiGF0",
                "_score": 1.0,
                "_source": {
                    "名字": "艾希",
                    "称号": "寒冰射手",
                    "定位": "下路、辅助"
                }
            },
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "v44HV4EB32Jj3QrMtmEa",
                "_score": 1.0,
                "_source": {
                    "名字": "孙悟空",
                    "称号": "齐天大圣",
                    "定位": "上路、打野"
                }
            },
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "wI4HV4EB32Jj3QrMzGHj",
                "_score": 1.0,
                "_source": {
                    "名字": "阿狸",
                    "称号": "九尾狐妖",
                    "定位": "中单"
                }
            }
        ]
    }
}

主键查询:

localhost:9200/lol/_doc/vY4HV4EB32Jj3QrMIGHs
{
    "_index": "lol",
    "_type": "_doc",
    "_id": "vY4HV4EB32Jj3QrMIGHs",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "名字": "易",
        "称号": "无极剑圣",
        "定位": "打野"
    }
}

5.条件查询

在 Postman 中,向 ES 服务器发 GET 请求 :

localhost:9200/lol/_search

同时带上查询条件:

{
    "query":{
        "match":{
            "名字":"齐天大圣"
        }
    }
}

查询结果:

{
    "took": 262,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 3.4526575,
        "hits": [
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "v44HV4EB32Jj3QrMtmEa",
                "_score": 3.4526575,
                "_source": {
                    "名字": "孙悟空",
                    "称号": "齐天大圣",
                    "定位": "上路、打野"
                }
            }
        ]
    }

6.多条件查询

查询条件:

“must”相当于与,“should”相当于或

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "名字":"艾希"
                }
            },{"match":{
                    "名字":"卡沙"
                }
            }]
        }
    }
}

7.全文检索

查询条件:

{
    "query":{
        "match":{
            "名字":"孙易"
        }
    }
}

响应:

{
    "took": 69,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.7427702,
        "hits": [
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "vY4HV4EB32Jj3QrMIGHs",
                "_score": 1.7427702,
                "_source": {
                    "名字": "易",
                    "称号": "无极剑圣",
                    "定位": "打野"
                }
            },
            {
                "_index": "lol",
                "_type": "_doc",
                "_id": "v44HV4EB32Jj3QrMtmEa",
                "_score": 1.1508858,
                "_source": {
                    "名字": "孙悟空",
                    "称号": "齐天大圣",
                    "定位": "上路、打野"
                }
            }
        ]
    }
}

8.聚合查询

在 Postman 中,向 ES 服务器发 GET请求 :

localhost:9200/lol/_search
{
    "aggs":{//聚合操作
        "定位聚合":{
            "terms":{//分组
                "field":"定位"
            }
        }
    }
}