Elasticsearch - 根据字段的特定值增强文档

标签 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"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

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

每个可以创建事件的用户还可以购买系统包。我想增加套餐值(value)更高的用户。我确定我需要使用 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"
            }
        }
    }
]

在返回这两个结果的查询中,基于加里的包裹具有更高的值(value)(即 user.package.成本)。

在其他方面相同的搜索中,Gary 的结果必须排在第一位。

有什么指点吗?

谢谢。

最佳答案

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

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

           }
        }
       ]
    }
 }

希望有帮助..

关于Elasticsearch - 根据字段的特定值增强文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509714/

相关文章:

elasticsearch - 使用 AWS IAM 通过网关访问 AWS ElasticSearch 的 Kibana

database - 在集中设置中需要 redis 服务器

elasticsearch - 字段名称约定

elasticsearch - ElasticSearch 中的索引与部分更新

elasticsearch - 匹配列表中的多个字段?

node.js - 请求包含无法识别的参数:[type] in elastic search while making request for getting suggestion

groovy - Elasticsearch-geo_point数组上运行distanceInKm的MissingMethodException

elasticsearch - 如何从ES查询中了解ES索引?

java - 如何从 ES 1.7 读取海量数据并索引到 ES 6.7

python - Elasticsearch按查询更新