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 ;)
The dot operator can be used to access nested data.
You can try something like this:
If resource_file_catagory is non_analyzed the value is not tokenized and stored as a single value, hence giving you an exact match.
You can also use elasticsearch-head plugin for data validation and also query building reference.
https://www.elastic.co/guide/en/elasticsearch/reference/1.4/modules-plugins.html or https://mobz.github.io/elasticsearch-head/
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:
But you probably want something like this:
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 aterm
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.