title: Elasticsearch custom sorting / adding filter clauses scores
body: <p>I have this simple documents set:</p>
  
  <pre><code>{
    id : 1,
    book_ids : [2,3],
    collection_ids : ['a','b']
  },
  {
    id : 2,
    book_ids : [1,2]
  }
  </code></pre>
  
  <p>If I run this <strong><em>filter query</em></strong>, it will match both documents:</p>
  
  <pre><code>{
      bool: {
          filter: [
              {
                  bool: {
                      should: [
                          {
                              bool: {
                                  must_not: {
                                      exists: {
                                          field: 'book_ids'
                                      }
                                  }
                              }
                          },
                          {
                              bool: {
                                  filter: {
                                      term: {
                                          book_ids: 2
                                      }
                                  }
                              }
                          }
                      ]
                  }
              },
              {
                  bool: {
                      should: [
                          {
                              bool: {
                                  must_not: {
                                      exists: {
                                          field: 'collection_ids'
                                      }
                                  }
                              }
                          },
                          {
                              bool: {
                                  filter: {
                                      term: {
                                          collection_ids: 'a'
                                      }
                                  }
                              }
                          }
                      ]
                  }
              }
          ]
      }
  }
  </code></pre>
  
  <p>The thing is I want to sort these documents, and I would like the first one (id: 1) to be returned first because it matched both the <code>book_ids</code> value and the <code>collection_ids</code> values provided.</p>
  
  <p>A simple <strong><em>sort</em></strong> clause like this one is not working:</p>
  
  <pre><code>[
    'book_ids',
    'collection_ids'
  ]
  </code></pre>
  
  <p>because it will return first document 2 due to the <code>book_ids</code> array first value.</p>
  
  <p><em>Edit</em>: this is a simplified example of the problem I am facing, which has N such clauses in the should clause. Moreover there is an order between the clauses, as I tried to reflect with the <code>sort</code> snippet: results matching the first clause (<code>book_ids</code>) should appear before results matching the second clause (<code>collection_ids</code>). I am really looking for some kind of SQL sort operation where I would only take into account the matching value of the field array. A viable option might be to assign decreasing constant_scores to each <code>term</code> clause, according to the expected sort order, and ES would have to sum this sub-scores to compute the final score. But I cannot figure out how to do it or if it is even possible.</p>
  
  <p><em>Bonus question</em>:
  is there any way for ElasticSearch to return some kind of new document with only the matching values? Here is what I would expect as a response to the above <strong><em>filter query</em></strong>:</p>
  
  <pre><code>{
    id : 1,
    book_ids : [2],
    collection_ids : ['a']
  },
  {
    id : 2,
    book_ids : [2]
  }
  </code></pre>