elasticsearch 7.x 使用
作者:admin 发布时间:June 9, 2020 分类:Others
创建一个新的索引库
PUT /leyou
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
查看索引信息
GET /leyou
删除索引库
DELETE /leyou
添加映射(mappings),ps:等同数据库建表
# 参数说明
PUT /索引库名/_mapping #(自7.0起取消了Type,不需要加索引类型名,统一为_doc)
{
"properties": {
"字段名": {
"type": "类型", # 类型:可以是text、long、short、date、integer、object等
"index": true, # 是否索引,默认为true
"store": true, # 是否存储,默认为false
"analyzer": "分词器" # 分词器 使用ik分词器: ik_max_word 会将文本做最细粒度的拆分,ik_smart 会做最粗粒度的拆分
}
}
}
# 常用类型
1 String类型,分两种:
text: 可分词,不可参与聚合
keyword: 不可分词,数据会作为完整字段进行匹配,可以参与聚合
2 Numerical数值类型,分两种:
基本数据类型:long、interger、short、byte、double、float、half_float
浮点数的高精度类型:scaled_float 需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
3 日期类型:
Date elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
# 使用示例
PUT /leyou/_mapping
{
"properties":{
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": "false"
},
"price":{
"type":"float"
}
}
}
查看索引库的索引类型
GET /leyou/_mapping
新增数据
1 不指定id,自动生成
POST /leyou/_doc/
{
"title":"华为手机",
"images":"http://image.leyou.com/10086.jpg",
"price":2799.00
}
2 指定id
POST /leyou/_doc/1
{
"title":"华为手机",
"images":"http://image.leyou.com/10086.jpg",
"price":2799.00
}
查询的数据
GET /leyou/_search
删除单条数据
DELETE /索引库名/_doc/id值
基本查询
语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
查询类型: match_all:查所有,match:匹配查询,term:词条查询 ,range:范围查询
GET /leyou/_search
{
"query": {
"match": {
"title": "手机"
}
}
}
GET /leyou/_search
{
"query":{
"match_all": {} #查所有 可简写为 GET /leyou/_search
}
}
场景:只想查 华为Mac, 但是出来的值包含了手机
# 添加max 数据
POST /leyou/_doc/5
{
"title":"华为Mac",
"images":"http://image.leyou.com/10086.jpg",
"price":3799.00
}
# 改良查询json: 引入operator
GET /leyou/_search
{
"query": {
"match": {
"title": {
"query": "华为Mac",
"operator": "and" # 这里定义了操作符and,表示只检索完全匹配的数据
}
}
}
}
场景: 想搜索 华为Mac的同时,了解电视的行情(华为Mac电视),上面的操作符and又不好用了(没有结果)
# 添加电视数据
POST /leyou/_doc/6
{
"title":"华为电视",
"images":"http://image.leyou.com/10086.jpg",
"price":6799.00
}
# 改良查询json: 这里引入 minimum_should_match 匹配的程度 可以是词条数目也可以是百分比(通常百分比)
GET /leyou/_search
{
"query": {
"match": {
"title": {
"query": "华为Mac电视",
"minimum_should_match": "75%" # 搜索75%匹配的数据
}
}
}
}
多字段查询:multi_match
# 先加一个数据
POST /leyou/_doc/7
{
"title":"华为手机",
"subTitle":"吊打小米",
"images":"http://image.leyou.com/10086.jpg",
"price":6799.00
}
# 多字段查询
GET /leyou/_search
{
"query": {
"multi_match": {
"query": "华为吊打小米", # 搜索内容
"fields": ["title","subTitle"], # 搜索的字段title,subTitle
"minimum_should_match": "75%" # 匹配程度
}
}
}
词条查询:term 查询条件必须为不可分词的最小词条
# 查出华为的全部内容
GET /leyou/_search
{
"query": {
"term": {
"title": {
"value": "华为"
}
}
}
}
多词条精确匹配:terms
# 查出:包含手机与电视的内容(精确匹配)
GET /leyou/_search
{
"query": {
"terms": {
"title": [
"小米",
"电视"
]
}
}
}
基本查询补充 过滤
# 查出title匹配手机的数据,并只显示title,price字段
GET /leyou/_search
{
"_source": ["title","price"], # 查询字段
"query": {
"match": {
"title": "手机"
}
}
}
includes:只取查询某字段
GET /leyou/_search
{
"_source": {
"includes": "price"
},
"query": {
"match": {
"title": "手机"
}
}
}
excludes:排除查询某字段
GET /leyou/_search
{
"_source": {
"excludes": "subTitle"
},
"query": {
"match": {
"title": "手机"
}
}
}
bool组合将其他查询通过must(与) should(或) must_not(非) 组合
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
},{
"term": {
"price": "3799"
}
}
]
}
}
}
范围查询range
# 查出了价格范围为5000到9999的文档,并且,匹配华为
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
},{
"range": {
"price": {
"gte": 5000,
"lte": 9999
}
}
}
]
}
}
}
fuzzy模糊查询
# fuzziness表示模糊的容错为1个字(要是一个字与value不同也会被查到),fuzzy中只能放词
GET /leyou/_search
{
"query": {
"fuzzy": {
"title": {
"value": "手视",
"fuzziness": 1
}
}
}
}
filter过滤,一般配合bool查询使用,对结果进行过滤,这种过滤是不会影响到_source(排名)的,过滤一般配合搜索栏外的选项上的进行过滤
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
}
],
"filter": {
"range": {
"price": {
"gte": 1000,
"lte": 8000
}
}
}
}
}
}
放在bool中,虽然结果一样,但是会影响_source(排名)
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 1000,
"lte": 8000
}
}
}
]
}
}
}
排序
# 一般根据query的结果进行排序 desc:降序 asc:升序
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
}
],
"filter": {
"range": {
"price": {
"gte": 1000,
"lte": 8000
}
}
}
}
},
"sort": [
{
"price": {
"order": "asc"
}
}
]
}
# 排序也可以组合,当排序1相同时,按所写字段2排序 价格升序,价格相同_id降序
GET /leyou/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
}
],
"filter": {
"range": {
"price": {
"gte": 1000,
"lte": 8000
}
}
}
}
},
"sort": [
{
"price": {
"order": "asc"
}
},{
"_id":{
"order": "desc"
}
}
]
}
# SQL查询方式
POST /_sql?format=txt
{
"query": "SELECT account_number,address,age,balance FROM account LIMIT 10"
}
# 将SQL转化为DSL
POST /_sql/translate
{
"query": "SELECT account_number,address,age,balance FROM account WHERE age>32 LIMIT 10"
}
# SQL和DSL混合使用
POST /_sql?format=txt
{
"query": "SELECT account_number,address,age,balance FROM account",
"filter": {
"range": {
"age": {
"gte": 30,
"lte": 35
}
}
},
"fetch_size": 10
}
# WHERE
POST /_sql?format=txt
{
"query": "SELECT account_number,address,age,balance,state FROM account WHERE state='VA' LIMIT 10 "
}
#GROUP BY
POST /_sql?format=txt
{
"query": "SELECT state,COUNT(*),MAX(age),AVG(balance) FROM account GROUP BY state LIMIT 10"
}
# HAVING
POST /_sql?format=txt
{
"query": "SELECT state,COUNT(*),MAX(age),AVG(balance) FROM account GROUP BY state HAVING COUNT(*)>15 LIMIT 10"
}
#ORDER BY
POST /_sql?format=txt
{
"query": "SELECT account_number,address,age,balance,state FROM account ORDER BY balance DESC LIMIT 10 "
}
POST /_sql?format=txt
{
"query": "DESCRIBE account"
}
POST /_sql?format=txt
{
"query": "SHOW TABLES"
}
POST /_sql?format=txt
{
"query": "SHOW FUNCTIONS LIKE '%DATE%'"
}