查看索引

GET _cat/indices?v&pretty
1677894228750.jpg

查看映射

GET test/_mappings

根据ID删除文档

DELETE test/_doc/RzrGCoYBybaXcbzwDnJf

创建映射

PUT test/_mapping
{
        "properties": {
            "customerJson": {
                "type": "object"
            },
         "operationMode":{
            "type":"integer"
        },
         "age":{
            "type":"long"
        },
        "birthday":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd"
        },
         "name":{
            "type":"text"
        },
        "friend":{
            "type":"nested",
            "properties":{
                "modifiedTime":{
                    "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd",
                    "type":"date"
                }
            }
        }
    }
}

设置字段数量

PUT test/_settings
{
  "index.mapping.total_fields.limit": 3
}

设置嵌套结构对象数量

PUT test/_settings
{
  "index.mapping.nested_objects.limit": 2000
}

设置副本数量

PUT test/_settings
{
"index" : {
        "number_of_replicas" : 0
    }
}

设置映射模式

1、true 是默认值,自动添加新出现的字段到 mapping 中。
2、false,不添加新出现的字段到 mapping 中,但可以在 doc 中保存新字段。
3、"strict" 不允许出现新字段,会报错。其中嵌套结构内部支持单独配置。

PUT test/_mapping
{
  "dynamic": false
}

将索引的 refresh_interval 设置为 1分钟

1、当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到。
2、refresh_interval 配置的刷新间隔。refresh_interval 的默认值是 1s。
3、单位:ms: 毫秒 s: 秒 m: 分钟

PUT my_index/_settings
{
    "index" : {
        "refresh_interval" : "1m"
    }
}

删除索引

DELETE test

增加别名

PUT test/_aliases
{
        "actions": [
            {
              "add": {
              "index": "test", 
              "alias": "mytest"
            }
            }
        ]
    }

删除别名

PUT test/_aliases
{
    "actions": [
        {"remove": {"index": "test", "alias": "mytest"}}
    ]
} 

迁移数据

POST /_reindex
{
  "source": {
    "index": "indexName"
  },
  "dest": {
    "index": "newIndexName"
  }
}

查询全部

`GET scrm_customer/_search`
{
    "query": {
    "match_all": {}
  }
}

查询总数

GET test/_search
{
  "track_total_hits":true
}

查询总数

GET scrm_customer/_count

查询最大值

GET test/_search
{
  "size": 0,
  "aggs": {
    "max_userId": {
      "max": {
        "field": "userId"
      }
    }
  }
}

去重查询,总数及只查所需字段

GET test/_search
{
  "explain":true,
    "from":0,
    "size":10,
    "timeout":"60s",
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "userId":{
                            "value":100024,
                            "boost":1
                        }
                    }
                }

            ],
            "adjust_pure_negative":true,
            "boost":1
        }
    },
    "_source":{
        "includes":[
            "userId"
        ],
        "excludes":[

        ]
    },
    "aggregations":{
        "DISTINCT_TOTAL_COUNT":{
            "cardinality":{
                "field":"userId"
            }
        }
    },
    "collapse":{
        "field":"userId"
    }
}

常见问题

1、"type":"exception","reason":"Elasticsearch exception [type=mapper_parsing_exception, reason=The number of nested documents has exceeded the allowed limit of [10000]. This limit can be set by changing the [index.mapping.nested_objects.limit] index level setting.]
2、Validation Failed: 1: this action would add [9] total shards, but this cluster currently has [2997]/[3000] maximum shards open;], data=null)
3、"type":"illegal_argument_exception","reason":"Limit og total fields [1000] has been exceeded"

限制

1、object以及nested类型的字段,也会创建properties,会占用字段数量,每个index建议不超过1000个properties
2、创建索引不能超过1000
3、规格容量评估
规格容量评估
4、ES性能优化
性能优化

应用

1、Elasticsearch在SCRM的应用及优化


往后余生
0 声望1 粉丝

« 上一篇
ES