使用另一个搜索(从 Solr 迁移)对 Elastic 结果进行后处理



我目前正在将一个应用程序从 Solr 迁移到 Elastic,偶然发现了一个有趣的 Solr 功能,我无法在 Elastic 中重现:对 Solr 的查询返回一个后处理标志,该标志对结果进行质量检查,指示是否在结果字段中找到所有标记。

q  = some_field:(the brown fox)
fl = some_field, full_match:exists(query({!edismax v='some_field:(the brown fox)' mm='100%'}))

Solr 结果如下所示:

{
"response": {
"docs": [
{
"some_field": "The Brown Bear",
"full_match": false
},
{
"some_field": "The Quick Brown Fox",
"full_match": true
}
]
}
}

客户端使用该标志进一步处理结果文档,与分数无关(我在示例中省略了分数(。我发现这非常聪明,因为使用了Solr的标记化和分布式计算能力,而不是在客户端中做所有事情。

现在在 Elastic 中,我认为这应该在script_fields块中完成,但实际上我不知道如何使用无痛脚本执行子查询,经过两天的调查,我怀疑这是否可行:

{
"query": {
"match": {
"some_field": "the brown fox"
}
},
"_source": [
"some_field"
],
"script_fields": {
"full_match": {
"script": "???" <-- Search with Painless script?
}
}
}

欢迎任何创意。

如何将 Elasticsearch 的命名查询与 minimum_should_match 参数结合使用,并将其设置为 100% 以仅匹配所有令牌匹配的文档?

然后,您将能够检测响应中所有令牌匹配的查询。您还可以设置"boost":0以避免影响主查询的分数。

下面是一个示例请求:

{
"query": {
"bool": {
"should": [
{
"match": {
"message": {
"query": "the brown fox",
"_name": "main_query"
}
}
},
{
"match": {
"message": {
"query": "the brown fox",
"_name": "all_tokens_match",
"minimum_should_match": "100%",
"boost": 0
}
}
}
]
}
}
}

然后,您将获得如下所示的响应:

{
"hits": [
{
"_score": 0.99938476,
"_source": {
"message": "The Quick Brown Fox"
},
"matched_queries": [
"main_query",
"all_tokens_match"
]
},
{
"_score": 0.38727614,
"_source": {
"message": "The Brown Bear"
},
"matched_queries": [
"main_query"
]
}
]
}

然后,查询中的所有令牌都匹配的文档将all_tokens_match包含在响应的matched_queries部分中。

最新更新