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

Postprocessing Elastic results with another search (migrating from Solr)

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

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 的 named queries in combination with the 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"
            ]
        }
    ]
}

您的查询中所有标记匹配的文档将在 matched_queries 部分包含 all_tokens_match的响应。