Elasticsearch的基本操作

简单介绍

查询所有数据。

GET /*

删除所有数据。

DELETE /*

查询所有索引。

GET _cat/indices?v

全文检索

GET test1/_search?q=guide

新增测试数据。

PUT test1/type1/1
{
  "name": "lanyulei",
  "age": 18
}

获取刚刚新增的数据。

GET test1/type1/1

删除索引。

DELETE /test1

增删查改

PUT方式,如果数据存在则修改,如果数据不存在则新增。且数据的修改是全量的。不管前面的数据有多少都会被后面的数据进行覆盖。

PUT goods/name/1
{
  "name": "lanyulei",
  "age": 12,
  "nationality": "china",
  "description": "shuai",
  "tags": ["gao", "fu", "shuai"]
}

获取数据。

GET goods/name/1

使用 “POST” 后面加上 “_update” 修改指定字段数据。

POST goods/name/1/_update
{
  "doc": {
    "age": 18 
  }
}

删除数据。

DELETE goods/name/1

根据指定字段查找数据,例如:查找名称为lanyulei的数据。

# 普通方式查找
GET goods/name/_search?q=name:lanyulei

# 构建方式查找
GET goods/name/_search
{
  "query": {
    "match": {
      "name": "lanyulei"
    }
  }
}

排序查询

需注意,在排序的过程中,只能使用可排序的属性,包括数据、日期,其他的属性,均不可排序。

制作测试数据。

put /goods/fruit/1
{
  "name":"xiangjiao",
  "describe":"haochi tian",
  "price":40,
  "producer":"feilvbin",
  "tags":["xiangjiao","haochi"]
}


put /goods/fruit/2
{
  "name":"pingguo",
  "describe":"cui",
  "price":60,
  "producer":"zhongguo",
  "tags":["haokan","xiang"]
}

put /goods/fruit/3
{
  "name":"lizi",
  "describe":"zide",
  "price":10,
  "producer":"zhongguo",
  "tags":["suan","tian"]
}

put /goods/fruit/4
{
  "name":"boluo",
  "describe":"getouda",
  "price":74,
  "producer":"malaxiya",
  "tags":["huang","youci"]
}

put /goods/fruit/5
{
  "name":"mihoutao",
  "describe":"suan",
  "price":45,
  "producer":"xinxilan",
  "tags":["lv","huang"]
}

put /goods/fruit/6
{
  "name":"xigua",
  "describe":"haochi",
  "price":109,
  "producer":"zhongguo",
  "tags":["da","haochi"]
}

查询所有数据。

GET goods/fruit/_search
{
  "query": {
    "match_all": {}
  }
}

排序查询。

GET goods/fruit/_search
{
  "query": {
    "match": {
      "name": "pingguo"
    }
  },
  "sort": {
    "price": {
      "order": "asc" # desc 倒序
    }
  }
}

分页查询

每页显示2条数据,无序的话,按照默认排序。

GET goods/fruit/_search
{
  "query": {
    "match": {
      "name": "pingguo"
    }
  },
  "sort": {
    "price": {
      "order": "asc"
    }
  },
  "from": 0, # 从下标为0开始
  "size": 2  # 显示多少条
}

使用 “_source” 查询指定字段的数据。

GET goods/fruit/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name", "producer"]
}

bool查询

bool的方式,根据指定字段查询数据。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "pingguo"
          }
        }
      ]
    } 
  }
}

bool多条件查询,相当于sql的and语句。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "pingguo"
          }
        },
        {
          "match": {
            "producer": "zhongguo"
          }
        }
      ]
    } 
  }
}

bool多条件或查询,相当于sql的or语句。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "pingguo"
          }
        },
        {
          "match": {
            "producer": "zhongguo"
          }
        }
      ]
    } 
  }
}

bool多条件非查询,相当于sql的not语句。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "pingguo"
          }
        },
        {
          "match": {
            "producer": "zhongguo"
          }
        }
      ]
    } 
  }
}

按条件查询

查询name是pingguo的,然后价钱大于100的数据。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "must": [ # 这里可以使用should但是不建议使用should,会出现异常错误
        {
          "match": {
            "name": "pingguo"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "price": {
              "gt": 100
            }
          }
        }
      ]
    }
  }
}

短语检索

通过检索数组类型的数据,可直接通过全文检索,匹配多个值,通过空格间隔。

GET goods/fruit/_search
{
  "query": {
    "match": {
      "tags": "da lv"
    }
  }
}

短语匹配,就像 match 查询对于标准全文检索是一种最常用的查询一样,当你想找到彼此邻近搜索词的查询方法时,就会想到 match_phrase 查询。

GET goods/fruit/_search
{
  "query": {
    "match_phrase": {
      "name": "pingguo"
    }
  }
}

高亮显示

GET goods/fruit/_search
{
  "query": {
    "match": {
      "name": "pingguo"
    }
  },
  "highlight": {
    "pre_tags": "<b style='color: red'>",
    "post_tags": "</b>",
    "fields": {
      "name": {}
    }
  }
}

聚合函数

查询出来的数据,选择指定字段计算平均值,例如计算价钱平均值。

GET goods/fruit/_search
{
  "from": 0,
  "size": 20,
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

默认会同时展示计算结果与查询出来的数据,如果不想要查询出来的数据,将 size 设置为0。

GET goods/fruit/_search
{
  "size": 0,
  "aggs": {
    "avg_price": {
      "avg": { # sum 总数,avg 平均值
        "field": "price"
      }
    }
  }
}

bool查询后,进行聚合函数处理。

GET goods/fruit/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "pingguo"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "price": {
              "gte": 100
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "c": {
      "sum": {
        "field": "price"
      }
    }
  }
}

分段查询后,进行聚合函数处理。

GET goods/_search
{
  "size": 0,
  "aggs": {
    "f": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 50
          },
          {
            "from": 50,
            "to": 100
          },
          {
            "from": 100,
            "to": 150
          }
        ]
      },
      "aggs": {
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

mapping

查询mapping。

GET goods/_mapping

新建mappings。

PUT my_index1
{
  "mappings": {
    "a": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "text",
        },
        "age": {
          "type": "long"
        }
      }
    }
  }
}

# dynamic的三个参数
# 1. false 不会新增的字段数据不会创建索引
# 2. true 会为所有数据属性创建索引,包括新建的字段属性
# 3. strict 必须添加指定的属性,添加动态属性的时候,会报错。

copy_to属性

copy_to属性,把当前属性的值复制给指定的字段,所有copy_to的值和对copy_to属性赋的值都可以保留。

PUT my_index2
{
  "mappings": {
    "doc": {
      "dynamic": false,
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name",
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name",
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}

index属性

index属性,默认为true,如果设置为false,则当前属性不能被创建索引。及不能通过当前属性进行数据的筛选、搜索、查询等操作。

PUT my_index2
{
  "mappings": {
    "doc": {
      "dynamic": false,
      "properties": {
        "first_name": {
          "type": "text",
          "index": true
        },
        "last_name": {
          "type": "text",
          "index": false
        }
      }
    }
  }
}

对象型属性

创建对象型数据。

PUT my_index3/doc/2
{
  "name": "tom",
  "age": 11,
  "add": {
    "address": "bj",
    "tel": "13333333333"
  }
}

根据嵌套的属性数据进行过滤查询。

GET my_index3/doc/2
{
  "query": {
    "match": {
      "add.address": "bj"
    }
  }
}

本文为原创文章,未经授权禁止转载本站文章。
原文出处:兰玉磊的个人博客
原文链接:https://www.fdevops.com/2020/09/22/es-6712
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。

(5)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
兰玉磊兰玉磊
上一篇 2020年9月18日
下一篇 2020年9月22日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注