【问题标题】:Is it possible to do this sort in ElasticSearch, without using script_score?是否可以在不使用 script_score 的情况下在 ElasticSearch 中进行这种排序?
【发布时间】:2016-08-21 03:29:25
【问题描述】:

我想用一个 ElasticSearch 查询来进行这种排序(不使用 script_score):

  1. region=DE 和 language=de 的对象,按 createdDate 排序。
  2. region=DE 和任何语言的对象,按 createdDate 排序。
  3. 具有任何地区和语言=en 的对象,按 createdDate 排序。

起初,我以为我可以做一个function_score查询(boost_mode:replace,score_mode:sum)和:

  1. 如果 region=DE 且 language=de,则将分数设置为 300000000000000 + createdDate。
  2. 如果 region=DE 和 language!=de,则将分数设置为 200000000000000 + createdDate。
  3. 如果 region!=DE 和 language=en,则将分数设置为 100000000000000 + createdDate。

我可以使用 field_value_factor 将 createdDate 添加到分数中。但是如果 region=DE 和 language=de,我找不到 function_score function 将 300000000000000 添加到分数中。

是否可以在不使用 script_score 的情况下做到这一点?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这是怎么做的:

    {
        "sort": [
            {"_score": "desc"},
            {"created_date": "desc"}
        ],
        "query": {
            "function_score": {
                "query": {
                    "bool": {
                        "minimum_should_match": 1,
                        "should": [
                            {
                                "constant_score": {
                                    "filter": {
                                        "bool": {
                                            "must": [
                                                { "term": { "region": "DE" } },
                                                { "term": { "language": "de" } }
                                            ]
                                        }
                                    },
                                    "boost": 3
                                }
                            },
                            {
                                "constant_score": {
                                    "filter": {
                                        "bool": {
                                            "must": [
                                                { "term": { "region": "DE" } },
                                                { "not": { "term": { "language": "de" } } }
                                            ]
                                        }
                                    },
                                    "boost": 2
                                }
                            },
                            {
                                "constant_score": {
                                    "filter": {
                                        "bool": {
                                            "must": [
                                                { "not": { "term": { "region": "DE" } } },
                                                { "term": { "language": "en" } }
                                            ]
                                        }
                                    },
                                    "boost": 1
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 2018-05-05
      • 1970-01-01
      • 2022-11-14
      • 2011-09-14
      相关资源
      最近更新 更多