0 votes
1 view
in AWS by (18.2k points)

I have my elasticsearch query that returns record between the range of publishedDates:

{

  query: {

    bool: {

      filter: [

      ],

      must: {

        range: {

          publishedDate: {

            gte: "2018-11-01",

            lte: "2019-03-30"

          }

        }

      }

    }

  }

  from: 0,

  size: 3,

}

I need to show 3 random results every time I send this query

It is mentioned in the elastic search documentation that I can send a seed to get random results:

After following the documentation, I updated my query as:

{

  "query" : {

    "bool": {

      "filter": [

      ],

      "must": {

        "range": {

          "publishedDate": {

            "gte": "2018-11-01",

            "lte": "2019-03-30"

          }

        }

      }

    },

    "function_score": {

      "functions": [

        {

          "random_score": {

            "seed": "123123123"

           }

        }

      ]

    }

  },

  "from": 0,

  "size": 3

}

But it is not working (saying query is malformed), can anyone suggest how to correct this query to return 3 random search results.

1 Answer

0 votes
by (42.4k points)

If you only want random results, then you can use this (Documentation):

{

  "query": {

    "function_score": {

      "query": {

        "range": {

          "publishedDate": {

            "gte": "2018-11-01",

            "lte": "2019-03-30"

          }

        }

      },

      "boost": "5",

      "random_score": {},

      "boost_mode": "multiply"

    }

  },

  "from": 0,

  "size": 3

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...