Rails and Elasticsearch - Filters, weights, and statistics
<p>Consider the following model</p>

<pre><code>class MyModel # `Employee` used in concrete examples
  field :field1 # company_name used in concrete examples
  field :field2 # job_name used in concrete examples
  scope: filter1 # non_retired
  scope: filter2
end
</code></pre>

<ul>
<li>I need to filter / do named search queries, eg filter by non_retired</li>
<li>I need to weights some fields (eg give 3 times more importance to some fields)</li>
<li>I need to get statistics based on the <strong>total</strong> results (ie. not just the first 10 paginated results), for example, aggregating the company_names (I have already the code working, but I have problems on getting the "total" results ids.</li>
</ul>

<p>I have problems putting all that together. From <a href="https://stackoverflow.com/questions/26002833/rails-elasticsearch-named-scope-search">Rails elasticsearch - named scope search</a> I understood that I needed to feed ids directly to Elasticsearch.</p>

<pre><code>def search_by_id(query, type, ids, options = {})
  self.weighted_search(query, options.deep_merge({
    query: {
      multi_match: {
        filter: {
          ids: {
            values: ids
          }.tap do |filter|
          filter[:type] = type if type
        end
        }
      }
    }
  }))
end

def weighted_search(query, options = {})
  self.__elasticsearch__.search(
    {
      query: {
        multi_match: {
          query: query,
          fields: [
            "company_name^3",
            "job_name^2",
          ],
          # strategy: 'leap_frog_filter_first'
          # PROBLEM :cannot use this strategy on multi_match ?
        }
      }
    }.deep_merge(options)
  )
end
</code></pre>

<p>This is generating a BadRequest error with the following explanation</p>

<blockquote>
  <p>[400] {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[match] query does not support [$oid]</p>
</blockquote>

<p><strong>I do not understand this error... I cannot filter by ids ??</strong></p>

<p>Then, suppose I have this working, how do I extract the subsets of ids that were a match for ElasticSearch ??</p>

<pre><code>search = MyModel.search('query')
search.total_results # =&gt; 81
search.records.count # =&gt; 10
</code></pre>

<p>But I need to get all the 81 ids so I can perform some statistics (ie. aggregate the company names, I already have some code working, but now I only get the first 10 results aggregated...)</p>