Elasticsearch - 根据字段的特定值

时间:2016-02-19 16:09:22

标签: elasticsearch

我有一个有效的Elasticsearch查询,如下所示:

{
    "index": "event",
    "type": "item",
    "body": {
        "query": {
            "filtered": {
                "query": {
                    "multi_match": {
                        "query": "party",
                        "type": "phrase_prefix",
                        "fuzziness": null,
                        "fields": [
                            "label^2",
                            "excerpt",
                            "body"
                        ]
                    }
                },
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "is_deleted": false
                                }
                            },
                            {
                                "term": {
                                    "is_private": false
                                }
                            },
                            {
                                "range": {
                                    "date_start": {
                                        "gt": "now"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

此查询会搜索与关键字搜索匹配的事件,这些事件将在未来开始,并过滤掉任何标记为已删除或私有的事件。

每个可以创建活动的用户也可以向系统购买包裹。我想提升其软件包价值更高的用户。我确定我需要使用Function Score Query来实现自定义评分功能,但我无法理解文档并盲目地尝试它并不能让我到任何地方。

例如,索引文档可能如下所示:

[
    {
        "label": "My Birthday Party",
        "excerpt": "Come celebrate with me as I turn 30",
        "body": "I'm turning 30 soon, let's go dancing!",
        "date_start": "2016-06-19",
        "is_deleted": false,
        "is_private": false,
        "user": {
            "id": 1,
            "name": "Pablo",
            "package": {
                "id": 14,
                "cost": "1000",
                "label": "The \"£10 per month\" package"
            }
        }
    },
    {
        "label": "House Warming Party",
        "excerpt": "I moved house, help me decorate it",
        "body": "I love my new house, but it's empty and I have no furniture.",
        "date_start": "2016-04-19",
        "is_deleted": false,
        "is_private": false,
        "user": {
            "id": 2,
            "name": "Gary",
            "package": {
                "id": 15,
                "cost": "5000",
                "label": "The \"£50 per month\" package"
            }
        }
    }
]

在一个返回这两个结果的查询中,我如何优先考虑可怜的加里(谁需要帮助装饰)优先于帕布罗的生日,基于加里的套餐具有更高的价值(即user.package.cost)。

在一个平等的搜索中,加里的结果必须先出现。

任何指针?

感谢。

1 个答案:

答案 0 :(得分:2)

使用script score来实现这一目标。

 "query": {
  "function_score": {
     "query": {
        "match_all": {}
     },
     "functions": [
        {
           "script_score": {
              "script": "_score * doc['user.package.cost'].value"

           }
        }
       ]
    }
 }

希望有所帮助......