elasticsearch学习笔记三十三Elasticsearch-Bouncing-Results问题

10次阅读

共计 970 个字符,预计需要花费 3 分钟才能阅读完成。

想象一下有两个文档有同样值的时间戳字段,搜索结果用 timestamp 字段来排序。由于搜索请求是在所有有效的分片副本间轮询的,那就有可能发生主分片处理请求时,这两个文档是一种顺序,而副本分片处理请求时又是另一种顺序。

这就是所谓的 bouncing results 问题: 每次用户刷新页面,搜索结果表现是不同的顺序。让同一个用户始终使用同一个分片,这样可以避免这种问题,可以设置 preference 参数为一个特定的任意值比如用户会话 ID 来解决。

对于 preference

详细请查看
https://www.elastic.co/guide/…

偏好这个参数 preference 允许 用来控制由哪些分片或节点来处理搜索请求。它接受像 _primary, _primary_first, _local, _only_node:xyz, _prefer_node:xyz, 和 _shards:2,3 这样的值, 这些值在 search preference 文档页面被详细解释。
但是最有用的值是某些随机字符串,它可以避免 bouncing results 问题。
例如,使用用户的会话 ID xyzabc123,如下所示:

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"}
      }
    ]
  }
}

正文完
 0