Elasticsearch相同两次搜索出现不一样的结果

1. 原因

主要的原因是因为有副本(replica)的存在,主分片和副本分片可能不一致,导致最终在主分片和副本分片上计算得到的得分不同,而导致最终的查询结果不一致。
但是是如何造成主分片和副本分片不一致的情况,可能是因为用户删除了部分文档,之后主分片进行了merge, 而副本分片没有进行merge。 这种情况下主分片和副本分片上的总文档数量就会不同,打分时计算出的IDF的值不同,最终得到了不同的得分。

2. 解决办法

解决方式就是在查询时指定preference参数:保证同样的查询语句会请求到相同的分片。

2.1 老版本参数

可以指定为_primary、_replica(此为老版本参数)或者其它自定义的值,保证同样的查询语句会请求到相同的分片。

2.2 新版本参数

ES7.4.x以后版本参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

  1. _only_local

    Run the search only on shards on the local node.

  2. _local

    If possible, run the search on shards on the local node. If not, select shards using the default method.

  3. _only_nodes:<node-id>,<node-id>

    Run the search on only the specified nodes IDs. If suitable shards exist on more than one selected nodes, use shards on those nodes using the default method. If none of the specified nodes are available, select shards from any available node using the default method.

  4. _prefer_nodes:<node-id>,<node-id>

    If possible, run the search on the specified nodes IDs. If not, select shards using the default method.

  5. _shards:<shard>,<shard>

    Run the search only on the specified shards. This value can be combined with other preference values, but this value must come first. For example: _shards:2,3|_local

  6. <custom-string>

    Any string that does not start with _. If the cluster state and selected shards do not change, searches using the same value are routed to the same shards in the same order.

示例:

# 返回将针对其执行搜索请求的索引和分片(Returns the indices and shards that a search request would be executed against)。
# 启用kerberos认证
curl --negotiate -u : -XGET "http://ip:9200/index_name/_search_shards?pretty"
# 不启用kerberos认证
curl -XGET "http://ip:9200/index_name/_search_shards?pretty"

# 使用 _prefer_nodes(启用kerberos认证)
curl --negotiate -u : -XGET "http://ip:9200/index_name/_search?pretty=true&preference=_prefer_nodes:xxxx" -H 'Content-Type:application/json' -d'{"explain":treu,"query":{"match_all":{}}}'

# 使用 _prefer_nodes(不启用kerberos认证)
curl -XGET "http://ip:9200/index_name/_search?pretty=true&preference=_prefer_nodes:xxxx" -H 'Content-Type:application/json' -d'{"explain":treu,"query":{"match_all":{}}}'
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Elasticsearch 是一个开源的分布式搜索和分析引擎,用于实时搜索和分析大规模数据。在 Elasticsearch 中,word 搜索结果的页面展示可以通过以下几个方面来实现。 1. 搜索结果列表:在搜索结果页面上,通常会展示出与搜索词匹配的文档的列表。每个文档通常会显示标题、摘要、相关信息等,以便用户快速浏览和识别是否与其需求匹配。可以通过 Elasticsearch查询语句来过滤和排序搜索结果,以提供更有针对性的展示。 2. 分页与翻页:若搜索结果数量较大,需要将搜索结果分页展示。通常会显示每页的搜索结果数量和总页数,用户可以通过翻页操作浏览更多的搜索结果。可以使用 Elasticsearch 的分页查询语句,如 `from` 和 `size` 参数来实现结果的分页展示。 3. 高亮关键词:为了让用户更清晰地了解搜索结果与搜索词的匹配程度,可以将搜索词在搜索结果中进行高亮显示。Elasticsearch 提供了 `highlight` 查询,可以针对匹配的关键词添加 HTML 标签或其他样式,用以突出显示。 4. 搜索过滤器:为了让用户更方便地缩小搜索结果的范围,可以提供一些搜索过滤器,如按照时间、类型等进行筛选。这可以通过 Elasticsearch 的过滤器功能来实现,如 `range` 和 `term` 过滤器。 5. 搜索建议:为了提高用户体验,可以在搜索结果页面上提供搜索建议功能,根据用户当前的搜索词,展示相关的搜索建议。这可以通过 Elasticsearch 的自动补全功能来实现,如 `suggest` 查询。 总之,Elasticsearch 可以通过查询语句、分页查询、高亮显示、过滤器和搜索建议等功能来实现 word 搜索结果页面的展示,提供给用户更准确、便捷的搜索体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值