前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >012.Elasticsearch基础API入门以及term与match综合测试

012.Elasticsearch基础API入门以及term与match综合测试

作者头像
CoderJed
发布2020-06-19 16:57:17
7550
发布2020-06-19 16:57:17
举报
文章被收录于专栏:Jed的技术阶梯

1. 简单的集群管理

1.1 查看集群健康状态

代码语言:javascript
复制
[root@node01 ~]# curl -X GET 'node01:9200/_cat/health?v'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1586505396 07:56:36  elasticsearch yellow          1         1     20  20    0    0       20             0                  -                 50.0%
  • green:每个索引的primary shard和replica shard都是active状态的
  • yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
  • red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了

1.2 查看集群信息

代码语言:javascript
复制
[root@node01 ~]# curl -X GET 'node01:9200'
{
  "name" : "node-0",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "a4AJdVa8TaiWczmHXc80qQ",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

2. 索引操作

2.1 创建索引

代码语言:javascript
复制
[root@node01 ~]# curl -X PUT "node01:9200/index1"

# 返回
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index1"
}

2.2 查看索引

代码语言:javascript
复制
[root@node01 ~]# curl -X GET "node01:9200/index1"

# 返回
{
    "index1": {
        "aliases": {},
        "mappings": {}, # 字段信息
        "settings": {
            "index": {
                "creation_date": "1586503723100",
                "number_of_shards": "5", # 默认shard数:5
                "number_of_replicas": "1", # 默认replica数:1
                "uuid": "mnGGs-8yTbuDrcbwUmdTZQ",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index1"
            }
        }
    }
}

2.3 删除索引

代码语言:javascript
复制
curl -X DELETE "node01:9200/index1"

# 返回
{
    "acknowledged": true
}

2.4 批量查看索引

代码语言:javascript
复制
curl -X GET "node01:9200/index1,index2"

# 返回
{
    "index1": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1586504252493",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "ICoU-6pfT36cHvEGSys7Ww",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index1"
            }
        }
    },
    "index2": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1586504255661",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "r3bVxPGRQUC1aZslG2N3yw",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index2"
            }
        }
    }
}

2.5 查看全部索引

代码语言:javascript
复制
# 方法一
curl -X GET "node01:9200/_all"
# 返回
{
    "index1": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1586504252493",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "ICoU-6pfT36cHvEGSys7Ww",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index1"
            }
        }
    },
    "index2": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1586504255661",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "r3bVxPGRQUC1aZslG2N3yw",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index2"
            }
        }
    },
    "index3": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1586504463960",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "CrtgrJ7QSdyz0Qow_i0pBg",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "index3"
            }
        }
    },
    "test_index": {
        "aliases": {},
        "mappings": {
            "test_type": {
                "properties": {
                    "message": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "user": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1586503491778",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "KyWsIMTDQUyNPmRsR0DEvw",
                "version": {
                    "created": "6060099"
                },
                "provided_name": "test_index"
            }
        }
    }
}

# 方法二
curl -X GET "node01:9200/_cat/indices?v"
# 返回
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index2     r3bVxPGRQUC1aZslG2N3yw   5   1          0            0      1.2kb          1.2kb
yellow open   index1     ICoU-6pfT36cHvEGSys7Ww   5   1          0            0      1.2kb          1.2kb
yellow open   test_index KyWsIMTDQUyNPmRsR0DEvw   5   1          1            0        5kb            5kb
yellow open   index3     CrtgrJ7QSdyz0Qow_i0pBg   5   1          0            0      1.1kb          1.1kb

2.6 判断索引是否存在

代码语言:javascript
复制
[root@node01 ~]# curl -I "node01:9200/index1"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 231

[root@node01 ~]# curl -I "node01:9200/index100"
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 355

2.7 关闭索引

代码语言:javascript
复制
[root@node01 ~]# curl -X POST "node01:9200/index1/_close"
{
    "acknowledged":true
}

2.8 开启索引

代码语言:javascript
复制
[root@node01 ~]# curl -X POST "node01:9200/index1/_open"
{
    "acknowledged":true,
    "shards_acknowledged":true
}

3. mapping操作

3.1 mapping是什么

  • mapping,就是index的type的元数据,每个type都有一个自己的mapping,决定了这个type的数据类型,建立倒排索引的行为,还有进行搜索的行为,可以类比关系型数据库,ES给某个index的type设置mapping,就相当于给一张表定义各个字段的名称和数据类型
  • 往一个不存在的index里面插入数据,es会自动建立该index,同时建立type以及对应的mapping
  • mapping中就自动定义了每个field的数据类型
  • es可以进行dynamic mapping,自动建立mapping,包括自动设置数据类型;也可以提前手动创建index和type的mapping,对各个field进行设置,包括数据类型,包括索引行为,包括分词器,等等

3.2 创建mapping

为空index设置mapping,index需要提前创建好

代码语言:javascript
复制
# ES6.x需要在_mapping后指定type,type可以自动创建
curl -X PUT "node01:9200/nba/_mapping/_doc" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

# ES7.x
curl -X PUT "node01:9200/nba/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "name": {
            "type": "text"
        },
        "team_name": {
            "type": "text"
        },
        "position": {
            "type": "keyword"
        },
        "play_year": {
            "type": "keyword"
        },
        "jerse_no": {
            "type": "keyword"
        }
    }
}
'

创建index的时候设置mapping

代码语言:javascript
复制
# ES6.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "test_type": {
      "properties": {
        "id": {
          "type": "text"
        }
      }
    }
  }
}
'
# ES7.x
curl -X PUT "node01:9200/index4" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "properties": {
      "id": {
      "type": "text"
      }
    }
  }
}
'

3.3 查看mapping

查看某个索引的mapping

代码语言:javascript
复制
# ES6.0需要指定type
curl -X GET "node01:9200/nba/_mapping/_doc"
# ES7.0不需要指定type
curl -X GET "node01:9200/nba/_mapping"

{
    "nba": {
        "mappings": {
            "_doc": {
                "properties": {
                    "jerse_no": {
                        "type": "keyword"
                    },
                    "name": {
                        "type": "text"
                    },
                    "play_year": {
                        "type": "keyword"
                    },
                    "position": {
                        "type": "keyword"
                    },
                    "team_name": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

查看多个索引或者type的mapping

  • ES5.x:一个index可以有多个type
    • 查看一个index的一个type的mapping:curl -X GET "ip:9200/index/_mapping/type"
    • 查看一个index的多个type的mapping:curl -X GET "ip:9200/index/_mapping/type1,type2"
    • 查看一个index的所有type的mapping:curl -X GET "ip:9200/index/_mapping"
    • 查看多个index的多个type的mapping:curl -X GET "ip:9200/index1,index2/_mapping/type1,type2" 这时会做笛卡尔积,把所有可以查到的index的type的mapping返回
    • 可以使用通配符: curl -X GET "ip:9200/index*,test*/_mapping" curl -X GET "ip:9200/*1,*2/_mapping/type*" 使用通配符也会做笛卡尔积,所有满足通配符的index和type,只要可以这个index有这个type就会查出mapping并返回
  • ES6.x:一个index只能有一个type
    • 查看一个index的mapping curl -X GET "ip:9200/index/_mapping/type" curl -X GET "ip:9200/index/_mapping" 由于只有一个type,所以这两条查询本质上是一样的
    • 查看多个index的mapping curl -X GET "ip:9200/index1,index2/_mapping/type1,type2" curl -X GET "ip:9200/test*/_mapping" 同样,也是做笛卡尔积,只要可以查出来就返回
  • ES7.x:没有type的概念
    • 查看多个index的mapping: curl -X GET "ip:9200/index1,index2/_mapping" curl -X GET "ip:9200/test*/_mapping"
  • ES6.x测试 index和type对应关系: test_index1/test_type1 test_index2/test_type2 curl -X GET "node01:9200/test_index1/_mapping" { "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index1/_mapping/test_type1" { "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index1,test_index2/_mapping" { "test_index2":{ "mappings":{ "test_type2":{ "properties":{ "name":{ "type":"text" } } } } }, "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type1" { "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type2" { "test_index2":{ "mappings":{ "test_type2":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index1,test_index2/_mapping/test_type1,test_type2" { "test_index2":{ "mappings":{ "test_type2":{ "properties":{ "name":{ "type":"text" } } } } }, "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } } curl -X GET "node01:9200/test_index*,index*/_mapping" { "index3":{ "mappings":{} }, "test_index":{ "mappings":{ "test_type":{ "properties":{ "message":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256 } } }, "user":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256 } } } } } } }, "test_index2":{ "mappings":{ "test_type2":{ "properties":{ "name":{ "type":"text" } } } } }, "index2":{ "mappings":{} }, "index1":{ "mappings":{ "_doc":{ "properties":{ "jerse_no":{ "type":"keyword" }, "name":{ "type":"text" }, "play_year":{ "type":"keyword" }, "position":{ "type":"keyword" }, "team_name":{ "type":"text" } } } } }, "test_index1":{ "mappings":{ "test_type1":{ "properties":{ "name":{ "type":"text" } } } } } }

查看全部索引的mapping

  • ES5.x/ES6.x
    • 查看所有的index的所有的type的mapping curl -X GET "ip:9200/_mapping
    • 查看所有的index的某些type的mapping,没有这个type的index,就不返回它的mapping curl -X GET "ip:9200/_all/_mapping/type1,type2
  • ES7.x curl -X GET "ip:9200/_mapping" curl -X GET "ip:9200/_all/_mapping"
  • ES6.x测试
代码语言:javascript
复制
  curl -X GET node01:9200/_all/_mapping/test_type1,test_type2
{
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      }
  }
  
  curl -X GET node01:9200/_mapping
  {
      "index1": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "index2": {
          "mappings": {}
      },
      "test_index2": {
          "mappings": {
              "test_type2": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index": {
          "mappings": {
              "test_type": {
                  "properties": {
                      "message": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "user": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      }
                  }
              }
          }
      },
      "test2": {
          "mappings": {}
      },
      "nba": {
          "mappings": {
              "_doc": {
                  "properties": {
                      "jerse_no": {
                          "type": "keyword"
                      },
                      "name": {
                          "type": "text"
                      },
                      "play_year": {
                          "type": "keyword"
                      },
                      "position": {
                          "type": "keyword"
                      },
                      "team_name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test_index1": {
          "mappings": {
              "test_type1": {
                  "properties": {
                      "name": {
                          "type": "text"
                      }
                  }
              }
          }
      },
      "test1": {
          "mappings": {}
      },
      "index3": {
          "mappings": {}
      }
  }

3.4 修改mapping

代码语言:javascript
复制
# 只能新增field,不能修改已有field的数据类型
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

# 如果要写上原有的字段,一定要和以前的数据类型一样
curl -X PUT "node01:9200/test_index1/_mapping/test_type1" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        },
        "name": {
            "type": "text"
        }
    }
}
'

# EX7.x不需要指定type
curl -X PUT "node01:9200/test_index1/_mapping" -H 'Content-Type:application/json' -d'
{
    "properties": {
        "id": {
            "type": "text"
        }
    }
}
'

4.document的CRUD操作

4.1 创建测试用index

代码语言:javascript
复制
curl -X PUT "node01:9200/nba"

curl -X PUT "node01:9200/nba" -H 'Content-Type:application/json' -d'
{
    "mappings": {
        "_doc": {
            "properties": {
                "jerse_no": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                },
                "play_year": {
                    "type": "keyword"
                },
                "position": {
                    "type": "keyword"
                },
                "team_name": {
                    "type": "text"
                },
                "country": {
                    "type": "keyword"
                }
            }
        }
    }
}
'

4.2 自动创建索引

当向一个不存在的index中添加document时,可以自动创建索引,也可以根据传入的数据自动创建mapping,ES也会自动对这些文档进行倒排索引

代码语言:javascript
复制
# 查看ES集群配置
curl -X GET "node01:9200/_cluster/settings"

{
    "persistent":{},
    "transient":{}
}

# 设置自动创建index功能
curl -X PUT "node01:9200/_cluster/settings" -H 'Content-Type:application/json' -d'
{
    "persistent": {
        "action.auto_create_index": "false"
    }
}
'

{   "acknowledged":true,
    "persistent":{
        "action":{
            "auto_create_index":"false"
        }
    },
    "transient":{}
}

当把action.auto_create_index设置成false后,就不可以向不存在的index插入数据了

4.2 添加document

代码语言:javascript
复制
# 手动指定id
curl -X PUT "node01:9200/nba/_doc/1" -H 'Content-Type:application/json' -d'
{
    "name":"哈登",
    "team_name":"火箭",
    "position":"得分后卫",
    "play_year":"10",
    "jerse_no":"13",
    "country": "美国"
}
'
# 也可以让ES自动生成id,不指定id要使用POST请求
curl -X POST "node01:9200/nba/_doc" -H 'Content-Type:application/json' -d'
{
    "name":"库里",
    "team_name":"勇士",
    "position":"控球后卫",
    "play_year":"10",
    "jerse_no":"13",
    "country": "美国"
}
'

# 可以手动指定操作类型
# 指定操作类型时,必须手动设置id
# 当不指定操作时,假如原来已经有id=2的document,那么执行以下操作就会把原来的覆盖掉
# 而指定操作为"create",那么当id=2的document已经存在时,就会报错
curl -X POST "node01:9200/nba/_doc/2?op_type=create" -H 'Content-Type:application/json' -d'
{
    "name":"姚明",
    "team_name":"火箭",
    "position":"中锋",
    "play_year":"9",
    "jerse_no":"11",
    "country": "中国"
}
'

4.3 查看文档

查看单个文档

代码语言:javascript
复制
curl -X GET "node01:9200/nba/_doc/1"

{
    "_index":"nba",
    "_type":"_doc",
    "_id":"1",
    "_version":1,
    "_seq_no":0,
    "_primary_term":1,
    "found":true,
    "_source": {
        "name":"哈登",
        "team_name":"火箭",
        "position":"得分后卫",
        "play_year":"10",
        "jerse_no":"13",
        "country": "美国"
    }
}

查看多个文档

代码语言:javascript
复制
curl -X POST "node01:9200/_mget" -H 'Content-Type:application/json' -d'
{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1"
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2"
        }
    ]
}
'

curl -X POST "node01:9200/nba/_mget" -H 'Content-Type:application/json' -d'
{
    "docs": [
        {
            "_type": "_doc",
            "_id": "1"
        },
        {
            "_type": "_doc",
            "_id": "2"
        }
    ]
}
'

curl -X POST "node01:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d'
{
    "docs": [
        {
            "_id": "1"
        },
        {
            "_id": "2"
        }
    ]
}
'

curl -X POST "node01:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d'
{
    "ids": ["1", "2"]
}
'

4.4 修改文档

代码语言:javascript
复制
# 方法一:直接覆盖原文档
# 但是要列出所有的field,即使这个field不修改
# 否则新的文档就不包含你没有写的那个field了
# 当然,如果就是要删除这个字段,就不用写了
curl -X PUT "node01:9200/nba/_doc/1" -H 'Content-Type:application/json' -d'
{
    "name":"大胡子",
    "team_name":"火箭",
    "position":"得分后卫",
    "play_year":"9",
    "jerse_no":"13",
    "country": "美国"
}
'

# 方法二:修改指定的字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
    "doc": {
        "name":"登哥"
    }
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
    "doc": {
        "name":"登哥"
    }
}
'

# 增加一个字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
    "script": "ctx._source.age=18"
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
    "script": "ctx._source.age=18"
}
'

# 删除一个字段
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
    "script": "ctx._source.remove(\"age\")"
}
'
# ES7.x
curl -X POST "node01:9200/nba/_update/1" -H 'Content-Type:application/json' -d'
{
    "script": "ctx._source.remove(\"age\")"
}
'

# 根据参数值,更新字段,要求文档存在并且修改的field存在
curl -X POST "node01:9200/nba/_doc/1/_update" -H 'Content-Type:application/json' -d'
{
    "script": "ctx._source.age+=params.age",
    "params": {
        "age": 4
    }
}
'
# 根据参数值,更新字段,如果文档不存在,新创建一个文档,并且将upsert中的数据插入到该文档中
curl -X POST "node01:9200/nba/_doc/3/_update" -H 'Content-Type:application/json' -d'
{
    "script": {
        "source": "ctx._source.age+=params.age",
        "params": {
            "age": 4
        }
    },
    "upsert": {
        "age": 10
    }
}
'

4.5 删除文档

代码语言:javascript
复制
curl -X DELETE "node01:9200/nba/_doc/3"

5. 搜索

5.1 创建测试数据

代码语言:javascript
复制
# text:用于全文检索,该类型的字段将通过分词器进行分词
# keyword:不分词,只能搜索该字段完整的值
curl -X PUT "node01:9200/shop" -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "desc": {
          "type": "text"
        }
      }
    }
  }
}
'

curl -X PUT "node01:9200/shop/product/1" -H 'Content-Type:application/json' -d'
{
    "name": "Yunnanbaiyao Toothpaste",
    "desc": "Yunnanbaiyao Toothpaste"
}
'
curl -X PUT "node01:9200/shop/product/2" -H 'Content-Type:application/json' -d'
{
    "name": "Darlie Toothpaste",
    "desc": "Darlie Toothpaste"
}
'
curl -X PUT "node01:9200/shop/product/3" -H 'Content-Type:application/json' -d'
{
    "name": "ZhongHua Toothpaste",
    "desc": "ZhongHua Toothpaste"
}
'

5.2 多种搜索方式

Query String Search:在请求URL中包括search的参数

代码语言:javascript
复制
# 查看全部文档
curl -X GET "node01:9200/shop/product/_search"
# 搜索商品名称中包含“Toothpaste”的商品,而且按照price降序排序
curl -X GET "node01:9200/shop/product/_search?q=name:Toothpaste&sort=price:desc"

适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的,在生产环境中,几乎很少使用Query String Search

Query DSL(Domain Specified Language):查询领域专用语言

代码语言:javascript
复制
# 查询全部文档
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
  "query": {"match_all": {}}
}
'

使用的最多的查询方式,更加适合生产环境的使用,可以构建复杂的查询

5.3 词条查询(term)和全文检索(full text)

  • 词条查询:词条查询不会分析查询条件,只有当词条和查询字符串串完全匹配时,才匹配搜 索。
  • 全文查询:ElasticSearch引擎会先分析查询字符串,将其拆分成多个单词,只要已分析的字 段中包含词条的任意一个,或全部包含,就匹配查询条件,返回该文档;如果不包含任意一 个分词,表示没有任何文档匹配查询条件
  • 查询结果与使用termmatch与数据本身的类型息息相关

term测试

代码语言:javascript
复制
# 词条查询

# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "term": {
            "name": "Toothpaste"
        }
    }
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "term": {
            "name": "Darlie Toothpaste"
        }
    }
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "term": {
            "desc": "Toothpaste"
        }
    }
}
'
# 没有结果
# 这个没有结果,是因为"Darlie Toothpaste"分词后分成了"darlie"和"toothpaste"
# 所有完全匹配"Darlie Toothpaste"是无法匹配到的
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "term": {
            "desc": "Darlie Toothpaste"
        }
    }
}
'

# 有3条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "term": {
            "desc": "toothpaste"
        }
    }
}

terms测试

代码语言:javascript
复制
# 没有数据
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "terms": {
            "name": ["Darlie", "Toothpaste"]
        }
    }
}
'
# 两条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "terms": {
            "name": ["Darlie Toothpaste", "Yunnanbaiyao Toothpaste"]
        }
    }
}
'
# 一条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "terms": {
            "name": ["Darlie", "Yunnanbaiyao Toothpaste"]
        }
    }
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "terms": {
            "desc": ["Darlie Toothpaste", "Yunnanbaiyao Toothpaste"]
        }
    }
}
'

# 1个结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "terms": {
            "desc": ["darlie", "Yunnanbaiyao Toothpaste"]
        }
    }
}
'

match测试

代码语言:javascript
复制
# 有结果(1条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match": {
            "name": "Darlie Toothpaste"
        }
    }
}
'
# 没有结果
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match": {
            "name": "Toothpaste"
        }
    }
}
'
# 有结果(3条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match": {
            "desc": "Toothpaste"
        }
    }
}
'
# 有结果(3条)
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match": {
            "desc": "Darlie Toothpaste"
        }
    }
}
'

multi_match测试

代码语言:javascript
复制
curl -X PUT "node01:9200/shop/product/5" -H 'Content-Type:application/json' -d'
{
    "name": "Apple Toothpaste",
    "desc": "Apple Darlie"
}
'
curl -X PUT "node01:9200/shop/product/6" -H 'Content-Type:application/json' -d'
{
    "name": "Orage Darlie",
    "desc": "Orage"
}
'

# 3条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "multi_match": {
            "query": "Darlie",
            "fields": ["name", "desc"]
        }
    }
}
'
# 4条结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "multi_match": {
            "query": "Orage Darlie",
            "fields": ["name", "desc"]
        }
    }
}
'

match_phrase测试

代码语言:javascript
复制
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase": {
            "desc": "Darlie Toothpaste"
        }
    }
}
'
# 有结果(3条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase": {
            "desc": "Toothpaste"
        }
    }
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase": {
            "name": "Toothpaste"
        }
    }
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase": {
            "name": "Darlie Toothpaste"
        }
    }
}
'

match_all测试

代码语言:javascript
复制
# 查询全部数据
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_all": {}
    }
}
'
curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_all": {
            "desc": "Darlie Toothpaste"
        }
    }
}
'
# 结果
{
    "error":{
        "root_cause":[
            {
                "type":"parsing_exception",
                "reason":"[5:13] [match_all] unknown field [desc], parser not found","line":5,
                "col":13
            }
        ],
        "type":"parsing_exception",
        "reason":"[5:13] [match_all] unknown field [desc], parser not found",
        "line":5,
        "col":13,
        "caused_by":{
            "type":"x_content_parse_exception",
            "reason":"[5:13] [match_all] unknown field [desc], parser not found"}
        },
        "status":400
    }
}

curl -X GET "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_all": {}
    },
    "from": 0,
    "size": 10
}
'

match_phrase_prefix测试

代码语言:javascript
复制
curl -X PUT "node01:9200/shop/product/4" -H 'Content-Type:application/json' -d'
{
    "name": "Darlie Pro Toothpaste",
    "desc": "Darlie Pro Toothpaste"
}
'

# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase_prefix": {
            "name": "Darlie Toothpaste"
        }
    }
}
'
# 没有结果
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase_prefix": {
            "name": "Darlie"
        }
    }
}
'
# 有结果(2条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase_prefix": {
            "desc": "Darlie"
        }
    }
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase_prefix": {
            "desc": "Darlie Pro"
        }
    }
}
'
# 有结果(1条)
curl -X POST "node01:9200/shop/product/_search" -H 'Content-Type:application/json' -d'
{
    "query": {
        "match_phrase_prefix": {
            "desc": "Darlie Toothpaste"
        }
    }
}
'

总结

keyword

text

term

完全匹配才返回

完全匹配分词后的单词才返回

terms

传入多个字符串,返回那些可以完全匹配的结果

每个传入的单词,在分词后的所有单词中进行匹配,完全匹配才返回

match_all

查询全部数据,不能传入任何参数

查询全部数据,不能传入任何参数

match

完全匹配才返回

对输入字符串进行分词,指定的字段文本分词后的词语中包含任意一个输入字符串的分词词语,就算匹配,就可以作为结果返回

multi_match

指定的多个字段都完全匹配才返回

对输入字符串进行分词,指定的字段文本分词后的词语中包含任意一个输入字符串的分词词语,就算匹配,就可以作为结果返回

match_phrase

完全匹配才返回

输入字符串不分词,指定的字段文本分词后的词语中包含完整的输入字符串,才可以算匹配,才能作为结果返回

match_phrase_prefix

完全匹配才返回

输入一个单词,例如"hello",只要指定的字段文本分词后的词语中有一个词语是以"hello"作为前缀,就算匹配,输入一个短语例如"hello world tom",那么先匹配分词的后的词语中包含"hello world"的文档,然后在这些文档中过滤,只要这些文档的词语中包含以"tom"开头的词语,就算匹配

小提示

代码语言:javascript
复制
# 使用pretty参数可以让返回结果更好看
[root@node01 ~]# curl -X GET "node01:9200/shop/product/_search?pretty" -H 'Content-Type:application/json' -d'
> {
>     "query": {"match_all": {}}
> }
> '
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "Apple Toothpaste",
          "desc" : "Apple Darlie"
        }
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "Darlie Toothpaste",
          "desc" : "Darlie Toothpaste"
        }
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "Darlie Pro Toothpaste",
          "desc" : "Darlie Pro Toothpaste"
        }
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "Orage Darlie",
          "desc" : "Orage"
        }
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Yunnanbaiyao Toothpaste",
          "desc" : "Yunnanbaiyao Toothpaste"
        }
      },
      {
        "_index" : "shop",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "ZhongHua Toothpaste",
          "desc" : "ZhongHua Toothpaste"
        }
      }
    ]
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 简单的集群管理
    • 1.1 查看集群健康状态
      • 1.2 查看集群信息
      • 2. 索引操作
        • 2.1 创建索引
          • 2.2 查看索引
            • 2.3 删除索引
              • 2.4 批量查看索引
                • 2.5 查看全部索引
                  • 2.6 判断索引是否存在
                    • 2.7 关闭索引
                      • 2.8 开启索引
                      • 3. mapping操作
                        • 3.1 mapping是什么
                          • 3.2 创建mapping
                            • 为空index设置mapping,index需要提前创建好
                            • 创建index的时候设置mapping
                          • 3.3 查看mapping
                            • 查看某个索引的mapping
                            • 查看多个索引或者type的mapping
                            • 查看全部索引的mapping
                          • 3.4 修改mapping
                          • 4.document的CRUD操作
                            • 4.1 创建测试用index
                              • 4.2 自动创建索引
                                • 4.2 添加document
                                  • 4.3 查看文档
                                    • 查看单个文档
                                    • 查看多个文档
                                  • 4.4 修改文档
                                    • 4.5 删除文档
                                    • 5. 搜索
                                      • 5.1 创建测试数据
                                        • 5.2 多种搜索方式
                                          • Query String Search:在请求URL中包括search的参数
                                          • Query DSL(Domain Specified Language):查询领域专用语言
                                        • 5.3 词条查询(term)和全文检索(full text)
                                          • term测试
                                          • terms测试
                                          • match测试
                                          • multi_match测试
                                          • match_phrase测试
                                          • match_all测试
                                          • match_phrase_prefix测试
                                          • 总结
                                          • 小提示
                                      相关产品与服务
                                      命令行工具
                                      腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
                                      领券
                                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档