---
title: "elasticsearch的5种分片查询优先级"
author: "rockelixir"
date: "2013-06-20T22:16:26Z"
tags: []
categories: ["开源软件"]
slug: "elasticsearch-5"
draft: false
lastmod: "2013-06-20T22:19:11Z"
id: 1890888
url: "https://rockelixir.iteye.com/blog/1890888"
---

elasticsearch可以使用preference参数来指定分片查询的优先级，使用时就是在请求url上加上preference参数，如：http://ip:host/index/_search?preference=_primary
java的调用接口翻译为：client.prepareSearch(“index”).setPreference(“_primary”)。
默认情况下es有5种查询优先级：
_primary: 指查询只在主分片中查询
_primary_first: 指查询会先在主分片中查询，如果主分片找不到（挂了），就会在副本中查询。
_local: 指查询操作会优先在本地节点有的分片中查询，没有的话再在其它节点查询。
_only_node:指在指定id的节点里面进行查询，如果该节点只有要查询索引的部分分片，就只在这部分分片中查找，所以查询结果可能不完整。如_only_node:123在节点id为123的节点中查询。
Custom (string) value:用户自定义值，指在参数cluster.routing.allocation.awareness.attributes指定的值，如这个值设置为了zone，那么preference=zone的话就在awareness.attributes=zone*这样的节点搜索，如zone1、zone2。关于这个值作用可以参考下面文章。
虽然es有提供这5种优先级，但感觉还是不能满足我的需求，我是想能指定在某一个或多个节点中查询，比如node1和node2里面的分片能组成一个完整的索引，那我可以只在node1和node2中搜索就行了。看来只能改源码解决，改源码也非常简单。
首先找到org.elasticsearch.cluster.routing.operation.plain.PlainOperationRouting这个类，es搜索时获取分片信息是通过这个类的。它的preferenceActiveShardIterator()方法就是根据条件来找出响应的分片。看源码可知其主要是根据preference这个参数来决定取出的分片的。如果没有指定该参数，就随机抽取分片进行搜索。如果参数以_shards开头，则表示只查询指定的分片。注意，这个功能官网的文档中没有写到。
然后下面就是判断我上面说的5种优先级情况。我们现在要加个多节点分片查询的功能，仿照单个节点分片查询（指_only_node）就行了，在

```java
if (preference.startsWith("_only_node:")) {
    return indexShard.onlyNodeActiveShardsIt(preference.substring("_only_node:".length()));
}
```

后面加上

```java
if (preference.startsWith("_only_nodes:"))  {
    return indexShard.onlyNodesActiveShardsIt(preference.substring("_only_nodes:".length()));
}
```

onlyNodesActiveShardsIt这个方法在org.elasticsearch.cluster.routing.IndexShardRoutingTable中是没有的，要自己写。加上
   
```java
 
/**
 * Prefers execution on the provided nodes if applicable.
 */
 public ShardIterator onlyNodesActiveShardsIt(String nodeIds) {
     String[] ids = nodeIds.split(",");
     ArrayList<ShardRouting> ordered = new ArrayList<ShardRouting>(shards.size());
     // fill it in a randomized fashion
     for (int i = 0; i < shards.size(); i++) {
         ShardRouting shardRouting = shards.get(i);
         for(String nodeId:ids){
           if (nodeId.equals(shardRouting.currentNodeId())) {
             ordered.add(shardRouting);
           }
         }
     }
     return new PlainShardIterator(shardId, ordered);
 }
```

重新编译源码就行了。查询时加上preference=_only_nodes:node1id,node2id 就可以指定在node1和node2中搜索