ElasticSearch查询执行偏好

本文介绍了Elasticsearch中搜索请求的偏好设置方法,包括如何指定主分片、副本分片进行搜索,如何限制搜索范围到特定节点或分片等。偏好设置有助于优化搜索效率并确保结果的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提:es版本2.4

Preference

Controls a preference of which shard replicas to execute the search request on. By default, the operation is randomized between the shard replicas.

The preference is a query string parameter which can be set to:

_primary
The operation will go and be executed only on the primary shards.

_primary_first

The operation will go and be executed on the primary shard, and if not available (failover), will execute on other shards.

_replica

The operation will go and be executed only on a replica shard.

_replica_first

The operation will go and be executed only on a replica shard, and if not available (failover), will execute on other shards.

_local

The operation will prefer to be executed on a local allocated shard if possible.

_only_node:xyz

Restricts the search to execute only on a node with the provided node id (xyz in this case).

_prefer_node:xyz

Prefers execution on the node with the provided node id (xyz in this case) if applicable.

_shards:2,3

Restricts the operation to the specified shards. (2 and 3 in this case). This preference can be combined with other preferences but it has to appear first: _shards:2,3;_primary

_only_nodes

Restricts the operation to nodes specified in node specification https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html

Custom (string) value

A custom value will be used to guarantee that the same shards will be used for the same custom value. This can help with “jumping values” when hitting different shards in different refresh states. A sample value can be something like the web session id, or the user name.

For instance, use the user’s session ID to ensure consistent ordering of results for the user:

curl localhost:9200/_search?preference=xyzabc123 -d '
{
    "query": {
        "match": {
            "title": "elasticsearch"
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值