Elasticsearch之索引简单应用
阅读原文时间:2023年09月06日阅读:1

本篇所有操作都在 Kibana 上执行

PUT product
{
  // 索引设置
  "settings": {
    // 分片数量
    "number_of_shards": 3,
    // 副本数量
    "number_of_replicas": 1
  },
  // 索引字段映射
  "mappings": {
    // 字段属性
    "properties": {
        // 商品名称
        "name":{
          // 字段类型为文本
          "type":"text"
        },
        // 商品标签
        "label":{
          "type":"keyword"
        },
        "price":{
           "type": "scaled_float",
           // 比例因子设置为100 在ES中会按分存储
           // 注意:scaling_factor属性是只针对scaled_float这个数据类型才有
            "scaling_factor": 100
        },
        // 商品状态
        "status":{
          "type":"integer"
        },
        // 创建日期
        "create_date":{
          "type":"date"
        }
    }
  }
}

执行命令,我们会得到如下返回信息,表示创建成功

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "product"
}

获取索引全部信息:

## 命令格式  GET {索引名称}
GET product

返回结果:

{
  "product": {
    "aliases": {},
    "mappings": {
      "properties": {
        "create_date": {
          "type": "date"
        },
        "label": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "price": {
          "type": "scaled_float",
          "scaling_factor": 100
        },
        "status": {
          "type": "integer"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "3",
        "provided_name": "product",
        "creation_date": "1693981437123",
        "number_of_replicas": "1",
        "uuid": "awfrhothQaeoL2bHvufN5w",
        "version": {
          "created": "8090199"
        }
      }
    }
  }
}

查看索引 Mapping 信息

GET product/_mapping

返回结果:

{
  "product": {
    "mappings": {
      "properties": {
        "create_date": {
          "type": "date"
        },
        "label": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "price": {
          "type": "scaled_float",
          "scaling_factor": 100
        },
        "status": {
          "type": "integer"
        }
      }
    }
  }
}

查看索引 settings 信息

GET product/_settings

返回结果:

{
  "product": {
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "3",
        "provided_name": "product",
        "creation_date": "1693981437123",
        "number_of_replicas": "1",
        "uuid": "awfrhothQaeoL2bHvufN5w",
        "version": {
          "created": "8090199"
        }
      }
    }
}

查看索引 aliases 信息

GET product/_alias

返回结果:

{
  "product": {
    "aliases": {}
  }
}

目前我们没设置别名,所以返回为空

单个新增

语法:

PUT {索引名}/_doc/{文档id}
{
  {字段名}:{字段值/]
}

实例:

PUT product/_doc/1
{
  "name":"篮球",
  "label":["运动","球类"],
  "price":33.5,
  "status":0,
  "create_date":"2023-08-08T13:00:00"
}

执行成功将返回:

{
  "_index": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

批量新增

语法:

POST _bulk
{ action: { metadata }}
{ request body        }
{ action: { metadata }}
{ request body        }

注:bulk对 JSON 串的有着严格的要求。每个 JSON 串不能换行,只能放在同一行,同时,相邻的 JSON 串之间必须要有换行(delete语法除外).

实例:

POST _bulk
{"create":{"_index":"product","_id":2}}
{"name":"足球","label":["运动","球类"],"price":60.3,"status":0,"create_date":"2023-08-09T13:00:00"}
{"create":{"_index":"product","_id":3}}
{"name":"华为手机","label":["数码","手机"],"price":6999,"status":0,"create_date":"2023-08-31T13:00:00"}
{"create":{"_index":"product","_id":4}}
{"name":"苹果","label":["数码","手机"],"price":9999,"status":1,"create_date":"2023-08-31T13:00:00"}

返回结果:

{
  "took": 6,
  "errors": false,
  "items": [
    {
      "create": {
        "_index": "product",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "product",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "product",
        "_id": "4",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

查询所有

GET product/_search
{
    "query": {
        "match_all": {}
    }
}

返回

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "足球",
          "label": [
            "运动",
            "球类"
          ],
          "price": 60.3,
          "status": 0,
          "create_date": "2023-08-09T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "华为手机",
          "label": [
            "数码",
            "手机"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "苹果手机",
          "label": [
            "数码",
            "手机"
          ],
          "price": 9999,
          "status": 1,
          "create_date": "2023-09-01T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "篮球",
          "label": [
            "运动",
            "球类"
          ],
          "price": 33.5,
          "status": 0,
          "create_date": "2023-08-08T13:00:00"
        }
      }
    ]
  }
}

响应的数据结果分为两部分:

{
  ----------------first part:分片副本信息--------------------
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  ---------------second part:查询的数据集---------------------
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [{}]
  }
}

查询标签带手机的商品

GET product/_search
{
    "query": {
        "match":  {
          "label": "手机"
        }
    }
}

返回:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.5908618,
    "hits": [
      {
        "_index": "product",
        "_id": "3",
        "_score": 0.5908618,
        "_source": {
          "name": "华为手机",
          "label": [
            "数码",
            "手机"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "4",
        "_score": 0.5908618,
        "_source": {
          "name": "苹果手机",
          "label": [
            "数码",
            "手机"
          ],
          "price": 9999,
          "status": 1,
          "create_date": "2023-09-01T13:00:00"
        }
      }
    ]
  }
}

过滤状态为0的

GET product/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "label": "手机"
        }
      },
      "filter": {
        "term": {
          "status": 0
        }
      }
    }
  }
}

返回结果:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.5908618,
    "hits": [
      {
        "_index": "product",
        "_id": "3",
        "_score": 0.5908618,
        "_source": {
          "name": "华为手机",
          "label": [
            "数码",
            "手机"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      }
    ]
  }
}

查询结果里面都有一个 _score字段,一般 Elasticsearch 根据相关评分排序,相关评分是根据文档与语句的匹配度来得出, _score值越高说明匹配度越高。相关性(relevance)概念在Elasticsearch中非常重要,而这也是它与传统关系型数据库中记录只有匹配和不匹配概念最大的不同。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章