elasticsearch學習筆記(三十三)——Elasticsearch Bouncing Results問題

想象一下有兩個文檔有一樣值的時間戳字段,搜索結果用 timestamp 字段來排序。 因爲搜索請求是在全部有效的分片副本間輪詢的,那就有可能發生主分片處理請求時,這兩個文檔是一種順序, 而副本分片處理請求時又是另外一種順序。html

這就是所謂的 bouncing results 問題: 每次用戶刷新頁面,搜索結果表現是不一樣的順序。 讓同一個用戶始終使用同一個分片,這樣能夠避免這種問題, 能夠設置 preference 參數爲一個特定的任意值好比用戶會話ID來解決。

對於preference

詳細請查看
https://www.elastic.co/guide/...node

偏好這個參數 preference 容許 用來控制由哪些分片或節點來處理搜索請求。 它接受像 _primary, _primary_first, _local, _only_node:xyz, _prefer_node:xyz, 和 _shards:2,3 這樣的值, 這些值在 search preference 文檔頁面被詳細解釋。
可是最有用的值是某些隨機字符串,它能夠避免 bouncing results 問題。
例如,使用用戶的會話ID xyzabc123,以下所示:elasticsearch

GET /test_index/_search?preference=xyzabc123
{
  "query": {
    "match": {
      "test_field": "hello"
    }
  }
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.20521778,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.20521778,
        "_source" : {
          "test_field" : "hello, how are you"
        }
      },
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.16402164,
        "_source" : {
          "test_field" : "hello you, and world is very good"
        }
      }
    ]
  }
}
相關文章
相關標籤/搜索