# How to define a ratio of values which should be inside the result set of a function score query

**URL:** <https://discuss.elastic.co/t/how-to-define-a-ratio-of-values-which-should-be-inside-the-result-set-of-a-function-score-query/20241>\
**Category:** Elasticsearch\
**Created:** [October 14, 2014, 1:09pm UTC](https://discuss.elastic.co/t/how-to-define-a-ratio-of-values-which-should-be-inside-the-result-set-of-a-function-score-query/20241 "2014-10-14T13:09:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Hasenstab](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefan_hasenstab/32/131812_2.png) [@Stefan\_Hasenstab](https://discuss.elastic.co/u/Stefan_Hasenstab)\
**Post date:** [October 14, 2014, 1:09pm UTC](https://discuss.elastic.co/t/how-to-define-a-ratio-of-values-which-should-be-inside-the-result-set-of-a-function-score-query/20241/1 "2014-10-14T13:09:47Z")

</div>

Hi ES group,

i have a function score query, which searches for user profiles in a big  
pool (~2 million documents). The query works fine, but the index contains a  
lot more male profiles than female ones (around 100 times more). Now my  
goal is to somehow specify a ratio of the gender distribution of the  
result. For example i want to get 6 male and 4 female profiles at any time  
I query the index. How could I force ES to ensure this ratio?

I tried a filter on the gender attribute inside the function score query  
with a boost\_factor but it didn't worked out. The result contains about 9  
male and 1 female profile right now.

The query looks like the following (slightly stripped-down):

{  
"query": {  
"function\_score": {  
"boost\_mode": "replace",  
"score\_mode": "sum",  
"boost": 1,  
"filter": {  
// some hard filter, this shouldn't matter  
},  
"functions": [  
{  
"random\_score": {  
"seed": null  
}  
},  
{  
"boost\_factor": 1,  
"field\_value\_factor": {  
"field": "gender",  
"gender": "female"

```
      }
    },
    {
      "linear": {
        "location": {
          "origin": "", //some origin here
          "scale": "50km",
          "offset": "25km",
          "decay": 0.25
        }
      }
    },
    {
      "linear": {
        "lastlogin": {
          "origin": "2014-10-14",
          "scale": "30d",
          "offset": "1d",
          "decay": 0.5
        }
      }
    }
  ]
}

```

},  
"from": 0,  
"size": 10  
}

Best regards,  
Stefan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/237fdee5-3c8f-471e-b8fd-ce53f6b086be%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/237fdee5-3c8f-471e-b8fd-ce53f6b086be%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:56am UTC](https://discuss.elastic.co/t/how-to-define-a-ratio-of-values-which-should-be-inside-the-result-set-of-a-function-score-query/20241/2 "2017-07-06T00:56:21Z")

</div>

Closed on July 6, 2017, 12:56am UTC


