文件的重新分类和排序

时间:2015-06-04 02:35:19

标签: sorting elasticsearch

我的目标是编写一个查询,根据文档中字段的值重新排列文档。为了实现这一点,我使用了rescore查询,然后对结果进行排序。但是,对查询的解释显示,文档的排序是基于先前计算的分数而不是新分数完成的。

我看到以下内容解释了我无法使用rescore和排序。

"有时我们想要显示结果,其中页面上第一个文档的排序受附加规则的影响。不幸的是,rescore功能无法实现这一点。第一个想法指向window_size参数,但实际上该参数与结果列表中的第一个文档没有关联,但是每个分片都返回了结果数。另外window_size不能小于页面大小。 (如果它更少,ElasticSearch会默默使用页面大小)。另外,一个非常重要的事情 - 重新分类不能与排序相结合,因为排序是在重新引入更改后完成的。"

http://elasticsearchserverbook.com/elasticsearch-0-90-using-rescore/

我的查询是:

{
"query": {
    "filtered": {
        "query": {
            "bool": {
                "should": [
                    {
                        "constant_score": {
                            "query": {
                                "match": {
                                    "question": {
                                        "query": "diabetes"
                                    }
                                }
                            },
                            "boost": 1
                        }
                    },
                    {
                        "dis_max": {
                            "queries": [
                                {
                                    "constant_score": {
                                        "query": {
                                            "match": {
                                                "question": {
                                                    "query": "diabetes"
                                                }
                                            }
                                        },
                                        "boost": 0.01
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "match": {
                                                "answer_text": {
                                                    "query": "diabetes"
                                                }
                                            }
                                        },
                                        "boost": 0.0001
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "dis_max": {
                            "queries": [
                                {
                                    "constant_score": {
                                        "query": {
                                            "match_phrase": {
                                                "question_phrase": {
                                                    "query": "what is diabetes",
                                                    "slop": 0
                                                }
                                            }
                                        },
                                        "boost": 100
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "match_phrase": {
                                                "question_phrase": {
                                                    "query": "what is diabetes",
                                                    "slop": 1
                                                }
                                            }
                                        },
                                        "boost": 50
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "match_phrase": {
                                                "question_phrase": {
                                                    "query": "what is diabetes",
                                                    "slop": 2
                                                }
                                            }
                                        },
                                        "boost": 33
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "match_phrase": {
                                                "question_phrase": {
                                                    "query": "what is diabetes",
                                                    "slop": 3
                                                }
                                            }
                                        },
                                        "boost": 25
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "question_group_four",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 0.1
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "question_group_five",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 0.15
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_20",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 35
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_15",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 25
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_10",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 15
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_20",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 28
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_15",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 16
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_10",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 13
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_05",
                                                "query": "what__is__diabetes"
                                            }
                                        },
                                        "boost": 4
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "dis_max": {
                            "queries": [
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "question_group_four",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 0.1
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "question_group_five",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 0.15
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_20",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 35
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_15",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 25
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_no_synonyms_10",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 15
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_20",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 28
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_15",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 16
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_10",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 13
                                    }
                                },
                                {
                                    "constant_score": {
                                        "query": {
                                            "query_string": {
                                                "default_field": "concept_words_05",
                                                "query": "diabetes"
                                            }
                                        },
                                        "boost": 4
                                    }
                                }
                            ]
                        }
                    }
                ],
                "disable_coord": true
            }
        },
        "filter": {
            "and": [
                {
                    "term": {
                        "posted_by_expert": false
                    }
                },
                {
                    "term": {
                        "tip_question": false
                    }
                },
                {
                    "term": {
                        "show_in_work_queue": true
                    }
                },
                {
                    "range": {
                        "verified_answers_count": {
                            "gt": 0
                        }
                    }
                }
            ]
        }
    }
},
"rescore": {
    "window_size": 100,
    "query": {
        "rescore_query": {
            "function_score": {
                "functions": [
                    {
                        "script_score": {
                            "script": "_score * _source.concierge_boost"
                        }
                    }
                ]
            }
        }
    }
},
"sort": [
    "_score",
    {
        "count_words_with_high_concepts": {
            "order": "asc"
        }
    },
    {
        "popularity": {
            "order": "desc"
        }
    },
    {
        "length": {
            "order": "asc"
        }
    }
],
"fields": [],
"size": 10,
"from": 0

}

任何帮助高度赞赏!

1 个答案:

答案 0 :(得分:2)

事实上,这是不可能的。 But this has been discussed并决定目前不值得实施。然而,关于github的讨论揭示了这方面的困难 - 需要对文档进行排序,选择前100(在您的情况下),然后应用rescore,然后再对它们进行排序。我建议阅读github问题中的评论,尤其是来自simonw的评论。这个问题仍然存在,但似乎不会很快实施,如果它将完全实现的话。

关于你在另一级评分后的排序,我理解只需要重新调整几个文档,但似乎不可能。如果您将查询包装在另一个function_score中,并在其中定义script_score函数来计算最终得分,该怎么办?像这样:

{
  "query": {
    "function_score": {
      "query": {
        .......
      },
      "functions": [
        {
          "script_score": {
            "script": "doc['concierge_boost'].value"
          }
        }
      ]
    }
  },
  "sort": [
    "_score",
    {
        "count_words_with_high_concepts": {
            "order": "asc"
        }
    },
    {
        "popularity": {
            "order": "desc"
        }
    },
    {
        "length": {
            "order": "asc"
        }
    }
  ],
  "fields": [],
  "size": 10,
  "from": 0
}