0

I need to write a query in elasticsearch to get random 12 items in the top 100 sorted items.

I tried something like this, but I am unable to get random 12 items(I can get only the top 12 items).

The query I used:

GET product/_search
{
  "sort": [
    {
      "DateAdded": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "definitionName": {
                  "value": "ABC"
                }
              }
            },
            {
              "range": {
                "price": {
                  "gt": 0
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "random_score": {
            "seed": 314159265359
          }
        }
      ]
    }
  },
  "size": 12
}

Can anybody guide me where am I going wrong? (I am a beginner in writing ElasticQueries)

Thanks in Advance.

  • "sort": [ { "DateAdded": { "order": "desc" } } ] => if you want to use score calculated by your random, you need to remove this (or query { "_score": { "order": "desc" } }) – LeBigCat Feb 26 '20 at 08:57
  • How can I sort the results when I remove the code "sort": [ { "DateAdded": { "order": "desc" } } . Is there any other way to sort ? – vamsi penta Feb 26 '20 at 09:14

1 Answers1

0

EDIT: doesnot work, window_size recalculate score on the X top results. Also: need to set: "track_scores" to true at the top level. corect syntax is:

"rescore": {
    "window_size": 10,
    "query": {
      "score_mode": "max", //wathever
      "rescore_query": {
        "bool": {
          "should": [
            {
              //your query here - you can use a function or a script score too
            }
          ]
        }
      },
      "query_weight": 0.7,
      "rescore_query_weight": 1.2
    }
  }

Ok i understand better.

Indeed you have to sort by date (top 100) and rescore with a random function (read https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html#request-body-search-post-filter).

Should be something like:

{
  "sort": [
    {
      "DateAdded": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "definitionName": {
              "value": "ABC"
            }
          }
        },
        {
          "range": {
            "price": {
              "gt": 0
            }
          }
        }
      ]
    }
  },
  "size": 100,
  "rescore": {
    "window_size": 12,
    "query": {
      "rescore_query": {
        "random_score": {
          "seed": 314159265359
        }
      }
    }
  }
}
LeBigCat
  • 1,737
  • 1
  • 11
  • 16
  • When I am trying to use this code, I am getting error "[query] failed to parse field [rescore_query]". – vamsi penta Feb 26 '20 at 11:52
  • Well my bad, window_size will recalculate only the X top score, so as if you can make random score in them, all 100 will be returned in the response. I thinck you have to do it on application side. I edit response. – LeBigCat Feb 26 '20 at 13:55
  • It should be possible to achieve it using a script filter (in postfilter part), but it will be really slow. If you only need to return 15 elements instead of 100 it doesnot worth. If you need to return 10 instead of 10000, it could ;) (because of the size of the data returned to your application). – LeBigCat Feb 26 '20 at 14:00