title: Elasticsearch - Nested value query inside field_value_factor
body: <p>I'm new to ES and am looking to create a searchable catalogue of products for users but I can't work out the way to encode different users having bought the same products.</p>
  
  <p>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. </p>
  
  <p>I need to create the ability to search the products and have products that the particular user has bought get higher scoring over others. My issue is that I don't know how to pull out this field inside the field_value_factor function as it may not exist for all products.</p>
  
  <p>My closest try so far (thanks to Val) is:</p>
  
  <pre><code>{
     "query": {
        "bool": {
           "should": [
              {
                 "multi_match": {
                    "query": "black toner",
                    "fields": [
                       "name",
                       "description"
                    ],
                    "tie_breaker": 0.3
                 }
              },
              {
                 "query": {
                    "function_score": {
                       "query": {
                          "bool": {
                             "must": [
                                {
                                   "multi_match": {
                                      "query": "black toner",
                                      "fields": [
                                         "name",
                                         "description"
                                      ],
                                      "tie_breaker": 0.3
                                   }
                                },
                                {
                                   "nested": {
                                      "path": "user",
                                      "query": {
                                         "term": {
                                            "user.userid": "MWUser2"
                                         }
                                      }
                                   }
                                }
                             ]
                          }
                       },
                       "functions": [
                          {
                             "field_value_factor": {
                                "field": "user.count",
                                "modifier": "log1p",
                                "missing": 0
                             }
                          }
                       ]
                    }
                 }
              }
           ]
        }
     }
  }
  </code></pre>
  
  <p>The issue here is that I cannot apply the <code>nested``path</code> to the <code>field_value_factor</code>, so this is always coming out as 0 and the user specific scoring boost isn't working. When the <code>nested``path</code> is applied around the entire <code>function_score</code> the first <code>multi_match</code> query on <code>description</code> and <code>name</code> does not work.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>Another way to do this may be to calculate the scores separately and then combine them. I can do this, but the <code>should</code> method of combining them will normalise the scores which is not what I want. So instead of doing <code>0.9 + 4</code> and <code>0.5 + 5</code> I get <code>0.7+0.7</code> for both. Is there any way around this?</p>
  
  <pre><code>{ 
     "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
                                   }
                                }
                             ]
                          }
                       }
                    }
                 }
              }
           ]
        }
     }
  }
  </code></pre>
  
  <p>My mapping is:</p>
  
  <pre><code>{
    "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"}
        }
      }
    }
  }   
  </code></pre>
  
  <p>Some documents are:</p>
  
  <pre><code>{
    "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.\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",
    "@timestamp": "2016-09-21T13:38:30.077Z"
  }
  </code></pre>