• es常用语言


    参考链接

    Relational DBElasticsearch
    数据库(database) 索引(indices)
    表(tables) types
    行(rows) documents
    字段(columns) fields

    库表行字段,index,type,id,fields,  索引类型文档字段

    创建一篇文档

    PUT t1/doc/1     
    {
     "name": "小黑的小姨妈",
     "age": 18
    }

    index/type/id  id是单个文档

    查询所有索引

    GET _cat/indices?v

    查询指定的索引信息

    GET t1

    查询文档信息

    GET t1/doc/1           #查询指定文档
    GET t1/doc/_search #查询所有文档

    删除指定索引

    DELETE /t1

    修改文档

    PUT zhifou/doc/1
    {
      "name":"顾老二",
      "age":30,
      "from": "gu",
      "desc": "皮肤黑、武器长、性格直",
      "tags": ["", "", ""]
    }
    
    我们要将黑修改成黄:
    POST zhifou/doc/1/_update
    {
      "doc": {
        "desc": "皮肤很黄,武器很长,性格很直",
        "tags": ["很黄","很长", "很直"]
      }
    }

    查询字符串

    方式一:
    GET zhifou/doc/_search?q=from:gu
    属性是from,属性值是gu的文档
    
    方式二:
    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      }
    }

    match条件查询,查询含有(匹配)指定字段值的文档

    我们查看来自顾家的都有哪些人

    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      }
    }

    match查询全部文档

    查询zhifou索引下的doc类型中的所有文档,那就是查询全部

    GET zhifou/doc/_search
    {
      "query": {
        "match_all": {}
      }
    }

    match_phrase(短语查询)

    GET t1/doc/_search
    {
      "query": {
        "match_phrase": {
          "title": {
            "query": "中国"
          }
        }
      }
    }
    
    title字段中包含短语中国
    GET t1/doc/_search
    {
      "query": {
        "match_phrase": {
          "title": {
            "query": "中国世界",
            "slop": 2
          }
        }
      }
    }
    slop了。相当于正则中的中国.*?世界。这个间隔默认为0,指定短语间隔

    match_phrase_prefix(最左前缀查询)

    GET t3/doc/_search
    {
      "query": {
        "match_phrase_prefix": {
          "desc": "bea"
        }
      }
    }
    desc字段bea开头字
    GET t3/doc/_search
    {
      "query": {
        "match_phrase_prefix": {
          "desc": {
            "query": "bea",
            "max_expansions": 1
          }
          
        }
      }
    }
    max_expansions来设置最大的前缀扩展数量

    multi_match(多字段查询)

    方法一:
    GET t3/doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "title": "beautiful"
              }
            },
            {
              "match": {
                "desc": "beautiful"
              }
            }
          ]
        }
      }
    }
    
    方法二:
    GET t3/doc/_search
    {
      "query": {
        "multi_match": {
          "query": "beautiful",
          "fields": ["title", "desc"]
        }
      }
    }
    
    multi_match甚至可以当做match_phrase和match_phrase_prefix使用,只需要指定type类型即可:
    GET t3/doc/_search
    {
      "query": {
        "multi_match": {
          "query": "gi",
          "fields": ["title"],
          "type": "phrase_prefix"
        }
      }
    }
    GET t3/doc/_search
    {
      "query": {
        "multi_match": {
          "query": "girl",
          "fields": ["title"],
          "type": "phrase"
        }
      }
    }

    term查询#单个匹配项

    POST _analyze
    {
      "analyzer": "standard",
      "text": "Beautiful girl!"
    }
    # 结果
    ["beautiful", "girl"]
    
    GET w10/doc/_search
    {
      "query": {
        "term": {
          "t1": "beautiful"
        }
      }
    }

    terms查询#多个匹配项

    GET w10/doc/_search
    {
      "query": {
        "terms": {
          "t1": ["beautiful", "sexy"]
        }
      }
    }

    排序查询:sort#按某个字段降序查询

    查询顾府都有哪些人,并根据age字段按照降序

    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ]
    }

    按某个字段升序查询

    GET zhifou/doc/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "age": {
            "order": "asc"
          }
        }
      ]
    }

    分页查询:from/size#

    GET zhifou/doc/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ], 
      "from": 2,
      "size": 1
    }
    
    from:从哪开始查
    size:返回几条结果

    bool查询must#(and)

    单个条件查询:布尔查询所有from属性为gu的数据:
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "from": "gu"
              }
            }
          ]
        }
      }
    }
    多个条件查询:想要查询from为gu,并且age为30的数据
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "from": "gu"
              }
            },
            {
              "match": {
                "age": 30
              }
            }
          ]
        }
      }
    }

    bool查询should(or)

    查询只要是from为gu或者tags为闭月的数据
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "from": "gu"
              }
            },
            {
              "match": {
                "tags": "闭月"
              }
            }
          ]
        }
      }
    }

    bool查询must_not(not)

    查询from既不是gu并且tags也不是可爱,还有age不是18的数据
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "from": "gu"
              }
            },
            {
              "match": {
                "tags": "可爱"
              }
            },
            {
              "match": {
                "age": 18
              }
            }
          ]
        }
      }
    }

    bool查询filter

    查询from为gu,age大于25的数据 。比较符号有gt gte lt lte
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "from": "gu"
              }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "gt": 25
              }
            }
          }
        }
      }
    }
    
    查询from是gu,age在25~30之间
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "from": "gu"
              }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "gte": 25,
                "lte": 30
              }
            }
          }
        }
      }
    }
    must改成should,满足下面的filter但不满足上面的match也会显示出来
    GET zhifou/doc/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "from": "sheng"
              }
            }
          ],
          "filter": {
            "range": {
              "age": {
                "lte": 25
              }
            }
          }
        }
      }
    }
    • must:与关系,相当于关系型数据库中的and
    • should:或关系,相当于关系型数据库中的or
    • must_not:非关系,相当于关系型数据库中的not
    • filter:过滤条件。
    • range:条件筛选范围。
    • gt:大于,相当于关系型数据库中的>
    • gte:大于等于,相当于关系型数据库中的>=
    • lt:小于,相当于关系型数据库中的<
    • lte:小于等于,相当于关系型数据库中的<=

    结果过滤:_source

    在所有的结果中,我只需要查看name和age两个属性,其他的不要
    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "name": "顾老二"
        }
      },
      "_source": ["name", "age"]
    }

    avg

    查询from是gu的人的平均年龄
    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "aggs": {
        "my_avg": {
          "avg": {
            "field": "age"
          }
        }
      },
      "_source": ["name", "age"]
    }
    首先匹配查询from是gu的数据。在此基础上做查询平均值的操作,这里就用到了聚合函数,其语法被封装在aggs中,而my_avg则是为查询结果起个别名,封装了计算出的平均值。那么,要以什么属性作为条件呢?是age年龄,查年龄的什么呢?是avg,查平均年龄。
    
    只想看平均值
    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "aggs": {
        "my_avg": {
          "avg": {
            "field": "age"
          }
        }
      },
      "size": 0, 
      "_source": ["name", "age"]
    }

    max

    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "aggs": {
        "my_max": {
          "max": {
            "field": "age"
          }
        }
      },
      "size": 0
    }

    min

    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "aggs": {
        "my_min": {
          "min": {
            "field": "age"
          }
        }
      },
      "size": 0
    }

    sum

    GET zhifou/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      },
      "aggs": {
        "my_sum": {
          "sum": {
            "field": "age"
          }
        }
      },
      "size": 0
    }

    分组查询和分组聚合

    查询所有人的年龄段,并且按照15~2020~25,25~30分组,
    GET zhifou/doc/_search
    {
      "size": 0, 
      "query": {
        "match_all": {}
      },
      "aggs": {
        "age_group": {
          "range": {
            "field": "age",
            "ranges": [
              {
                "from": 15,
                "to": 20
              },
              {
                "from": 20,
                "to": 25
              },
              {
                "from": 25,
                "to": 30
              }
            ]
          }
        }
      }
    }
    
    查询所有人的年龄段,并且按照15~2020~25,25~30分组,并且算出每组的平均年龄。每个小组内的数据做平均年龄处理。
    GET zhifou/doc/_search
    {
      "size": 0, 
      "query": {
        "match_all": {}
      },
      "aggs": {
        "age_group": {
          "range": {
            "field": "age",
            "ranges": [
              {
                "from": 15,
                "to": 20
              },
              {
                "from": 20,
                "to": 25
              },
              {
                "from": 25,
                "to": 30
              }
            ]
          },
          "aggs": {
            "my_avg": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      }
    }

      

  • 相关阅读:
    python面试题目
    Mysql综合练习作业50题
    django rest framework自定义返回格式
    mongodb副本集部署
    celery学习笔记
    npm学习笔记
    awk学习笔记
    SaltStack grains学习笔记
    nginx + uwsgi + centos7部署django
    Reids集群安装
  • 原文地址:https://www.cnblogs.com/machangwei-8/p/14979956.html
Copyright © 2020-2023  润新知