elasticsearch相同查询语句返回doc_count解决在一定范围变化

本文介绍了如何通过设置Elasticsearch的preference参数来优化查询性能。主要包括五种查询优先级的详细解释,如_primary和_primary_first等,并给出了具体的Java调用示例。

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

解决方法: 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的节点里面进行查询,如果该节点只有要查询索引的部分分片,就只在这部分 分片中查找,所以查询结果可能不完整。

转载于:https://my.oschina.net/u/2367628/blog/777457

"profile" : { "shards" : [ { "cluster" : "(local)", "searches" : [ { "query" : [ { "children" : [ { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 1293, "match" : 0, "next_doc_count" : 2, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 6, "create_weight" : 705, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 446734 }, "description" : "_id:([fe 1f])", "type" : "MultiTermQueryConstantScoreBlendedWrapper", "time_in_nanos" : 448732 }, { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 89566, "advance_count" : 2, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 6, "create_weight" : 631, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 41989 }, "description" : "FieldExistsQuery [field=_primary_term]", "type" : "FieldExistsQuery", "time_in_nanos" : 132186 } ], "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 94223, "match" : 0, "next_doc_count" : 2, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 4, "create_weight" : 78261, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 586996 }, "description" : "+_id:([fe 1f]) #FieldExistsQuery [field=_primary_term]", "type" : "BooleanQuery", "time_in_nanos" : 759480 } ], "rewrite_time" : 60099, "collector" : [ { "reason" : "search_query_phase", "children" : [ { "reason" : "search_top_hits", "name" : "SimpleTopScoreDocCollector", "time_in_nanos" : 7047 } ], "name" : "QueryPhaseCollector", "time_in_nanos" : 24342 } ] } ], "shard_id" : 0, "index" : "cn_zhihui_flowbase_replay_result_2021-07", "id" : "[488T3UaYQfOtLPgUGaXhXw][cn_zhihui_flowbase_replay_result_2021-07][0]", "aggregations" : [ ], "node_id" : "488T3UaYQfOtLPgUGaXhXw" }, { "cluster" : "(local)", "searches" : [ { "query" : [ { "children" : [ { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 1199, "match" : 0, "next_doc_count" : 3, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 9, "create_weight" : 729, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 59218 }, "description" : "_id:([fe 1f])", "type" : "MultiTermQueryConstantScoreBlendedWrapper", "time_in_nanos" : 61146 }, { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 5230, "advance_count" : 3, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 9, "create_weight" : 486, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 28277 }, "description" : "FieldExistsQuery [field=_primary_term]", "type" : "FieldExistsQuery", "time_in_nanos" : 33993 } ], "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 9962, "match" : 0, "next_doc_count" : 3, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 6, "create_weight" : 93906, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 135863 }, "description" : "+_id:([fe 1f]) #FieldExistsQuery [field=_primary_term]", "type" : "BooleanQuery", "time_in_nanos" : 239731 } ], "rewrite_time" : 42980, "collector" : [ { "reason" : "search_query_phase", "children" : [ { "reason" : "search_top_hits", "name" : "SimpleTopScoreDocCollector", "time_in_nanos" : 7015 } ], "name" : "QueryPhaseCollector", "time_in_nanos" : 30710 } ] } ], "shard_id" : 0, "index" : "cn_zhihui_flowbase_replay_result_2021-12", "id" : "[488T3UaYQfOtLPgUGaXhXw][cn_zhihui_flowbase_replay_result_2021-12][0]", "aggregations" : [ ], "node_id" : "488T3UaYQfOtLPgUGaXhXw" }, { "cluster" : "(local)", "searches" : [ { "query" : [ { "children" : [ { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 5535, "match" : 0, "next_doc_count" : 27, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 81, "create_weight" : 713, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 316615 }, "description" : "_id:([fe 1f])", "type" : "MultiTermQueryConstantScoreBlendedWrapper", "time_in_nanos" : 322863 }, { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 20475, "advance_count" : 27, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 81, "create_weight" : 355, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 116637 }, "description" : "FieldExistsQuery [field=_primary_term]", "type" : "FieldExistsQuery", "time_in_nanos" : 137467 } ], "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 40894, "match" : 0, "next_doc_count" : 27, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 54, "create_weight" : 29703, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 679931 }, "description" : "+_id:([fe 1f]) #FieldExistsQuery [field=_primary_term]", "type" : "BooleanQuery", "time_in_nanos" : 750528 } ], "rewrite_time" : 68918, "collector" : [ { "reason" : "search_query_phase", "children" : [ { "reason" : "search_top_hits", "name" : "SimpleTopScoreDocCollector", "time_in_nanos" : 18537 } ], "name" : "QueryPhaseCollector", "time_in_nanos" : 60503 } ] } ], "shard_id" : 0, "index" : "cn_zhihui_flowbase_replay_result_2025-05", "id" : "[488T3UaYQfOtLPgUGaXhXw][cn_zhihui_flowbase_replay_result_2025-05][0]", "aggregations" : [ ], "node_id" : "488T3UaYQfOtLPgUGaXhXw" }, { "cluster" : "(local)", "searches" : [ { "query" : [ { "children" : [ { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 484, "match" : 0, "next_doc_count" : 1, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 3, "create_weight" : 312, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 25596 }, "description" : "_id:([fe 1f])", "type" : "MultiTermQueryConstantScoreBlendedWrapper", "time_in_nanos" : 26392 }, { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 1599, "advance_count" : 1, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 3, "create_weight" : 179, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 10225 }, "description" : "FieldExistsQuery [field=_primary_term]", "type" : "FieldExistsQuery", "time_in_nanos" : 12003 } ], "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 3653, "match" : 0, "next_doc_count" : 1, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 2, "create_weight" : 17836, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 61653 }, "description" : "+_id:([fe 1f]) #FieldExistsQuery [field=_primary_term]", "type" : "BooleanQuery", "time_in_nanos" : 83142 } ], "rewrite_time" : 23984, "collector" : [ { "reason" : "search_query_phase", "children" : [ { "reason" : "search_top_hits", "name" : "SimpleTopScoreDocCollector", "time_in_nanos" : 3586 } ], "name" : "QueryPhaseCollector", "time_in_nanos" : 13213 } ] } ], "shard_id" : 0, "index" : "cn_zhihui_flowbase_replay_result_2021-09", "id" : "[7Jldh1R6TJmHEEPgG8gJJA][cn_zhihui_flowbase_replay_result_2021-09][0]", "aggregations" : [ ], "node_id" : "7Jldh1R6TJmHEEPgG8gJJA" }, { "cluster" : "(local)", "searches" : [ { "query" : [ { "children" : [ { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 4740, "match" : 0, "next_doc_count" : 26, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 0, "advance_count" : 0, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 78, "create_weight" : 284, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 301518 }, "description" : "_id:([fe 1f])", "type" : "MultiTermQueryConstantScoreBlendedWrapper", "time_in_nanos" : 306542 }, { "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 21502, "advance_count" : 26, "count_weight_count" : 0, "score" : 0, "build_scorer_count" : 78, "create_weight" : 178, "shallow_advance" : 0, "count_weight" : 0, "create_weight_count" : 1, "build_scorer" : 101649 }, "description" : "FieldExistsQuery [field=_primary_term]", "type" : "FieldExistsQuery", "time_in_nanos" : 123329 } ]分析"profile": true性能分析报告,返回如何提升dsl的查询效率GET /cn_zhihui_flowbase_replay_result/_search { "profile": true, "query": { "bool": { "must": [ { "terms": { "_id": ["1","2"] } } ] } }, "from": 0, "size": 1000 }
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值