Search code examples
ruby-on-railsrubyelasticsearchruby-on-rails-5

Get all records without childrens even if they exists in elasticsearch


I am using elasticsearch_rails gem to do elasticsearch queries. I have mappings like this

mapping do
    indexes :id, type: 'keyword'
    indexes :name, type: 'text'
    indexes :slug, type: 'keyword'
    indexes :variants, type: 'nested' do
      indexes :id, type: 'keyword'
      indexes :sku, type: 'keyword'
      indexes :main, type: 'boolean'
      indexes :price, type: 'float'
    end
  end

def as_indexed_json(options = {})
    as_json(only: ['id', 'name', 'slug'],
            include: {
                variants: {
                    only: ['id', 'sku', 'main', 'price']
                }
            })
  end

and what I am trying to do is to get all "parents" element, but without "variants". I mean I want to get all products without variants even if they have some variants.

I am trying to do it because when I have a product with large amount of variants (fe. product with 2.5k variants) elasticsearch returns me whole "collection" (product and all of its variants) and if I'm going to list 20 products with 2k variants each its gonna took forever.

I am retrieving all products with simple Product.elasticsearch.search('*') query

Regards.


Solution

  • To not transport all source data, the elasticsearch keyword to use is "_source" https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html