沙师弟,师父的充电器掉了

ES2.2 相同搜索条件,不同命中结果数,响应文档也不同

Elasticsearch | 作者 huangzhiwei | 发布于2019年08月27日 | 阅读数:1689

seo索引相同的查询条件,查询结果命中数hits一会8589条,一会11796,响应结果震荡
 
请求URI:
GET /alia_changsha_seo/t_seo_ask_question/_search
{
  "from": 0,
  "size": 3, 
  "query": {
    "match": {
      "title": "月嫂价格"
    }
  }
}
 
响应结果1:
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 11796,
      "max_score": 5.6444073,
      "hits": [
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "602063",
            "_score": 5.6444073,
            "_routing": "602063",
            "_source": {
               "createtime": 1539790146000,
               "questionid": 602063,
               "isrecomment": 0,
               "modifytime": 1539790146000,
               "questionstate": 0,
               "title": "月嫂价格多少",
               "isanonymous": 0,
               "userid": 7121652807227513000,
               "content": "月嫂价格多少"
            }
         },
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "575049",
            "_score": 4.9388566,
            "_routing": "575049",
            "_source": {
               "createtime": 1533888237000,
               "questionid": 575049,
               "isrecomment": 0,
               "modifytime": 1533888237000,
               "questionstate": 0,
               "title": "月嫂价格是多少?",
               "isanonymous": 0,
               "userid": 7121652807225102000,
               "content": "月嫂价格是多少?"
            }
         },
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "167785",
            "_score": 2.4323895,
            "_routing": "167785",
            "_source": {
               "createtime": 1523588480000,
               "questionid": 167785,
               "isrecomment": 0,
               "modifytime": 1523588480000,
               "questionstate": 0,
               "title": "上海请育儿嫂一个月价格",
               "isanonymous": 0,
               "userid": 628471958165659600,
               "content": "上海请育儿嫂一个月价格"
            }
         }
      ]
   }
}
 
响应结果2:
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 8589,
      "max_score": 5.6382246,
      "hits": [
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "602063",
            "_score": 5.6382246,
            "_routing": "602063",
            "_source": {
               "createtime": 1539790146000,
               "questionid": 602063,
               "isrecomment": 0,
               "modifytime": 1539790146000,
               "questionstate": 0,
               "title": "月嫂价格多少",
               "isanonymous": 0,
               "userid": 7121652807227513000,
               "content": "月嫂价格多少"
            }
         },
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "575049",
            "_score": 4.9334464,
            "_routing": "575049",
            "_source": {
               "createtime": 1533888237000,
               "questionid": 575049,
               "isrecomment": 0,
               "modifytime": 1533888237000,
               "questionstate": 0,
               "title": "月嫂价格是多少?",
               "isanonymous": 0,
               "userid": 7121652807225102000,
               "content": "月嫂价格是多少?"
            }
         },
         {
            "_index": "tk_changsha_seo-default",
            "_type": "t_seo_ask_question",
            "_id": "161434",
            "_score": 4.92701,
            "_routing": "161434",
            "_source": {
               "createtime": 1522311203000,
               "questionid": 161434,
               "isrecomment": 0,
               "modifytime": 1522311203000,
               "questionstate": 0,
               "title": "月嫂价格是多少",
               "isanonymous": 0,
               "userid": 819880222691700700
            }
         }
      ]
   }
}
 
经过验证,第一种响应结果出现育儿嫂价格的文档,和查询语句相关性不高;
第二种响应结果,命中文档数更少,但是响应结果的文档和查询语句相关性更高
 
看论坛上,有过类似问题,解决方案是通过设置preference=_primary_first,设置后,响应结果一直都稳定在结果2,是我们想要的响应结果
 
目前不太理解,为什么搜索请求落在不同的节点上,会出现命中结果文档总数hits不同,响应文档的排序不同
已邀请:

bellengao - 博客: https://www.jianshu.com/u/e0088e3e2127

赞同来自: tacsklet

因为主分片和副本分片并不是完全一致的(比如有删除过doc的情况下,主分片进行过merge,副本分片没有进行过merge,此时主分片和副本分片就会不一致),同样的查询语句在主分片和副本分片上执行计算到的doc的得分可能会不一样,所以最终结果就会不一样

要回复问题请先登录注册