Is it possible to boost 'newest' items using elasticsearch? (FOQElasticaBundle)

后端 未结 3 1826
广开言路
广开言路 2020-12-07 15:22

I\'m currently implementing elasticsearch in my Symfony2 application via the FOQElasticaBundle and so far it\'s been working great based on boosts applied to various fields

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 15:49

    A full elasticsearch 5 example based on function_score. See this blogpost and function_score docs for more info.

    Allows for boosting more recent entries based on multiple date ranges, with varying strengths, on a gaussian curve without "hard cutoffs".

    {
        "query": {
            "function_score": {
    
                "score_mode": "sum", // All functions outputs get summed
                "boost_mode": "multiply", // The documents relevance is multiplied with the sum
    
                "functions": [
                    {
                        // The relevancy of old posts is multiplied by at least one.
                        // Remove if you want to exclude old posts
                        "weight": 1
                    },
                    {
                        // Published this month get a big boost
                        "weight": 5,
                        "gauss": {
                            "date": { // <- Change to your date field name
                                "origin": "2017-04-07", // Change to current date
                                "scale": "31d",
                                "decay": 0.5
                            }
                        }
                    },
                    {
                        // Published this year get a boost
                        "weight": 2,
                        "gauss": {
                            "date": { // <- Change to your date field name
                                "origin": "2017-04-07", // Change to current date
                                "scale": "356d",
                                "decay": 0.5
                            }
                        }
                    }
                ],
    
                "query": {
                    // The rest of your search here, change to something relevant
                    "match": { "title": "< your search string >" }
                }
            }
        }
    }
    

提交回复
热议问题