ruby-on-railselasticsearchtire

Elastic Search nested


I'm using Elastic search through tire gem.

Given this structure to index my resource model

mapping do
  indexes :_id
  indexes :version,             analyzer: 'snowball', boost: 100 
  indexes :resource_files do
    indexes :_id
    indexes :name,                analyzer: 'snowball', boost: 100
    indexes :resource_file_category do
      indexes :_id
      indexes :name,                analyzer: 'snowball', boost: 100
    end
  end
end

How can i retrieve all the resources that have resource_files with a given resource_file_category id?

i've looked in the elastic search docs and i think could be using the has child filter http://www.elasticsearch.org/guide/reference/query-dsl/has-child-filter.html

i've tried this way

filter :has_child, :type => 'resource_files', :query => {:filter => {:has_child => {:type => 'resource_file_category', :query => {:filter => {:term => {'_id' => params[:resource_file_category_id]}}}}}}

but i'm not sure if is possible/valid to make a "nested has_child filter" or if is there a better/simpler way to do this... any advice is welcome ;)


Solution

  • I'm afraid I don't know what your mapping definition means. It'd be easier to read if you just posted the output of:

    curl -XGET 'http://127.0.0.1:9200/YOUR_INDEX/_mapping?pretty=1' 
    

    But you probably want something like this:

    curl -XGET 'http://127.0.0.1:9200/YOUR_INDEX/YOUR_TYPE/_search?pretty=1'  -d '
    {
       "query" : {
          "term" : {
             "resource_files.resource_file_catagory._id" : "YOUR VALUE"
          }
       }
    }
    '
    

    Note: The _id fields should probably be mapped as {"index": "not_analyzed"} so that they don't get analyzed, but instead store the exact value. Otherwise if you do a term query for 'FOO BAR' the doc won't be found, because the actual terms that are stored are: ['foo','bar']

    Note: The has_child query is used to search for parent docs who have child docs (ie docs which specify a parent type and ID) that match certain search criteria.