【问题标题】:Boosting matched documents in Elasticsearch which have a certain tag提升 Elasticsearch 中具有特定标签的匹配文档
【发布时间】:2013-10-01 18:42:44
【问题描述】:

我有一个看起来像这样的文档索引:

{
    url: "/foo/bar",
    html_blocks: [
       "<h1>hi</h1>"
    ],
    tags: [
        "video",
        "text"
    ],
    title: "My title"
}

我想在 titlehtml_blocks 字段中查询这些文档,如果匹配有 video 标签。

到目前为止,我的查询如下所示:

{
    "query": {
        "query_string": {
            "query": "foo",
            "fields": [
                "title",
                "html_blocks"
            ]
        }
    }
}

如何修改它,以便它继续仅在现有查询中找到匹配项时才返回结果,但会为任何具有视频的结果添加提升 标签?谢谢!

【问题讨论】:

    标签: search lucene elasticsearch


    【解决方案1】:

    您需要一个 custom_filters_score,它只会在匹配时提高。请注意,过滤器输入不会被分析,因此如果需要分析,您可以将其包装在查询中。您的其他提升选项(虽然不是真正适用于这种情况)是提升查询,它有利于降级结果和 custom_score_query,它有利于根据一些计算值增加提升。

    见:Custom_filters_score

    {
        "query": {
            "custom_filters_score": {
                "query": {
                    "query_string": {
                        "query": "foo",
                        "fields": [
                            "title",
                            "html_blocks"
                        ]
                    }
                },
                "filters": [
                    {
                        "filter": {
                            "term": {
                                "tags": "video"
                            }
                        },
                        "boost": 3
                    }
                ]
            }
        }
    }
    

    编辑:

    这就是我所说的使用过滤器查询包装查询的意思。相信我,一旦你掌握了 ES 的窍门,你就会被嵌套得如此之深,以至于你会产生一些有史以来最令人满意的查询。

    {
        "query": {
            "custom_filters_score": {
                "query": {
                    "query_string": {
                        "query": "foo",
                        "fields": [
                            "title",
                            "html_blocks"
                        ]
                    }
                },
                "filters": [
                    {
                        "filter": {
                            //here comes the filter query, and I changed term to match
                            //since match analyzes
                            "query":{                          
                                "match": {
                                    "tags": "video"
                                }
                             }
                        },
                        "boost": 3
                    }
                ]
            }
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多