# Elasticsearch probability depends on document weight

**URL:** <https://discuss.elastic.co/t/elasticsearch-probability-depends-on-document-weight/164048>\
**Category:** Elasticsearch\
**Created:** [January 13, 2019, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-probability-depends-on-document-weight/164048 "2019-01-13T17:24:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitry\_Ermichev](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@Dmitry\_Ermichev](https://discuss.elastic.co/u/Dmitry_Ermichev)\
**Post date:** [January 13, 2019, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-probability-depends-on-document-weight/164048/1 "2019-01-13T17:24:01Z")

</div>

I want to make query, that returns random documents with probability of each document depends on it weight. In my index each document has weight from 1 to N. So element with weight 1 must appears in result 2 times less, than document with weight 2.  
For example I have 3 documents (one with weight 2, two with weight 1). So probability of each document appearance must be 50%, 25%, 25%. Example:

```auto
[
            {
                "_index": "we_recommend_on_main",
                "_type": "we_recommend_on_main",
                "_id": "5-0",
                "_score": 1.1245852,
                "_source": {
                    "id_map_placement": 6151,
                    "image": "/upload/banner1",
                    "weight": 2
                }
            },
            {
                "_index": "we_recommend_on_main",
                "_type": "we_recommend_on_main",
                "_id": "8-0",
                "_score": 0.14477867,
                "_source": {
                    "id_map_placement": 6151,
                    "image": "/upload/banner1",
                    "weight": 1
                }
            },
            {
                "_index": "we_recommend_on_main",
                "_type": "we_recommend_on_main",
                "_id": "8-1",
                "_score": 0.0837487,
                "_source": {
                    "id_map_placement": 6151,
                    "image": "/upload/banner2",
                    "weight": 1
                }
            }
        ]

```

I found the solution with search like this:

```auto
{
    "size": 1,
    "query": {
        "function_score": {
            "functions": [
                {
                    "random_score": {}
                },
                {
                    "field_value_factor": {
                        "field": "weight",
                        "modifier": "none",
                        "missing": 1
                    }
                }
            ],
            "score_mode": "multiply",
            "boost_mode": "replace"
        }
    },
    "sort": [
        {
            "_score": "desc"
        }
    ]
}

```

That solution was in issue [https://github.com/elastic/elasticsearch/issues/7783#issuecomment-64880008](https://github.com/elastic/elasticsearch/issues/7783#issuecomment-64880008), but it doesn't work as I expected.  
After i tested this query with 10000 times result is

```auto
{
        "5-0": 6730,
        "8-1": 1613,
        "8-0": 1657
    }

```

But not

```auto
{
        "5-0": 5000,
        "8-1": 2500,
        "8-0": 2500
    }

```

I asked this question on stackoverflow, and get answer, that my assumption about probability is wrong [https://stackoverflow.com/a/54133955/1848278I](https://stackoverflow.com/a/54133955/1848278I) understand this, but can't find what query I need to get needed results. Please help me.

---

<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:** [February 10, 2019, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-probability-depends-on-document-weight/164048/2 "2019-02-10T17:24:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
