Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k 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 (44.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

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Jul 18, 2019 in AWS by yuvraj (19.1k points)

Browse Categories

...