title: Combining multiple different function_score with Elasticsearch
body: <p>I've got several different Elasticsearch function_score but I'm not sure how to combine them</p>
  
  <p>This is the test set I'm looking at (I added comments to be able to refer to specific items in the question, these comments are not actually in the index)</p>
  
  <pre><code>[
      { // Item 1
          "priority": 0.7,
          "classification": [
              {
                  "feature": "A",
                  "confidence": 0.4
              },
              {
                  "feature": "C",
                  "confidence": 0.3
              },
              {
                  "feature": "B",
                  "confidence": 0.6
              }
          ]
      },
      { // Item 2
          "priority": 0.8,
          "classification": [
              {
                  "feature": "A",
                  "confidence": 0.3
              },
              {
                  "feature": "C",
                  "confidence": 0.6
              }
          ]
      },
      { // Item 3
          "priority": 0.4,
          "classification":  [
              {
                  "feature": "D",
                  "confidence": 0.6
              },
              {
                  "feature": "C",
                  "confidence": 0.8
              }
          ]
      }
  ]
  </code></pre>
  
  <p>Now assume I want to score items with the following weights: </p>
  
  <ul>
  <li>"A" with weight of 2</li>
  <li>"B" with weight of 3</li>
  </ul>
  
  <p>I would like to do the following:</p>
  
  <ol>
  <li>Calculate average confidence for each item only for features "A" and "B" (e.g. average confidence of 0.5 for item 1)</li>
  <li>Calculate priority for each item (e.g. popularity of 0.8 item 2)</li>
  <li>Calculate the sum of weights for each item feature (if item has feature "A" it receives a weight of 2, if
  it has feature "B" it receives a weight of 3. e.g. item 1 would receive a weight of 5 and item 2 a weight of 2)</li>
  <li>Combine the different calculations into a final score</li>
  </ol>
  
  <p>I know how to create the function_score for the average confidence, it would be something like this:</p>
  
  <pre><code>{
    "nested": {
      "path": "classification",
      "query": {
         "function_score": {
            "functions": [
                {
                    "field_value_factor": {
                        "field": "classification.confidence",
                        "missing": 0
                    },
                    "weight": 0
                }
            ],
            "query": {
                "terms": {
                    "classification.feature": [
                        "A",
                        "B"
                    ]
                }
            },
            "score_mode": "avg"
          }
      }
    }
  }
  </code></pre>
  
  <p>I also know how to create the function score for the priority field, it would be something like this:</p>
  
  <pre><code>{
      "function_score": {
          "functions": [
              {
                  "field_value_factor": {
                      "field": "popularity",
                      "missing": 0
                  },
                  "weight": &lt;some-weight&gt;
              }
          ],
          "score_mode": "sum"
      }
  }
  </code></pre>
  
  <p>I think (but not sure) I know how to create the function score for the sum of feature weights (ignoring weights for features that don't match "A" or "B"). It would probably be something like this:</p>
  
  <pre><code>{
    "query": {
          "function_score": {
              "query": {
                  "bool": {
                      "should": [
                          { "match": { "classification.feature": "A" } },
                          { "match": { "classification.feature": "B" } }
                      ]
                  }
              },
              "functions": [
                {
                    "filter": { "match": { "classification.feature": "A" } },
                    "weight": 2
                },
                {
                    "filter": { "match": { "classification.feature": "B" } },
                    "weight": 3
                },
              ],
              "score_mode":"sum"
          }
      }
  }
  </code></pre>
  
  <p>But I have no idea how to combine these 3 different function score (I'm currently not sure what would be the actual combine function. I will need to play with different functions and decide which one works best for me but for the question sake we can say I would like to do average on the results of my 3 function_score)</p>
  
  <p>And so my questions are:</p>
  
  <ol>
  <li>Is it possible to define multiple function_score and then define how to combine them?</li>
  <li>If it's not possible to combine multiple function_score what approach should I take in order to solve this issue? (I'm not fixated on using 3 different function_score but not sure how to do it otherwise)</li>
  <li>Although I said I want to do average on all the function_score results I may later want to do something a
  bit more complicated like this: <code>score("popularity") + (score("feature-weight") * score("confidence"))</code> - is
  there a way to achieve this?</li>
  </ol>
  
  <p>I'm currently testing this on ES 2.4.5 (which I know is deprecated). We are going to upgrade pretty soon
  anyway but:</p>
  
  <ul>
  <li>Is it only possible to achieve with later ES versions?</li>
  <li>Even if its only possible in later ES versions I would still like to know how to accomplish it (and use it after we upgrade)</li>
  </ul>
  
  <p>Googling this didn't result in any useful information</p>
  
  <p>Thanks in advance</p>