TOPIC

磷光与烟火逆流

一个敲代码的程序猿

ElasticSearch 基操勿六

在Kibana 开发工具下操作
使用url body 传参使用json 数据格式

# 新增索引
PUT shopping

# 获取索引信息
GET shopping

# 获取所有索引
GET _cat/indices?v

# 删除索引
DELETE shopping

# 给shopping添加内容 文档创建
POST shopping/_doc
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://www.gulixueyuan.com/xm.jpg",
  "price":3999.99
  
}

# 文档创建 自定义id(主键) PUT 请求也可创建不过是幂等性的
POST shopping/_doc/1001
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://www.gulixueyuan.com/xm.jpg",
  "price":3999.99
  
}


# 文档查询 主键查询 _id
GET shopping/_doc/1001
# 查询没有的主键
GET shopping/_doc/1002
# 查询索引下所有数据
GET shopping/_search


# 文档修改
# 全量修改 因为是幂等性的所以可以使用PUT
PUT shopping/_doc/1001
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://www.gulixueyuan.com/xm.jpg",
  "price":3999.00
  
}
# 局部修改
POST shopping/_update/1001
{
  "doc":{
    "title":"华为"
  }
}

# 删除
DELETE shopping/_doc/1001

# 条件查询
# 请求地址查询
GET shopping/_search?q=category:小米
# 请求体查询
GET shopping/_search
{
  "query": {
    "match": {
      "category":"小米"
    }
  }
}
# 全量查询
GET shopping/_search
{
  "query": {
    "match_all": {}
  }
}
# 分页查询
GET shopping/_search
{
  "query": {
    "match_all": {}
  },
  "from": 2, 
  "size": 2 
}


# 查询控制查询字段
GET shopping/_search
{
  "query": {
    "match_all": {}
  },
  "from": 2, 
  "size": 2,
  "_source": ["title","price"]
}


# 查询排序
GET shopping/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0, 
  "size": 10,
  "_source": ["title","price"],
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
GET shopping/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0, 
  "size": 10,
  "_source": ["title","price"],
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}


# 多条件查询
# and 条件
GET shopping/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "category": "小米"
          }
        },
        {
          "match": {
            "price": 3999.00
          }
        }
      ]
    }
  },
  "from": 0, 
  "size": 10,
  "_source": ["title","price"],
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}
# or 条件
GET shopping/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "category": "小米"
          }
        },
        {
          "match": {
            "price": 3999.00
          }
        }
      ]
    }
  }
}


# 范围 条件 gt 大于 gte 大于等于 lt 小于 lte 小于等于
GET shopping/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "category": "小米"
          }
        },
        {
          "match": {
            "price": 3999.00
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gte": 3999.5
          }
        }
      }
    }
  }
}

# query match 是倒排索引 分词 全文检索  
# 想要完全匹配 使用 match_phrase
GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "小米"
    }
  }
}

GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "小"
    }
  }
}

GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "米"
    }
  }
}

GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "小华"
    }
  }
}

# 高亮显示

GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "小"
    }
  },
  "highlight": {
    "fields": {
      "category": {}
    }
  }
}


# 聚合查询
GET shopping/_search
{
  "aggs": {
    "price_group": {
        "terms": {
          "field": "price"
      }
    }
  },
  "size":0
}


GET shopping/_search
{
  "aggs": {
    "price_avg": {
        "avg": {
          "field": "price"
      }
    }
  },
  "size":0
}

posted @ 2022-02-01 16:54  Jacobyang  阅读(34)  评论(0编辑  收藏  举报