es version 7.3.1
curl ip:9200 -u legend_admin:legend_admin
{
"name" : "LFA-L0294069",
"cluster_name" : "legend-es-dev",
"cluster_uuid" : "1TwYiWGlR0CJLKB__B5JPA",
"version" : {
"number" : "7.3.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "4749ba6",
"build_date" : "2019-08-19T20:19:25.651794Z",
"build_snapshot" : false,
"lucene_version" : "8.1.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
ip:9200 开发elastic/elastic
1、索引查询
https://www.cnblogs.com/pilihaotian/p/5830754.html
我们通常用用_cat API检测集群是否健康。 确保9200端口号可用:
curl 'http://ip:9200/_cat/health?v' -u legend_admin:legend_admin
绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用.
2.通过如下语句,我们可以获取集群的节点列表:
curl 'ip:9200/_cat/nodes' -u legend_admin:legend_admin
3。通过如下语句,列出所有索引:
curl 'ip:9200/_cat/indices?v' -u legend_admin:legend_admin
get _cat/nodes
2、
get _cat/health
get _cat/nodes
get _cat/indices
get _cat/master?h=ip,n 查询输出master的ip以及node name
curl -XGET 'ip:9200/msp_dx_task_index?pretty' -u legend_admin:legend_admin
curl -XDELETE 'ip:9200/customer?pretty' 删除索引
curl -XPUT 'ip:9200/customer?pretty' -u legend_admin:legend_admin
----
注意keyword类型不分词,完整匹配
text类型分词,模糊匹配
term针对keyword类型可完整匹配,对text类型查询不到
match 模糊匹配,针对分词
----
8 修改数据
curl -XPUT 'ip:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
curl -XPUT 'ip:9200/customer/external/1?pretty' -d '
{
"name": "Jane Doe"
}'
上述命令语句是:先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。
9.更新数据
9.1 这个例子展示如何将id为1文档的name字段更新为Jane Doe:
curl -XPOST 'ip:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe" }
}'
9.2 这个例子展示如何将id为1数据的name字段更新为Jane Doe同时增加字段age为20:
curl -XPOST 'ip:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
9.3 也可以通过一些简单的scripts来执行更新。一下语句通过使用script将年龄增加5:
curl -XPOST 'ip:9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age = 5"
}'
10 删除数据
删除数据那是相当的直接. 下面的语句将执行删除Customer中ID为2的数据:
curl -XDELETE 'ip:9200/customer/external/2?pretty'
https://www.cnblogs.com/shihuc/p/6214248.html
11 curl http://ip:9200/_cat 帮助
curl http://ip:9200/_cat/master?v
get _cat/master?v
3. allocation指令
该指令提供一个快照,反映当前节点有多少个分片(shard)以及用了多少磁盘空间(disk)。
1 [elastic@t0-tkonline-cms-search01 ~]$ curl http://ip:9200/_cat/allocation?v
curl http://10.90.7.2:9201/_cat/count?v #获取当前集群中有多少个documen
curl http://10.90.7.2:9201/_cat/count/tksearch?v #获取tksearch这个index
222 、https://www.cnblogs.com/pilihaotian/p/5830754.html
curl 'ip:9200/bank/_search?q=*&pretty' 查询索引下的所有数据
curl -XPOST 'ip:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
curl -H "Content-Type: application/json" -XPOST 'ip:9200/customer/_search?pretty' -u legend_admin:legend_admin -d '{"query": { "match_all": {} }, "from": 1, "size": 2}'
curl -H "Content-Type: application/json" -XPOST 'ip:9200/msp_dx_task_index/_search?pretty' -u legend_admin:legend_admin -d '{"query": { "match_all": {} }, "sort": [
{
"updateTime": {
"order": "desc"
}
}
]}'
curl -H "Content-Type: application/json" -XPUT 'ip:9200/msp_dx_task_index/_mapping/_doc' -u legend_admin:legend_admin -d PUT /msp_dx_task_index/_mapping
{
"properties":{
"updateTime":{
"type":"text",
"fielddata":true
}
}
}
https://blog.csdn.net/apple9005/article/details/90415558/ 更改字段类型
PUT /msp_dx_task_index/_mapping
{
"properties":{
"updateTime":{
"type":"text",
"fielddata":true
},
"synchDate":{
"type":"text",
"fielddata":true
}
}
}
批量更新
POST /infomations/infomations/_update_by_query
JSON请求格式
{
"query": {
"match": {
"status": "UP_SHELF"
}
},
"script": {
"inline": "ctx._source['status'] = 'DOWN_SHELF'"
}
}
POST请求/索引/文档名/_update_by_query
主要看一下下面的script
ctx._source[字段名] = “值”;ctx._source[字段名] = “值”;
多个的话就用分号隔开。
-- 造数据
curl -H "Content-Type: application/json" -XPUT 'ip:9200/msp_dx_task_index/_doc/test1?pretty' -u legend_admin:legend_admin -d '{ "matchNum" : "1",
"srcLine" : "1",
"taskNo" : "taskNo1",
"synchDate" : "20200201",
"ntmsRecordNum" : "1",
"updateTime" : "2020-06-04 10:32:09",
"asrIsTransferred" : "1",
"mspMatchFlg" : "1"}'
curl -H "Content-Type: application/json" -XPUT 'ip:9200/msp_dx_task_index/_doc/test2?pretty' -u legend_admin:legend_admin -d '{"matchNum" : "2",
"srcLine" : "1",
"taskNo" : "taskNo1",
"synchDate" : "20200202",
"ntmsRecordNum" : "2",
"updateTime" : "2020-06-05 10:32:09",
"asrIsTransferred" : "2",
"mspMatchFlg" : "1"}'
curl -H "Content-Type: application/json" -XPUT 'ip:9200/msp_dx_task_index/_doc/test3?pretty' -u legend_admin:legend_admin -d '{"matchNum" : "3",
"srcLine" : "2",
"taskNo" : "taskNo1",
"synchDate" : "20200206",
"ntmsRecordNum" : "2",
"updateTime" : "2020-06-06 10:32:09",
"asrIsTransferred" : "0",
"mspMatchFlg" : "1"}'
https://www.elastic.co/guide/cn/elasticsearch/guide/current/partial-updates.html
更新部分字段值,实际还是删除,但不会立刻删除
curl -H "Content-Type: application/json" -XPOST 'ip:9200/msp_dx_task_index/_doc/3a15c7d0_1121/_update?pretty' -u legend_admin:legend_admin -d '{ "script" : "ctx._source.synchDate=\"20200107\"" }'
curl ip:9200 -u legend_admin:legend_admin
下面例子展示如何返回两个字段(account_number balance)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
下面这个例子返回年龄大于40岁、不居住在ID的所有数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
、、、、、、、、、、、、、、
下面这个实例按照state分组,降序排序,返回balance的平均值:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'
-----------------
GET /synch_dx_ntms_loan_success_task_index/_search
{
"from": 0, "size": 2,
"sort": [ {
"SYNCH_DATE": {
"order": "asc"
}
}],
"query": {
"range": {
"SYNCH_DATE": {
"lt": "20200501"
}
}
}
--------------
1 范围查询的符号
符号 含义
gte greater-than or equal to, 大于或等于
gt greater-than, 大于
lte less-than or equal to, 小于或等于
lt less-than, 小于
GET book_shop/_search
{
"query": {
"range": {
"price": {
"gte": 40,
"lte": 80,
"boost": 2.0 // 设置得分的权重值(提升值), 默认是1.0
}
}
}
}
清空表
POST /msp_dx_task_index/_delete_by_query
{
"query": {
"match_all": {}
}
}
es scrollrequest,滚动查询,使用完后需要clear
https://blog.csdn.net/hzlafangm/article/details/88824874
1、每次1个,scrollid有效期10分钟
POST /customer/_search?scroll=10m
{
"size": 1
}
2、GET或者POST可以使用,URL不应包含index 或type名称 - 这在原始search请求中指定。现在通过scroll_id不要再传。
POST /_search/scroll
{
"scroll": "10m",
"scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAakn4WQkRwRDJkVlJUbzJHRmNUak9XXzdmdw=="
}
3、scroll超过超时时,将自动删除搜索上下文。但是,保持滚动打开会产生成本
DELETE /_search/scroll
{
"scroll_id": ["DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAakn4WQkRwRDJkVlJUbzJHRmNUak9XXzdmdw=="]
}
4、清除所有scrollid
DELETE /_search/scroll/_all
5、查询为空时,如果为text类型,只能查是否存在和Null,如果为keyword类型,则可以使用match查询
GET /msp_dx_task_recording_detail_index/_search
{
"from": 0, "size": 10,
"query": {
"match": {
"voiceId.keyword": ""
}
}
}
GET /msp_dx_task_recording_detail_index/_search
{
"from": 0, "size": 10,
"query": {
"bool" : {
"must_not": [
{
"exists": {
"field": "voiceId"
}
}
]
}
}
}
-------------------注意6-10选自 es 2.x-------------------------------------------------
6、fielddata 类似缓存使用时会全表扫描,需要设置最大内存
-- 查看es fielddata占用内存
GET /_stats/fielddata?fields=*
GET /_nodes/stats/indices/fielddata?fields=*
7、设置断路器
indices.fielddata.cache.size 和 indices.breaker.fielddata.limit
PUT /_cluster/settings
{
"persistent" : {
}
}
8、 doc values 和 fielddata
Elasticsearch 中的 Doc Values 常被应用到以下场景:
对一个字段进行排序
对一个字段进行聚合
某些过滤,比如地理位置过滤
某些与字段相关的脚本计算
doc values 支持大部分字段类型,但是text 字段类型不支持
如果确信某字段不需要排序或者聚合,或者从脚本中访问字段值,那么我们可以设置 doc_values = false,这样可以节省磁盘空间。
Fielddata针对text字段在默认时是禁用的
9、fielddata预加载
Fielddata 的载入可以使用 update-mapping API 对已有字段设置 lazy 或 eager 两种模式。
PUT /music/_mapping/_song
{
"tags": {
"type": "string",
"fielddata": {
"loading" : "eager"
}
}
}
10、序号
有种可以用来降低字符串 fielddata 内存使用的技术叫做 序号
和 fielddata 加载一样,全局序号默认也是延迟构建的。首个需要访问索引内 fielddata 的请求会促发全局序号的构建。由于字段的基数不同,这会导致给用户带来显著延迟这一糟糕结果。一旦全局序号发生重建,仍会使用旧的全局序号,直到索引中的分段产生变化:在刷新、写入或合并之后
PUT /music/_mapping/_song
{
"song_title": {
"type": "string",
"fielddata": {
"loading" : "eager_global_ordinals"
}
}
}
------------------------------------
11、嵌套和父子关系会使查询慢百倍
查询字段要尽量少,可以把多个字段合并为1个字段 The more fields a query_string or multi_match query targets, the slower it is
The way that Elasticsearch indexes numbers optimizes for range queries while keyword fields are better at term queries.
Avoid scripts
In general, scripts should be avoided. If they are absolutely needed, you should prefer the painless and expressions engines.
查询后会有缓存,时间范围可以提升到分小时等,这样可能会在其他用户查询时直接从缓存中取
index.store.preload 预加载filesystem
Use preference to optimize cache utilization
There are multiple caches that can help with search performance, such as the filesystem cache, the request cache or the query cache. Yet all these caches are maintained at the node level, meaning that if you run the same request twice in a row, have 1 replica or more and use round-robin, the default routing algorithm, then those two requests will go to different shard copies, preventing node-level caches from helping.
12、?refresh
POST
GET
POST /_refresh
GET /_refresh
POST /my_index/_close
POST /my_index/_open
POST /twitter/_flush
POST /
POST /_cache/clear
POST /twitter/_cache/clear?fielddata=true
POST /twitter/_cache/clear?query=true
POST /twitter/_cache/clear?request=true
13、
To clone an index, the index must be marked as read-only and have a cluster health status of green.
For example, the following request prevents write operations on my_source_index so it can be cloned. Metadata changes like deleting the index are still allowed.
PUT /my_source_index/_settings
{
"settings": {
"index.blocks.write": true
}
}
14、删除字段、修改字段类型
修改字段类可能同时需要修改代码查询方式。
新建一个新索引,把原索引数据移到新索引中,删除原索引,重建原索引,把新索引数据移到原索引中
POST _reindex
{
"source": {
"index": "synch_dx_ntms_loan_success_task_index",
"_source": ["ASR_TRANSFER_FLG", "CUSTOMER_ID",
"EXTEND_DEL_FLAG", "CUSTOMER_ID",
"ASR_TRANSFER_FLG", "HANDLE_STATE",
"MATCH_NUM", "MIS_MATCH_NUM",
"MSP_MATCH_FLG", "NLP_RECEIVED_FLG",
"NTMS_RECORD_NUM", "PUSH_NLP_FLG",
"SRC_LINE", "SYNCH_DATE",
"TASK_NO", "updateTime"
]
},
"dest": {
"index": "synch_dx_ntms_loan_success_task_index_new"
}
}
15、ignore_above限定keyword长度,大于的不会被索引
PUT my_index/_mappings
{
"properties" : {
"note" : {
"type" : "keyword",
"ignore_above": 2
}
}
}