# Boosting by popularity in a nested field with a filter

**URL:** <https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261>\
**Category:** Elasticsearch\
**Created:** [October 5, 2016, 10:59am UTC](https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261 "2016-10-05T10:59:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinwatts](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@martinwatts](https://discuss.elastic.co/u/martinwatts)\
**Post date:** [October 5, 2016, 10:59am UTC](https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261/1 "2016-10-05T10:59:01Z")

</div>

I am trying to create a searchable catalogue of products where products that the user has bought before will get a boost in scoring.

I have an index full of products, and these products may have been bought multiple times by different users which I have represented using nesting. Some products have entries for all users, some will have none.

I need to use ES scoring on text matching on the name and description fields and then need to use function\_score field\_value\_factor on the nested field to pick out the particular users "count" field.

I have 2 queries, one that returns the count for the user I want, and one that does the full-text searching. The problem is I don't know how to combine these as when I use "should" both scores are normalised, when really I want to just add these \_scores together.

Is there a way to do this?

Attempted query:

```
GET /nest_index_toy/_search
{ 
   "query": {
      "bool": {
         "should": [
            {
               "query": {
                  "multi_match": {
                      "use_dis_max": false, 
                     "query": "black super quality toner",
                     "fields": [
                        "name^3",
                        "description"
                     ],
                     "tie_breaker": 0.3
                  }
               }
            },
            {
               "query": {
                  "nested": {
                     "path": "user",
                     "query": {
                        "function_score": {
                           "filter": {
                              "term": {
                                 "user.userid": "MWUser1"
                              }
                           },
                           "functions": [
                              {
                                 "field_value_factor": {
                                    "field": "user.count",
                                    "modifier": "log1p",
                                    "missing": 0
                                 }
                              }
                           ]
                        }
                     }
                  }
               }
            }
         ]
      }
   }
}

```

My mapping is:

```
{
  "mappings": {
    "nest_type": {
      "properties": {
        "id" : {"type":"string"},
        "company_code" : {"type":"string"},
        "name" : {"type":"string"},
        "description" : {"type":"string"},
        "virtual_entity" : {"type":"boolean"},
        "created_at" : {"type":"date"},
        "updated_at" : {"type":"date"},
        "user": {
          "type": "nested",
          "properties": {
            "userid": {"type":"string"},
            "count": {"type":"short"},
            "last_bought": {"type":"date"}
          } 
        },
        "@timestamp" : {"type":"date"}
      }
    }
  }
}   

```

Some documents are:

```
{
  "id": "C8061X",
  "company_code": "MWCOMPCODE",
  "name": "Black LaserJet Toner Cartridge",
  "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra sharp black text, smooth greyscales and fine detail in graphics.\n- HP LaserJet C8061 Family Print Cartridges with Smart Printing Technology with in-built reliability and rigorous quality testing ensure maximum printer uptime with minimum user intervention.\n- HP LaserJet C8061 Family Print Cartridges all-in-one design allow effortless installation and maintenance. Smart Printing Technology features monitoring of supplies status and usage information via the printers control panel or web browser.\n",
  "virtual_entity": false,
  "created_at": "2016-09-21T12:23:53.000Z",
  "updated_at": "2016-09-21T12:23:53.000Z",
  "user": [
    {
      "userid": "MWUser1",
      "count": 4,
      "last_bought": "2016-09-14T12:43:30.000Z"
    },
    {
      "userid": "MWUser2",
      "count": 2,
      "last_bought": "2016-09-14T10:00:00.000Z"
    }
  ],
  "@timestamp": "2016-09-21T13:38:30.077Z"
}
{
  "id": "C8061Y",
  "company_code": "MWCOMPCODE",
  "name": "Black LaserJet Toner Cartridge Super Quality",
  "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra quality sharp black text, smooth greyscales and fine detail in graphics.",
  "virtual_entity": false,
  "created_at": "2016-09-21T12:23:53.000Z",
  "updated_at": "2016-09-21T12:23:53.000Z",
  "@timestamp": "2016-09-21T13:38:30.077Z"
}
```

---

<div class="post-metadata">

**Author:** ![martinwatts](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@martinwatts](https://discuss.elastic.co/u/martinwatts)\
**Post date:** [October 5, 2016, 3:57pm UTC](https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261/2 "2016-10-05T15:57:19Z")

</div>

I think my issue was having extra "query" tags where I didn't need them. I ended up using the following:

```
GET /nest_index_toy/_search
{
   "query": {
      "bool": {
         "must": {
            "multi_match": {
               "use_dis_max": false,
               "query": "black toner super quality",
               "fields": [
                  "name^3",
                  "description"
               ],
               "tie_breaker": 0.3,
               "boost": 2
            }
         },
         "should": [
            {
               "multi_match": {
                  "use_dis_max": false,
                  "query": "black toner super quality",
                  "fields": [
                     "name^3",
                     "description"
                  ],
                  "tie_breaker": 0.3,
                  "boost": 2
               }
            },
            {
               "nested": {
                  "path": "user",
                  "query": {
                     "function_score": {
                        "filter": {
                           "term": {
                              "user.userid": "MWUser1"
                           }
                        },
                        "functions": [
                           {
                              "field_value_factor": {
                                 "field": "user.count",
                                 "modifier": "log1p",
                                 "missing": 0
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 6, 2016, 5:11am UTC](https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261/3 "2016-10-06T05:11:43Z")

</div>

Thanks for sharing your solution!

---

<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 5, 2017, 10:14pm UTC](https://discuss.elastic.co/t/boosting-by-popularity-in-a-nested-field-with-a-filter/62261/4 "2017-07-05T22:14:29Z")

</div>


