🌹 分享 ElasticSearch 基础,请指教。
 
🌹🌹 如你对技术也感兴趣,欢迎交流。
 
🌹🌹🌹  如有对阁下帮助,请👍点赞💖收藏🐱🏍分享😀理论
Elasticsearch: The Official Distributed Search & Analytics Engine | ElasticElasticsearch is the leading distributed, RESTful, free and open search and analytics engine designed for speed, horizontal scalability, reliability, and easy management. Get started for free. https://www.elastic.co/elasticsearch/
https://www.elastic.co/elasticsearch/
Elasticsearch是基于Apache Lucene的搜索服务器它的最新版本是8.10.2。
Elasticsearch是一个实时的分布式开放源代码全文本搜索和分析引擎。可从RESTful Web服务界面访问它,并使用无模式的JSON(JavaScript对象表示法)文档存储数据。它基于Java编程语言构建,因此Elasticsearch可以在不同平台上运行。它使用户能够以很高的速度浏览大量的数据。
Elasticsearch & MYSQL

索引
对比关系型数据库,创建索引就等同于创建数据库
创建
PUT /shopping 
 
  
查看
单个索引
GET /shopping 
 
 全部索引
GET /_cat/indices?v 
 
 删除
DELETE /shopping 
 
 文档
创建
随机ID
POST /shopping/_doc
{
  "title":"华为meta60pro",
  "category":"手机",
  "images":"https://www.gulixueyuan.com/xm.jpg",
  "price":"6499.00"
}
 自定义ID
 自定义ID
 
POST /shopping/_doc/1000
{
  "title":"华为meta60pro max",
  "category":"手机",
  "images":"https://www.gulixueyuan.com/xm.jpg",
  "price":"7499.00"
}
PUT /shopping/_doc/1002
{
  "title":"华为meta60pro max尊享",
  "category":"手机",
  "images":"https://www.gulixueyuan.com/xm.jpg",
  "price":"8499.00"
} 
 
查询
主键查询
 不存在查询
 不存在查询
 

全部查询
GET /shopping/_search
修改
全量更新
PUT /shopping/_doc/1002
{
  "title":"华为meta60pro max尊享",
  "category":"手机",
  "images":"https://www.gulixueyuan.com/xm.jpg",
  "price":"12999.00"
}
 
 
 
 
 
 局部更新
POST /shopping/_update/1002
{
  "doc":{
    "title":"华为meta70pro max尊享"
  }
}
 删除
 删除
 
DELETE /shopping/_doc/1000 
 
复杂查询
请求路径方式
GET /shopping/_search?q=category:手机 请求体方式
 请求体方式
 
属性查询
GET /shopping/_search
{
  "query":{
    "match": {
      "category": "手机"
    }
  }
}
 全量查询
 全量查询
 
GET /shopping/_search
{
  "query":{
    "match_all": {}
  }
}
分页查询
GET /shopping/_search
{
  "query":{
    "match_all": {}
  },
  "from": 1,
  "size": 2
}

指定返回属性
GET /shopping/_search
{
  "query":{
    "match_all": {}
  },
  "from": 1,
  "size": 2,
  "_source": ["price","title"]
} 排序
 排序
 
GET /shopping/_search
{
  "query":{
    "match_all": {}
  },
  "from": 1,
  "size": 2,
  "_source": ["title"],
  "sort": [
    {
      "title": {
        "order": "desc"
      }
    }
  ]
}
错误

# 设置属性类型
PUT shopping/_mapping
{
    "properties": {
      "price": {
        "type": "text"
      },
      "title": {
        "fielddata": true, 
        "type": "text"
      }
    }
}
 
 
 组合查询
同时满足所有条件
## SQL: category="手机" AND price="6499.00"
GET /shopping/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
          "category": "手机"
        }
        },
        {
          "match": {
            "price": "6499.00"
          }
        }
      ]
    }
  }
}

满足某个条件
## SQL: category="手机" OR price="6499.00" 
GET /shopping/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
          "category": "手机"
        }
        },
        {
          "match": {
            "price": "6499.00"
          }
        }
      ]
    }
  }
} 
 
范围查询
## SQL: (category="手机" OR price=6499.00) and (price between 6500 and 10000) 
GET /shopping/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
          "category": "手机"
        }
        },
        {
          "match": {
            "price": 6499.00
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gt": 6500,
            "lt": 10000
          }
        }
      }
    }
  }
}
分页排序
# SQL: (category="手机" OR price=6499.00) and (price between 6500 and 10000)  limit 2 order by price desc
GET /shopping/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
          "category": "手机"
        }
        },
        {
          "match": {
            "price": 6499.00
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gt": 6500,
            "lt": 10000
          }
        }
      }
    }
  },
  "from": 0,
  "size": 2,
  "sort": [
      {
      "price": {
        "order": "desc"
      }
    }
  ]
} 高亮显示
 高亮显示
 
GET /shopping/_search
{
  "query":{
    "match_phrase": {
      "category": "手机"
    }
  },
  "highlight": {
    "fields": {
      "category": {}
    }
  }
}

聚合查询
分组聚合
GET /shopping/_search
{
 "aggs": {
   "cacl_avg": {
     "terms": {
       "field":"price"
     }
   }
 },
 "size": 0
}
 计算平均值
 计算平均值
 
GET /shopping/_search
{
 "aggs": {
   "cacl_avg": {
     "avg": {
       "field":"price"
     }
   }
 },
 "size": 0
}
 
 
创建映射关系
映射关系
PUT /user/_mapping
{
  "properties": {
    "user":{
      "type": "text",
      "index": true
    },
    "sex":{
      "type": "keyword",
      "index": false
    },
    "tel":{
      "type": "keyword",
      "index": true
    }
  }
}
数据

分词查询(type=text)

关键字查询 (type=keyword)
未被索引,不允许查询
 
 
总结
type=text ,index=true 时,可分词可查询;
type=keyword ,index=true 时,不可分词可查询;
index=false 时,不可查询







![web:[HCTF 2018]admin](https://img-blog.csdnimg.cn/0b38106fc5b44e148557fcbdcba1c734.png)











