ElasticSearch初识


定义

  • 开源
  • 分布式
  • 全文搜索引擎

这里省略安装ES过程,全文匹配查询有两种方式:

  1. http请求url携带参数进行查询
  2. 使用es_dsl语法,json作为请求体

# 索引相关

# 1、建索引,设分片

PUT /bookdb_index

{
  "settings": {
    "number_of_shards": 1
  }
}
1
2
3
4
5

# 2、删除

delete 
http://localhost:9200/mpls
1
2

# 3、查询

get _cat/indices
1

返回数据;含义名称为sta-mpls的index包含3511条数据,占用1、5mb空间。

green  open .monitoring-es-6-2021.03.04     cjXTAwKeSke14KVSHkNn9Q 1 0 11936 26   5.5mb   5.5mb
green  open .monitoring-kibana-6-2021.03.04 PN-MfrdVRw-35XiajI8d_g 1 0  2978  0 833.9kb 833.9kb
green  open .monitoring-kibana-6-2021.03.17 GjOGpbH6RSWDHSkN1WSs0w 1 0  1597  0 499.4kb 499.4kb
yellow open sta-mpls                        FIomgHCSRdyQu2wa8C36Bw 5 1  3511  0   1.5mb   1.5mb
green  open .monitoring-es-6-2021.03.17     Ip9ad5wNTbynhkvpSMuQvA 1 0 15988 30   7.2mb   7.2mb
green  open .monitoring-kibana-6-2021.05.28 W1NWoJK9S9qtl3jaUxo2pg 1 0   485  0   267kb   267kb
green  open .monitoring-es-6-2021.05.28     9TmM1qzVTxic79BJC2_8Jw 1 0  6330 54   3.2mb   3.2mb
yellow open customer                        rRVyP1X3SiqpbMgQZ3etTQ 5 1     2  0   8.5kb   8.5kb
green  open .kibana_1                       3_yTA-OyR8Sn5g2OWnuAkQ 1 0     5  0  22.4kb  22.4kb
1
2
3
4
5
6
7
8
9

# 初阶检索

# 1、创建文档

地址:http://localhost:9200/mpls/user/1
方式:PUT/POST
内容:{"name":"jack"}
1
2
3

  地址说明:mpls表示index,也就是myql中的数据库,user表示mysql中的表,1表示指定id,如果不指定id则会自动生成一个; 方式PUT用作修改,POST用作新增;内容表示创建或者修改user表中序列为1的用户name为jack。

# 2、查询

{
    "_index": "customer",
    "_type": "external",
    "_id": "2-81snkBzM-gT3kGCXGr",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

  _seq_no用作乐观锁控制并发;_http://localhost:9200/mpls/user/2?if_seq_no=9&&if_primary_term=1_表征当seq_no为9且primary_term为1时才更新, 且更新完后seq_no为自增;

# 3、更新文档

post http://localhost:9200/mpls/user/2/_update
{"name":"张三"}
1
2

  请求地址document序号后增加/_update,如果文档没有更新的话,返回json中的result就会为noop;加上_update是为了对比更新,可以在 并发量不高情况下使用。

# 4、文档添加属性

post http://localhost:9200/mpls/user/1/_update
{
    "doc": {
        "age": "18",
        "name": "张三111"
    }
}
1
2
3
4
5
6
7

# 5删除文档

delete
http://localhost:9200/mpls/user/1
1
2

# 6、批量导入

请求地址后缀加_bulk

POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"Jay Huang","age":18}
{"index":{"_id":"2"}}
{"name":"Jackson Huang","psss":"123456"}
1
2
3
4
5

返回内容

#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "took" : 175,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# 高阶检索

# 1.1、搜索排序(地址拼接)

查看数据并排序,customer表示index;_search为固定语法,q表示搜索关键字;sort表示排序,age表示排序字段

GET customer/_search?q=*&sort=age:desc
1

返回结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "age" : 36
        },
        "sort" : [
          36
        ]
      },
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "age" : 12,
          "name" : "小明"
        },
        "sort" : [
          12
        ]
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 1.2、条件搜索(DSL)

URL+请求体(QueryDSL) 请求地址+请求体
match_all:{}表示没有搜索条件

GET customer/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "age":"asc"
    }
  ]
}
1
2
3
4
5
6
7
8
9

返回结果

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "age" : 12,
          "name" : "小明"
        },
        "sort" : [
          12
        ]
      },
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "age" : 36
        },
        "sort" : [
          36
        ]
      }
    ]
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 2.1、match基本类型

match表示精准搜索(非字符串基本类型 精确匹配)

GET customer/_search
{
  "query": {
    "match": {"age": 30}
 }
}
1
2
3
4
5
6

# 2.2、match匹配字符串

精准匹配,类似 address="mill road"

GET customer/_search
{
  "query": {
    "match": {
      "address": "mill road"
 }
 }
}
1
2
3
4
5
6
7
8

# 2.3match短语匹配

模糊查询,类似 address like xxx

GET customer/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
 }
 }
}
1
2
3
4
5
6
7
8

# 3.多字段匹配

state和address分词匹配查询条件

GET customer/_search
{
  "query": {
    "multi_match": {
      "query": "mill land",
      "fields": [
        "state",
        "address"
 ]
 }
 }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 4.复合查询bool

GET bank/_search
{
  "query": {
    "bool": {
      "must": [
       {
          "match": {
            "address": "mill"
          }
       },
       {
          "match": {
            "gender": "M"
          }
      }
     ],
      "must_not": [
      {
          "match": {
            "age": "28"
      }
     }],
      "should": [
      {
          "match": {
            "firstname": "Winnie"
          }
      }]
   }
 }
}
主要为must must not should
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 5.高亮分页筛选

GET /bookdb_index/science/_search
{
  "query":{
    "multi_match": {
      "query": "祥子",
      "fields": ["publisher","title"]
    }
  },
  "size":1,
  "from": 0, 
  "_source": ["title"], 
  "highlight": {
    "fields": {"title": {}}
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

查询发布者或者标题中包含有"祥子"的文本,返回条数为1,从第0条开始,_source指定返回的字段,highlight表示高亮

注意不能修改已经存在的mapping只能创建新的mapping   es标准分词器standard只能分隔单个汉字,中文分词器ik,需要在github上复制zip链接地址, 然后解压到/usr/share/elasticsearch/plugins对应的挂载目录,重启镜像容器;验证方法:进入容器内部, 输入命令elasticsearch-plugin list即可;如果出现.DS_Store目录不存在,ls -a删除即可。自定义词库,修改config目录下的配置文件 IKAnalyzer.cfg.xml,网络地址修改es会自动重新加载。