什么是ElasticSearch

Elasticsearch 是一个分布式的、开源的搜索分析引擎,支持各种数据类型,包括文本、数字、地理、结构化、非结构化。

Elasticsearch 是基于 Apache Lucene 的。

Elasticsearch 因其简单的 REST API、分布式特性、告诉、可扩展而闻名。

Elasticsearch 是 Elastic 产品栈的核心,Elastic 产品栈是个开源工具集合,用于数据接收、存储、分析、可视化。

Elasticsearch 的竞争对手只有一个,Apache Solr,有着和 Elasticsearch 相似的特性,但 Solr 的发展势头远不及 Elasticsearch。

基本名词解释

名称含义
index  索引
type 文档类型    新版的ES一个索引下只允许一个文档类型 
/index/type 就相当于mysql的一个表
document 文档     对应mysql的row ( 一条记录)
field字段    对应mysql的 column
mapping 对应 mysql的 schema

每条记录都会有一个隐藏的主键_id

操作说明
GET获取 幂等操作,安全操作,无论操作多少次,资源状态不会改变 GET uri/xxx
PUT更新或者创建 幂等操作, PUT uri/xxxx
DELETE删除 幂等操作, DELETE uri/xxxx
POST创建 非幂等操作, POST uri

安全和幂等的意义在于:当操作没有达到预期的目标时,我们可以不停的重试,而不会对资源产生副作用。从这个意义上说,POST操作往往是有害的,但很多时候我们还是不得不使用它。

  

索引创建

1. 创建新的索引

PUT index001

2. 索引设置

2.1 分片设置

number_of_shards: 索引的主分片数,默认值是5。这个配置在索引创建后不能修改。 7.0版本后,主分片数默认为1。
number_of_replicas: 每个主分片的备份分片数量,默认为1。这个配置可以随时修改。当只有一个节点时设置为0,因为副本没有意义。

示例:

// 必须首次创建索引就指定主分片数量 ,否则就会默认为1,且无法修改
PUT my_test_index
{
    "settings": {
    "number_of_shards" :   1,
    "number_of_replicas" : 0
    }
}


// 修改副分片数量
PUT my_test_index/_settings
{
    "number_of_replicas" : 2
}
2.2 Mapping设置

首先查看刚刚创建的索引的mapping是什么样子的。

GET index001/_mapping

// 结果
{
    "index001""{
        "mappings":{}
     }
}

可以看到新建的索引,其没有mappings,下面添加这个index的文档类型,并设置mappings

PUT index001/_mapping
{
   properties":{
         "title":{"type":"text","store":"true"},
         "description": {"type":"text","index":"false"},
         "price": {"type":"double"},
         "onSale":{"type":"boolean"},
         "type": {"type":"integer"},
         "createDate":{"type":"date"}
   }
}

示例

创建新索引

PUT recent_order_index
{
  "settings": {
    "number_of_replicas": 3,
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "cargoId": { "type" : "long"},
      "driverUserName": { "type" : "keyword"},
      "loadAddress": { "type" : "text"},
      "searchable": { "type" : "boolean"},
      "companyId": { "type" : "long"}
    }
  }
}

查询索引基本信息

查询索引的主分片数量信息以及 mapping信息

GET recent_order_index

// 返回信息
{
  "recent_order_index" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "cargoId" : {
          "type" : "long"
        },
        "companyId" : {
          "type" : "long"
        },
        "driverUserName" : {
          "type" : "keyword"
        },
        "loadAddress" : {
          "type" : "text"
        },
        "searchable" : {
          "type" : "boolean"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "recent_order_index",
        "creation_date" : "1626692619057",
        "number_of_replicas" : "0",
        "uuid" : "Gb3vT1dDQK-LOGV612bEbQ",
        "version" : {
          "created" : "7130299"
        }
      }
    }
  }
}

添加文档

POST recent_order_index/_doc
{
  "cargoId": 7,
  "driverUserName":"张三",
  "loadAddress": "南京市玄武区2",
  "searchable": true,
  "companyId": 666
}

查询文档

GET /recent_order_index/_search
{
  "query": {
    "match": {
      "cargoId": "1"
    }
  },
  "sort":{
    "companyId" : "desc"
  }
}

更新文档

POST /recent_order_index/[type]/[id]/_update
{
   "doc": {
     "field1" : "value",
     "field2" : [
       "value22",  "value33"
      ]
   }
}

根据主键获取文档

注意需要写_doc,文档类型。后面一串为主键_id

GET recent_order_index/_doc/MzhvvnoBA6J-Ju8z1JDo
最后修改:2022 年 03 月 30 日
如果觉得我的文章对你有用,请随意赞赏