首页 > 解决方案 > 在 _source 结果集中包含附加值

问题描述

我正在尝试查看是否可以在运行时根据查询更新 _source 结果集中的字段,下面是查询:

body = {

        "from": 0,
        "size": 5,
        "_source": ["*"],
        "query": {
            "bool": {
                "must": [
                       
                    {
                        "bool": {
                            "should": [
                                {
                                    "bool": {
                                        "must": [
                                            {
                                                "query_string": {
                                                    "query": "home visit*",
                                                    "fields": [
                                                        "name",
                                                        "procedures"
                                                    ],
                                                    "boost": 1
                                                }
                                            },
                                            {
                                                "script": {
                                                    "script": {
                                                        "source": "if (doc['policy_types_keyword'].size() != 0) { return true; } else { return false; }",
                                                        "lang": "painless"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                },
                                {
                                    "bool": {
                                        "must": [
                                            {
                                                "query_string": {
                                                    "query": "home visit*",
                                                    "fields": [
                                                        "name",
                                                        "keywords",
                                                        "specialties"
                                                    ],
                                                    "boost": 1
                                                }
                                            },
                                            {
                                                "script": {
                                                    "script": {
                                                        "source": "if (doc['policy_types_keyword'].size() != 0) { return true; } else { return false; }",
                                                        "lang": "painless"
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            ],
                            "minimum_should_match": 1
                        }
                    }
                ]

            }
        }
    }

如果结果来自 must 子句的第一部分,则 _source 应将 'procedures' 更新为 True

 "procedures": True

如果结果来自 must 子句的第二部分,则 _source 应包含“specialties”

"specialties": True

如果结果来自 must 的两个部分

"procedures": True
"specialties": True

基本上,我试图在结果集中添加一个字段,让客户知道结果行来自过程查询或专业查询,或两者兼而有之。有没有办法做到这一点?

标签: elasticsearch

解决方案


推荐阅读