ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ElasticSearch 基础操作

2022-02-01 17:00:38  阅读:173  来源: 互联网

标签:search shopping GET price 基础 ElasticSearch query 操作 match


在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
}

标签:search,shopping,GET,price,基础,ElasticSearch,query,操作,match
来源: https://www.cnblogs.com/Jacob-yang/p/15859236.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有