EzDevInfo.com

retire

A rich Ruby API and DSL for the Elasticsearch search engine tire.rb

Elasticsearch – combine ids query with must_not clause

I’d like to combine ids query with must_not clause. I’m using (re)tire gem with rails application.

I’m doing something like this:

query do
  ids ["foo", "bar", "test"]
end

Which works. However, when I’m trying to combine it with must_not clause, I’m getting error – must_not works only withing boolean scope, which does not include ids query. Is there any way to combine that two queries?


Source: (StackOverflow)

Check if document key/id exists with Tire

I am using Tire gem as an elasticsearch client.

I wanted to know if there is a way to know if the document with known id exists in the index?

something like:

 Tire.exists? { index: 'myIndex', type: 'myType', id: 'myId' }

Source: (StackOverflow)

Advertisements

Rails Tire Search In Associations has_many

This is my model:

models/business_center.rb:

mapping do
  indexes :id, type: 'integer'
  indexes :name, type: 'string'
  indexes :address, type: 'string'
  indexes :offices_pub_count, type: 'integer'
  indexes :subtype_id, type: 'integer'
  indexes :offices_pub_retail_count, type: 'integer'
  indexes :metro_id, type: 'integer'
  indexes :area_id, type: 'integer'
  indexes :district_id, type: 'integer'
  indexes :latitude, type: 'integer'
  indexes :longitude, type: 'integer'

  indexes :offices do
    indexes :id,      type: 'integer'
    indexes :desc,      type: 'string'
    indexes :floor,      type: 'string'
    indexes :inner_info,      type: 'string'
    indexes :decoration,      type: 'string'
    indexes :fire_safety,      type: 'string'
    indexes :air_conditioning,      type: 'string'
    indexes :parking,      type: 'string'
    indexes :planning,      type: 'string'
    indexes :commercial_terms,      type: 'string'
    indexes :operation_cost_id,      type: 'string'
    indexes :retail,      type: 'boolean'

end

def as_indexed_json(options={})
  to_json(methods: [:offices_pub_count],
      include: {offices: { only: [:text,:desc,:floor,:inner_info,:decoration,:fire_safety,:air_conditioning,:parking,:planning,
                                     :commercial_terms,:operation_cost_id, :retail]
                }
      }

def offices_pub_count
  offices_pub.size
end

has_many :offices, :dependent => :destroy
has_many :offices_pub, class_name: 'Office', foreign_key:   'business_center_id', conditions: {published: true}    

And next time I try search as:

BusinessCenter.search(:load => { :include => 'offices' }) do
    query { string '*' }
    filter :term, "offices.retail" => true
end

and its get empty array

 => #<Tire::Results::Collection:0x0000000641fa90 @response={"took"=>4, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:load=>{:include=>"offices"}, :size=>10}, @time=4, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item>

Where I make mistake? How filtered request with attribute in has_many model (in my case its the Office)?

I saw many examples, but they are work with has_one or belongs_to associations. Example in https://gist.github.com/karmi/3200212 now work. Please, help me!)


Source: (StackOverflow)