ES-索引、文档插入、更新、删除、脚本API和Query参数

文档操作语法:
{GET|POST|DELETE|PUT} {索引名称|索引名称表达式} {_doc|_create|_search} {文档ID} {?param=value} {body参数}

索引

建立索引

put /mall_order_test
{
	"mappings":{
		"properties":{
			"orderId":{
				"type":"long"
			},
			"payAmount":{
				"type":"double"
			},
			"orderTime":{
				"type":"date",
				"format":"yyyy-MM-dd HH:mm:ss.SSS"
			},
			"goods":{
				"type":"nested",
				"properties":{
					"name":{
						"type":"text"
					},
					"price":{
						"type":"double"
					},
					"count":{
						"type":"integer"
					}
				}
			},
			"address":{
				"type":"nested",
				"properties":{
					"province":{
						"type":"keyword"
					},
					"city":{
						"type":"keyword"
					},
					"county":{
						"type":"keyword"
					},
					"street":{
						"type":"keyword"
					},
					"detail":{
						"type":"keyword"
					},
					"receiver":{
						"type":"text"
					},
					"receiverPhone":{
						"type":"text"
					}
				}
			}
		}
	}
}

修改索引

增加字段
put /mall_order_test/_mapping
{
  "properties":{
    "orderTitle":{
      "type":"text"
    },
    "goods":{
      "type":"nested",
      "properties":{
        "id":{
          "type":"integer"
        }
      }
    }
  }
}

向Elasticsearch发出了一个PUT请求,目标是mall_order_test索引的映射。然后我们指定了新字段的名称(goods.id)以及它的类型(integer)。

在更新映射之后,新的文档索引到mall_order_test将会包含orderTitlegoods.id字段,旧的文档需要被重新索引以包含这个新字段,如果你不需要回填旧数据,旧文档也可以不包含这个新字段。

旧文档对新字段设置数据,通过脚本的方式更新:

post /mall_order_test/_update_by_query
{
  "script":{
    "lang":"painless",
    "source":"ctx._source.orderTitle = params.orderTitle",
    "params":{
      "orderTitle":"默认值"
    }
  },
  "query":{
    "bool":{
      "must_not":{
        "exists":{
          "field":"orderTitle"
        }
      }
    }
  }
}

query条件查询没有orderTitle字段的文档,并且对文档执行script操作。

查询索引
get /mall_order_test/_mappings

返回索引的映射信息。

查询索引配置
GET /mall_order_test/_settings

包含:

  1. 搜索和索引的日志级别
  2. 刷新间隔时间
  3. 分片数量
  4. 索引创建时间
  5. 副本分片数量
  6. 索引UUID
索引配置

Elasticsearch 的 _settings API 允许你查看和更改索引的设置。这些设置分为两大类:静态设置和动态设置。

  1. 静态设置在索引创建时设置,并且不能在索引创建之后改变;
  2. 动态设置可以在运行时任何时候更改。

以下是一些常见的 Elasticsearch 索引设置及其默认值的例子,但请注意这不是一个完整的列表。设置可能会随着不同的 Elasticsearch 版本而变化:

  1. index.number_of_shards (静态)

    • 默认值: 1
    • 描述: 索引的主分片数。
  2. index.number_of_replicas (动态)

    • 默认值: 1
    • 描述: 每个主分片的副本分片数。
  3. index.refresh_interval (动态)

    • 默认值: “1s”
    • 描述: 为索引刷新(使新写入的文档对搜索可见)的频率。
  4. index.write.wait_for_active_shards (动态)

    • 默认值: “1”
    • 描述: 写操作应等待的活动分片数量以确保操作的耐久性。
  5. index.translog.flush_threshold_size (动态)

    • 默认值: “512mb”
    • 描述: translog 达到此大小时自动刷新。
  6. index.translog.durability (动态)

    • 默认值: “REQUEST”
    • 描述: 控制 translog 何时被刷写和同步到磁盘。
  7. index.analysis (静态)

    • 默认值: 无(取决于定义的 analyzer)
    • 描述: 定义索引中使用的分析器、分词器、过滤器和字符过滤器。
  8. index.routing.allocation.enable (动态)

    • 默认值: “all”
    • 描述: 控制分片分配到节点的策略。
  9. index.blocks.read_only (动态)

    • 默认值: false
    • 描述: 设置为 true 时,索引将为只读。
  10. index.blocks.read_only_allow_delete (动态)

    • 默认值: false
    • 描述: 当磁盘容量不足时自动设置为 true,使索引为只读并允许删除。
  11. index.mapping.total_fields.limit (动态)

    • 默认值: “1000”
    • 描述: 在一个索引中允许的字段上限。

这些是一些基本的设置例子,但还有许多其他的设置可以配置。由于具体的默认值可能随不同的 Elasticsearch 版本而变化,建议参考具体版本的官方文档,以了解最准确的配置项和默认值:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html

要查看所有可用的设置及其当前值,可以针对特定索引执行如下请求:

GET /my_index/_settings

这将为索引 my_index 返回当前设置。如果你想要查看所有索引的设置,可以省略索引名或使用 _all

GET /_settings

GET /_all/_settings
打开/关闭索引

索引被关闭,索引只能显示元数据信息,不能够进行读写操作。

# 关闭
POST /mall_order_test/_close

# 打开
POST /mall_order_test/_open
查询集群情况

1.查看集群健康状态:

GET _cat/health?v

# 输出
epoch      timestamp cluster                 status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1703838548 08:29:08  es-cn-09k1wi493002cgixs green           3         3   1284 576    0    0        0             0                  -                100.0%

2.查看索引状态:

GET _cat/indices?v

# 输出
health status index                                     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   mall_order_test                           T5mQm0dcRf6twAJaQZ0j0w   1   1          4            0     73.7kb         36.8kb

索引别名

推荐每个人为他们的Elasticsearch索引使用别名,因为在未来重建索引的时候,别名会赋予你更多的灵活性。假设一开始创建的索引只有一个主分片,之后你又决定为索引扩容。如果为原有的索引使用的是别名,现在你可以修改别名让其指向额外创建的新索引,而无须修改被搜索的索引之名称。

另一个有用的特性是,在不同的索引中创建窗口。比如,如果为数据创建了每日索引,你可能期望一个滑动窗口涵盖过去一周的数据,别名就称为last-7-days。然后,每天创建新的每日索引时,你可以将其加人别名,同时删除第8天前的旧索引。

创建/删除别名
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "mall_goods_test",
        "alias": "mall_goods_test_alias"
      }
    },
    {
      "remove": {
        "index": "mall_goods_test",
        "alias": "mall_goods_test_alias"
      }
    }
  ]
}

add 在索引mall_goods_test创建一个mall_goods_test_alias别名;
remove 在索引mall_goods_test创建一个mall_goods_test_alias别名。

PUT方式创建别名:

PUT /mall_goods_test/_alias/mall_goods_test_alias

在索引mall_goods_test创建一个mall_goods_test_alias别名。

DELETE方式删除别名:

DELETE /mall_goods_test/_alias/mall_goods_test_alias

删除索引mall_goods_test的mall_goods_test_alias别名。

查询索引别名
GET /mall_goods_test/_alias

查询索引mall_goods_test的别名

文档

插入

通用的插入
post /mall_order_test/_doc
{
  "orderId":199407131,
  "orderTime":"2023-12-28 20:00:54.713",
  "payAmount":16000,
  "address":{
    "province":"广东省",
    "city":"广州市",
    "county":"天河区",
    "street":"磨碟沙",
    "detail":"欢聚大厦19F",
    "recevier":"元旦",
    "receiverPhone":"13800138000"
  },
  "goods":[
  	{"name":"iphone15 pro max","price":15000,"count":1},  
  	{"name":"磁吸充电宝","price":1000,"count":1}
  ]
}

goods 为商品,是数组结构,address是嵌套对象。

查询

查询条件:
term ,不经过分词,直接查询精确的值
match,会使用分词器解析,类似模糊匹配
range,范围查询

match和match_phrase查询会执行分析步骤,term和terms不会执行分析步骤。

must、must_not、should 等需要配合bool使用。

查询结果处理:

  1. filter、nested_filter:数据过滤
  2. from、size:分页查询
  3. sort:根据文档_score值进行排序
  4. highlight:高亮
根据文档id查询_doc/id

指定文档id查询:

# sZNUsIwBSLqPVZ3tlB7z 是文档id(`_id`)
get /mall_order_test/_doc/sZNUsIwBSLqPVZ3tlB7z
URL 参数查询

在URL参数 q后面增加key:value的方式查询:

# long类型查询
get /mall_order_test/_search?q=orderId:199407131

# 模糊搜索
get /mall_order_test/_search?q=orderTitle:iPhone15
match

1.根据字段(orderId)搜索:

get /mall_order_test/_search
{
  "query":{
    "match": {
      "orderId": 199407131
    }
  }
}
must

must 可以和其他 bool 子句比如 should, must_notnested_filter(嵌套对象) 和 filter 组合使用。

根据多个条件查询,需要所有条件成立,类似SQL的 and操作:

POST /mall_order_test/_search
{
  "query": {
    "bool": {
      "must": [
        {"match":{"orderTitle":"iPhone15"}},
        {"range":{
          "payAmount":{
            "gte":10000
          }
        }}
      ]
    }
  }
}

查询orderTitle类似“iPhone15”的数据 并且 payAmount大于等于 10000的数据。

should

根据多个条件查询,其中一个条件成立,类似SQL的 or操作:

POST /mall_order_test/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "orderTitle": "iPhone15"
        }},
        {"range": {
          "payAmount": {
            "gte": 20000
          }
        }}
      ]
    }
  }
}

查询orderTitle类似“iPhone15”的数据 或者 payAmount大于等于 20000的数据。

must_not

类似SQL的 not操作:

POST /mall_order_test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "orderTitle": "iPhone12"
        }}
      ]
    }
  }
}

查询orderTitle不包含“iPhone12”的数据。

时间搜索
get /mall_order_test/_search
{
  "query":{
    "range": {
      "orderTime": {
        "gte": "2023-12-28 20:00:54.000",
        "lte": "2023-12-28 20:00:55.000"
      }
    }
  }
}
分页
post /mall_order_test/_search
{
  "query":{
    "match": {
      "orderTitle": "iPhone15"
    }
  },
  "from":0,
  "size":10
}

from:第几条数据开始
size:返回数据数量

高亮
POST /mall_order_test/_search
{
  "query": {
    "match":{"orderTitle":"iPhone15"}
  },
  "highlight": {
    "fields": {
      "orderTitle": {}
    }
  }
}

返回highlightorderTitle字段中把“iPhone15”搜索内容进行高亮标注。

自定义样式

POST /mall_order_test/_search
{
  "query": {
    "match":{"orderTitle":"iPhone15"}
  },
  "highlight": {
    "pre_tags": "<b class='key' style='color:red'>",
    "post_tags": "</b>",
    "fields": {
      "orderTitle": {}
    }
  }
}

pre_tags:类似HTML标签前缀
post_tags:类似HTML标签后缀

结果:

<b class='key' style='color:red'>iPhone15</b> Pro Max订单金额16000元
排序

ID、数字类型、日期类型才支持排序。

根据payAmountorderTime排序

post /mall_order_test/_search
{
  "query":{
    "match": {
      "orderTitle": "iPhone15"
    }
  },
  "sort":[
    {
      "payAmount":{
        "order":"desc"
      }
    },
    {
      "orderTime":{
        "order":"desc"
      }
    }
  ]
}

_scoript 脚本排序:
todo

Bucket聚合(Group By)

ElasticSearch中桶在概念上类似于 SQL 的分组(GROUP BY),而指标则类似于 COUNT() 、 SUM() 、 MAX() 等统计方法。

进而引入了两个概念:

  1. 桶(Buckets) 满足特定条件的文档的集合
  2. 指标(Metrics) 对桶内的文档进行统计计算

所以ElasticSearch包含3种聚合(Aggregation)方式:

  1. 桶聚合(Bucket Aggregration)
  2. 指标聚合(Metric Aggregration)
  3. 管道聚合(Pipline Aggregration)

Bucket 函数列表。

range

todo
1.aggs.range 按区间统计 比如0-4,4-6,6-10
2.date_range

histogram 直方图
和range类似,不用定义一个个范围,定义一个区间间隔。有histogram和date_histogram

significant_terms

todo

在这里插入图片描述

查询结果

{
  "took" : 1,
  "timed_out" : false,
  //分片信息
  "_shards" : {
    "total" : 1,//分片总数
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
  	//查询命中文档的数量
    "total" : {
      "value" : 1,//查询命中总数
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mall_order_test",//索引
        "_type" : "_doc",//类型
        "_id" : "sZNUsIwBSLqPVZ3tlB7z",//id
        "_score" : 1.0,
        //文档数据
        "_source" : {
          "orderId" : 199407131,
          "orderTime" : "2023-12-28 20:00:54.713",
          "payAmount" : 16000,
          "address" : {
            "province" : "广东省",
            "city" : "广州市",
            "county" : "天河区",
            "street" : "磨碟沙",
            "detail" : "欢聚大厦19F",
            "recevier" : "元旦",
            "receiverPhone" : "13800138000"
          },
          "goods" : [
            {
              "name" : "iphone15 pro max",
              "price" : 15000,
              "count" : 1
            },
            {
              "name" : "磁吸充电宝",
              "price" : 1000,
              "count" : 1
            }
          ]
        }
      }
    ]
  }
}

更新

通过文档ID更新

包括嵌套对象的更新,都是使用key-value的方式

# 通过id更新
post /mall_order_test/_update/sZNUsIwBSLqPVZ3tlB7z
{
  "doc":{
    "payAmount":16000,
    "orderTitle":"iPhone15 Pro Max",
    "address":{
      "street":"磨碟沙街道"
    }
  }
}

更新普通payAmount(double)、orderTitle(text)、address(nested)各个类型的数据。

文档不存在则插入-upsert

  如果文档不存在,则 upsert 元素的内容将作为新文档插入。 如果文档确实存在,则将改为执行脚本。
  

POST test/_update/1
{
    "script" : {
        "source": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}

  另外还有scripted_upsertdoc_as_upsert支持的URL参数

通过脚本更新

更新orderTitle,取值为现有orderTitle+订单金额+payAmount+元。

POST /mall_order_test/_update_by_query
{
  "script":{
    "lang":"painless",
    "source":"ctx._source.orderTitle = ctx._source.orderTitle+'订单金额'+ctx._source.payAmount+'元'",
    "params":{
    }
  },
  "query":{
    "bool":{
      "must":{
        "exists":{
          "field":"orderTitle"
        }
      }
    }
  }
}

ctx为es内部变量,通过ctx映射获得以下变量:_index、_type、_id、_version、_routing、_source 和 _now(当前时间戳)。

更新嵌套数组

指定文档ID,通过脚本更新:

post /mall_order_test/_update/sZNUsIwBSLqPVZ3tlB7z
{
  "script":{
    "lang":"painless",
    "source":"for(e in ctx._source.goods){if(e.name==params.oldName){e.name=params.name;e.id=params.id}}",
    "params":{
      "oldName":"磁吸充电宝",
      "name":"磁铁充电宝2万毫安",
      "id":1
    }
  }
}

删除

根据文档ID删除
# spNWsIwBSLqPVZ3t0R7y 为文档ID
DELETE /mall_order_test/_doc/spNWsIwBSLqPVZ3t0R7y
通过查询条件-_delete_by_query

  对匹配查询的每个文档执行删除。

POST twitter/_delete_by_query
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}

  返回结构体

  _delete_y_query在启动和删除使用内部版本控制时获取索引的快照。 这意味着如果文档在生成快照的时间和处理删除请求时,将会出现版本冲突。

  _delete_by_query 执行过程中,会依次执行多个搜索请求,以便找到所有要删除的匹配文档。 每找到一批文档,就会执行相应的批量请求,删除所有这些文档。 如果搜索或批量请求被拒绝,_delete_by_query 将依赖默认策略重试被拒绝的请求(最多 10 次)。 达到最大重试限制会导致 _delete_by_query 中止,并且所有失败都在响应失败中返回。 已执行的删除仍然存在。 换句话说,该过程没有回滚,只是中止。 当第一次失败导致中止时,失败的批量请求返回的所有失败都在 failures 元素中返回; 因此,可能会有相当多的失败实体。

  如果你想计算有多少个版本冲突,而不是中止,可以在URL中设置为conflicts=proceed或者在请求体中设置"conflicts": “proceed”。

  默认情况下 _delete_by_query 使用 scroll_size=1000 。您可以使用 scroll_size URL 参数更改。

POST twitter/_delete_by_query?scroll_size=5000
{
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

  _delete_by_query还支持refresh, wait_for_completion, wait_for_active_shards, timeout, 和scroll 的URL参数。

更新数据类型

文档路由(routing)

为什么使用路由?
加入索引有100个分片,查询时需要100个分片都执行查询操作。如果我们知道查询的数据都在其中2个分片上,只在2个分片上查询比100个分片查询消耗性能要少、速度更快,因为查询的数据量变少了,在大量数据集上很实用。

路由策略需要在两个方面下功夫:

  1. 索引文档的时候挑选合适的路由值;
  2. 执行查询时重用这些值(使用routing查询)。

注意
查询时指定routing时,查询只会在路由的分片上执行,如果文档符合查询,但不在routing指定值上,也不会返回。

设置routing必填
PUT /mall_user_test
{
  "settings": {
    "index.number_of_shards":10
  }, 
  "mappings": {
      "_routing":{
        "type": "long",
        "required":true
      },
    "properties": {
      "id":{
        "type": "long"
      },
      "name":{
        "type": "text"
      },
      "address":{
        "type": "text"
      }
    }
  }
}

_routing的required设置为true,表示为必填。

routing写入数据
POST /mall_user_test/_doc/1?routing=1
{
  "id":1,
  "name":"张飞",
  "address":"蜀国"
}

写入数据需要增加routing=1参数,即使用1(也是id)作为路由值。

Bulk方式写入:

POST _bulk
{"index":{"_index":"mall_user_test","_id":2,"routing":2}}
{"id":2,"name":"关羽","address":"蜀国"}

需要指定_id 和 routing参数。

routing查询数据
POST mall_user_test/_search?routing=1
{
  "query": {
    "match": {
      "id": 1
    }
  }
}

parameter 参数增加了routing=1,表明只有对应的分片上执行查询。

# 未指定routing参数查询分片使用情况,在10个分片上执行查询
"_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  }

# 指定routing=1参数查询分片使用情况,在1个分片上执行查询
"_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }

Bulk操作

Elasticsearch为减少网络间IO的操作,提供了Bulk(类似批处理)操作。

Bulk都支持那些操作呢?
文档查询、插入、更新和删除操作都支持。

Bulk每个请求由两个JSON文档组成,使用换行符隔开:

  1. 对那个索引、元数据进行什么操作
  2. 文档内容,可以为空。例如delete操作时指定_id,文档内容可以为空。

例如:

POST _bulk
{"index":{"_index":"mall_user_test","_id":2,"routing":2}}
{"id":2,"name":"关羽","address":"蜀国"}

第一个JSON文档:{"index":{"_index":"mall_user_test","_id":2,"routing":2}}指明对mall_user_test索引_id=2、routing=2的文档执行重新索引操作。
第二个JSON文档:{"id":2,"name":"关羽","address":"蜀国"}是文档内容,一般是索引field的key-value。

另外种格式是在url上指定索引名称:

POST /mall_user_test/_bulk

对索引mall_user_test执行bulk操作。

第一个JSON文档

JSON文档包含两部分:

  1. 操作
  2. 元数据

操作:

  1. index:重新索引文档,覆盖原有的文档
  2. create:创建文档
  3. update:更新文档数据
  4. delete:删除文档数据

元数据:

  1. _index:指明索引名称
  2. _id:指明操作的文档id
  3. routing:指明路由值
第二个JSON文档
索引文档- create
POST _bulk
{"index":{"_index":"mall_user_test","_id":2,"routing":2}}
{"id":2,"name":"关羽","address":"蜀国"}
插入文档
POST _bulk
{"create":{"_index":"mall_user_test","_id":3,"routing":3}}
{"id":3,"name":"刘备","address":"蜀国"}
更新文档- update
POST _bulk
{"update":{"_index":"mall_user_test","_id":2,"routing":2}}
{"doc":{"id":2,"name":"关羽","address":"蜀国"}}
删除文档 - delete
POST _bulk
{"delete":{"_index":"mall_user_test","_id":3,"routing":3}}
并发控制

index 和 delete 操作时,可以增加if_seq_noif_primary_term并发控制参数:

  1. if_seq_no:序列号,每个操作都会增加序列编号
  2. if_primary_term:

seq_no和primary_term 参数的获取:

GET /mall_user_test/_doc/2?routing=2

# 返回结果包含_seq_no、_primary_term
{
  "_index" : "mall_user_test",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "_routing" : "2",
  "found" : true,
  "_source" : {
    "id" : 2,
    "name" : "关羽",
    "address" : "蜀国"
  }
}
POST _bulk
{"update":{"_index":"mall_user_test","_id":2,"routing":2,"if_seq_no":0,"if_primary_term":1}}
{"doc":{"id":2,"name":"关羽","address":"蜀国"}}

Query 参数

是否返回 _source字段

  默认情况下,get 操作会返回 _source 字段的内容,除非您使用了 stored_fields 参数或 _source 字段被禁用。 您可以使用 _source 参数关闭 _source 检索。

_source=false

  不返回_source 字段。

_source=user,message

  _source字段返回 user和message字段。

返回或者排除字段_source_includes & _source_excludes

  如果您只需要完整 _source 中的一两个字段,则可以使用 _source_includes 和 _source_excludes 参数来包含或过滤掉您需要的部分。 这对于部分检索可以节省网络开销的大型文档特别有用。 这两个参数都采用逗号分隔的字段列表或通配符表达式。

_source_includes=user,message&_source_excludes=message

  只返回user字段,message字段不会返回

{
  "_index" : "twitter",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "kimchy"
  }
}
store属性字段返回-stored_fields

get 操作允许指定一组存储字段,这些字段将通过传递 stored_fields 参数返回。 如果请求的字段没有被存储(mapping 时 store=false),它们将被忽略。

前提:

PUT twitter
{
   "mappings": {
       "properties": {
          "counter": {
             "type": "integer",
             "store": false
          },
          "tags": {
             "type": "keyword",
             "store": true
          }
       }
   }
}

PUT twitter/_doc/1
{
    "counter" : 1,
    "tags" : ["red"]
}

tags字段使用store=true,couter字段使用store=false。
store=true 的字段才会在使用stored_fields参数时才能返回。

GET twitter/_doc/1?stored_fields=tags,counter

  返回结果:

{
   "_index": "twitter",
   "_type": "_doc",
   "_id": "1",
   "_version": 1,
   "_seq_no" : 22,
   "_primary_term" : 1,
   "found": true,
   "fields": {
      "tags": [
         "red"
      ]
   }
}
分片查询性能 - preference

控制执行 get 请求的分片副本的首选项。 默认情况下,操作在分片副本之间是随机的。

preference=_local
  • _local:如果可能,该操作将更倾向于在本地分配的分片上执行。
  • Custom (string) value:自定义值将用于保证相同的分片将用于相同的自定义值。 当在不同的刷新状态下击中不同的分片时,这有助于“跳跃值”。 示例值可以是 Web 会话 ID 或用户名之类的东西。
操作对搜索可见控制 - refresh

控制此请求所做的更改何时对搜索可见。

Index, Update, Delete, and Bulk APIs 支持refresh参数。

refresh=true|false(默认)|wait_for
  • true:操作发生后立即刷新相关的主分片和副本分片(不是整个索引),以便更新的文档立即出现在搜索结果中。 这只能在仔细考虑并验证它不会导致性能不佳(从索引和搜索的角度来看)之后进行。
  • false:不采取与刷新相关的操作。 此请求所做的更改将在请求返回后的某个时间点可见。
  • wait_for:等待请求所做的更改通过刷新可见,然后再回复。 这不会强制立即刷新,而是等待刷新发生。 Elasticsearch 会自动刷新每个 index.refresh_interval 更改的分片,默认为1秒。 该设置是动态的。 调用 Refresh API 或在任何支持它的 API 上将 refresh 设置为 true 也会导致刷新,进而导致已经运行的带有 refresh=wait_for 的请求返回。

   除非您有充分的理由等待更改变得可见,否则请始终使用 refresh=false

realtime

  默认情况下,get API 是实时的,不受索引刷新率的影响(当数据对搜索可见时)。 如果文档已更新但尚未刷新,get API 将就地发出刷新调用以使文档可见。 这也将使自上次刷新后更改的其他文档可见。 为了禁用实时 GET,可以将实时参数设置为 false。

realtime=true
timeout

操作超时时间,5m:5分钟。

timeout=5m

默认情况下,索引操作将在主分片上等待最多 1 分钟,然后才会失败并以错误响应。 timeout 参数可用于明确指定等待的时间。 这是将其设置为 5 分钟(5m)

version & version_type

使用示例:

version=2|version_type=external
conflicts

_delete_by_query操作时,如果你想计算有多少个版本冲突,而不是中止,可以在URL中设置为conflicts=proceed。

conflicts=proceed
version

每个索引文档都有一个版本号。 默认情况下,使用从 1 开始的内部版本控制,每次更新都会增加,包括删除。 可选地,版本号可以设置为外部值(例如,如果在数据库中维护)。 要启用此功能,应将 version_type 设置为 external。 提供的值必须是大于或等于 0 且小于 9.2e+18 左右的数字长整型值。

  使用外部版本类型时,系统会检查传递给索引请求的版本号是否大于当前存储文档的版本。 如果为真,文档将被索引并使用新的版本号。 如果提供的值小于或等于存储文档的版本号,则会发生版本冲突,索引操作将失败。

version_type 取值:

  1. internal:仅当给定版本与存储文档的版本相同时才索引文档。
  2. external or external_gt:仅当给定版本严格高于存储文档的版本或不存在现有文档时才索引文档。给定版本将用作新版本,并将与新文档一起存储。提供的版本必须是非负长整数。
  3. external_gte:仅当给定版本等于或高于存储文档的版本时才索引文档。如果没有现有文档,则操作也会成功。给定版本将用作新版本,并将与新文档一起存储。提供的版本必须是非负长整数。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冲上云霄的Jayden

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值