背景
我local操作的es版本是7.16.3,具体参考juejin.cn/post/731010…
1. 通用命令以及基础测试数据准备
GET /*
# 查看有哪些索引
GET _cat/indices
GET _cat/indices?v
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "我要做你爸爸"
}
PUT goods/_doc/1
{
"name":"xiangjiao",
"price":40,
"producer":"feilvbin",
"description":"haochi tian",
"tags":[ "xiangjiao","haochi"]
}
PUT goods/_doc/2
{
"name":"pingguo",
"price":60,
"producer":"zhonggguo",
"description":"cui",
"tags":[ "haokan","xiang"]
}
PUT goods/_doc/3
{
"name":"lizi",
"price":10,
"producer":"zhonggguo",
"description":"zide",
"tags":[ "suan","tian"]
}
PUT goods/_doc/4
{
"name":"boluo",
"price":74,
"producer":"malaixiya",
"description":"getouda",
"tags":[ "huang","youci"]
}
PUT goods/_doc/5
{
"name":"mihoutao",
"price":45,
"producer":"xinxilan",
"description":"suan",
"tags":[ "lv","da"]
}
PUT goods/_doc/6
{
"name":"xigua",
"price":109,
"producer":"zhongguo",
"description":"haochi",
"tags":[ "da","haochi"]
}
PUT goods/_doc/7
{
"name":"pingguo",
"price":120,
"producer":"zhonggguo",
"description":"cui",
"tags":[ "da","haochi"]
}
PUT goods/_doc/8
{
"name":"pingguo",
"price":120,
"producer":"zhonggguo",
"description":"cui",
"tags":[ "haokan","xiang"]
}
PUT goods/_doc/9
{
"name":"pingguo",
"price":245,
"producer":"chaoxian",
"description":"cui",
"tags":[ "haokan","lv"]
}
GET _cat/indices?v
2. 索引增删改查
PUT goods/_doc/2
{
"name":"pingguo",
"price":34,
"producer":"zhongguo",
"description":"haixing,bijiaotian",
"tags":[ "yuan","red"]
}
PUT goods/_doc/3
{
"name":"fengli",
"price":23,
"producer":"zhongguo",
"description":"youdiansuan,bijiaotian",
"tags":[ "chang","yellow","fang"]
}
GET goods/_doc/2
GET /goods/_search?q=producer:zhongguo
GET /goods/_search
{
"query": {
"match": {
"producer": "zhongguo"
}
}
}
#修改一个索引的数据, post会覆盖,没有就新增
POST goods/_update/1
{
"doc":{
"name": "xiangjiao_update_next"
}
}
3. 排序查找
# 全查
GET goods/_search
{
"query": {
"match_all": {}
}
}
# 精确查找
GET goods/_search
{
"query": {
"match": {
"name": "pingguo"
}
}
}
# 排序过程中只能使用可排属性,数字日期
GET goods/_search
{
"query": {
"match": {
"name": "pingguo"
}
},
"sort":[
{
"price":{
"order": "desc"
}
}
]
}
4. 排序查找和只返回部分字段
GET goods/_search
{
"query": {
"match": {
"name": "pingguo"
}
},
"sort":[
{
"price":{
"order": "asc"
}
}
],
"_source": ["name", "price"],
"from":0,
"size":2
}
5. bool查询
must: 必须匹配 相当于and, must not 是不匹配 相当于not, should 是可以匹配相当于or
GET goods/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "pingguo"
}
},
{
"match": {
"producer": "zhonggguo"
}
}
]
}
}
}
6. 过滤查询
filter 属于bool查询内的过滤条件,如果过滤条件使用should的话,检索的结果可能会出现问题,建议使用must关键字
GET goods/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "pingguo"
}
}
],
"filter": {
"range": {
"price": {
"gte": 100,
"lte": 200
}
}
}
}
}
}
7. 数据检索
# 检索数组类型的数据,直接通过全文检索,可以匹配多个值,通过空格间隔
GET goods/_search
{
"query": {
"match": {
"tags": "da lv"
}
}
}
8. 高亮检索
# 前置标签和后置标签要分开写,外双内单
GET /goods/_search
{
"query": {
"match": {
"name": "mihoutao"
}
},
"highlight": {
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>",
"fields": {
"name": {}
}
}
}
9. 聚合查询
GET /goods/_search
{
"size": 0,
"aggs": {
"avg_price": {
"max": {
"field": "price"
}
}
}
}
GET /goods/_search
{
"size": 20,
"query": {
"match": {
"name": "pingguo"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
# 聚合函数一定是先查询,然后再使用聚合函数
GET /goods/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "pingguo"
}
}
],
"filter": {
"range": {
"price": {
"gte": 100,
"lt": 200
}
}
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
GET /goods/_search
{
"size": 0,
"aggs": {
"range_search": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 50
},
{
"from": 50,
"to": 150
},
{
"from": 150,
"to": 300
}
]
},
"aggs": {
"sum_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
10. mapping关系
GET goods/_mapping
# dynamic :false,true, strict
#false不用指定,可以随时插入动态的属性,但是不会通过动态的属性进行查询,不会为动态属性创建索引
#true,可以随时插入动态的属性,会为动态属性创建索引
#strict,必须添加指定的属性,添加动态的属性会报错
GET my_index_1
PUT my_index_1
{
"mappings": {
"dynamic": "false",
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"long"
}
}
}
}
11. mapping中index的解释
PUT my_index5
{
"mappings": {
"dynamic": "false",
"properties":{
"name":{
"type":"text",
"index": "true"
},
"add":{
"type":"text",
"index": "false"
}
}
}
}
GET my_index5/_mapping
PUT my_index5/_doc/1
{
"name":"kitty",
"add":"beijing"
}
PUT my_index5/_doc/2
{
"name":"kitty",
"add":"shanghai"
}
# index 属性默认为true,如果设置为false,则不为当前属性创建索引
GET my_index5/_search
{
"query": {
"match": {
"add": "beijing"
}
}
}
11. mapping中copy_to 属性
PUT my_index4
{
"mappings": {
"dynamic": "false",
"properties":{
"first_name":{
"type":"text",
"copy_to": "full_name"
},
"last_name":{
"type":"text",
"copy_to": "full_name"
},
"full_name":{
"type":"text"
}
}
}
}
# copy_to 参数把当前的值复制给指定的字段,所有的copy_to的值和copy_to属性赋给的值都会被保留
PUT my_index4/_doc/1
{
"first_name":"kitty",
"last_name":"tomcat"
}
PUT my_index4/_doc/2
{
"first_name":"kitty",
"last_name":"python"
}
PUT my_index4/_doc/3
{
"first_name":"kitty",
"last_name":"php",
"full_name":"java"
}
GET my_index4/_search
{
"query": {
"match": {
"full_name": "java"
}
}
}
12. mapping中对象属性
PUT my_index7/_doc/1
{
"name": "roy",
"age": 13,
"addr":{
"address":"jiangsu",
"tel": "15371452705"
}
}
PUT my_index7/_doc/2
{
"name": "jack",
"age": 20,
"addr":{
"address":"yancheng",
"tel": "15371452706"
}
}
GET my_index7/_search
{
"query": {
"match": {
"addr.address": "jiangsu"
}
}
}
GET my_index7/_mapping
13. 分词器解释
ik分词只能按照词来分,默认分词按照字来分
PUT my_index8
{
"mappings": {
"properties":{
"content":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
GET my_index8/_mapping
PUT my_index8/_doc/1
{
"content":"今天是个好日子"
}
PUT my_index8/_doc/2
{
"content":"心想的事儿都能成"
}
PUT my_index8/_doc/3
{
"content":"今天我必须打扫卫生"
}
GET my_index8/_search
{
"query": {
"match": {
"content": "今天"
}
}
}
14. python操作es
from elasticsearch import Elasticsearch
es = Elasticsearch(['127.0.0.1:9200'], timeout= 3000)
es.indices.create(index='python_es01', ignore=400)
es.index(index='python_es01', doc_type='_doc', id = 1, body = {"name":"kitty", "age": 50})
es.index(index='python_es01', doc_type='_doc', body = {"name":"kitty", "age": 50})
def query01():
res = es.get(index='python_es01', doc_type="_doc", id = 1)
return res
if __name__ == '__main__':
result = query01()
print(result)