关于java:Elasticsearch-常用命令

48次阅读

共计 2141 个字符,预计需要花费 6 分钟才能阅读完成。

查看索引

GET _cat/indices?v&pretty

查看映射

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.]

限度

1、object 以及 nested 类型的字段,也会创立 properties,会占用字段数量,每个 index 倡议不超过 1000 个 properties
2、创立索引不能超过 1000

正文完
 0