使用elasticsearch-rails将Elasticsearch渗滤器从v2转换为v5

时间:2018-07-11 19:47:03

标签: ruby-on-rails elasticsearch elasticsearch-rails

我目前将Elasticsearch 2.4版与elasticsearch-rails gem一起使用。我有一个用于地理查询的过滤器;我的索引中填充了geojson多边形(geo_shape,并指定了lat-lng,我将返回所有包含该lat long的多边形。

# Elasticsearch 2
class Zone
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  mapping do
    indexes :coordinates, type: "geo_shape"
  end

  def store_percolated_document
    Zone.es_client.index :index => Zone.index_name,
                                    :type  => '.percolator',
                                    :id    => id,
                                    :body  => { 
                                    :query => { 
                                      :geo_shape => { 
                                        :coordinates =>  {
                                          :shape => {
                                            :type => "polygon",
                                            :coordinates => coordinates.to_a
                                          }
                                      } 
                                    } 
                                  }
  end

  def self.lookup_lat_lng(lat, lng)
    es_client.percolate(:index => Zone.index_name,
                        :type => Zone.document_type,
                        :body => {
                          :doc => {
                            :coordinates => {
                              :type => "point", :coordinates => [lng, lat]
                            }
                          }
                        })
  end
end

我正在尝试将Elasticsearch升级到5.6.10,并且难以让渗滤器正常工作。我知道percolator系统从v2开始就发生了变化,我会尽最大努力关注文档,但是当我知道他们应该给我一个多边形时,我无法获得任何成功的查询。

# Elasticsearch 5
class Zone
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  mapping do
    indexes :coordinates, type: "geo_shape"
    indexes :query, type: "percolator"
  end

  def store_percolated_document
    Zone.es_client.index :index => Zone.index_name,
                                       :type  => 'zone',
                                       :id    => id,
                                       :body  => { 
                                        :query => { 
                                          :geo_shape => { 
                                            :coordinates =>  {
                                              :shape => {
                                                :type => "polygon",
                                                :coordinates => coordinates.to_a
                                              }
                                            } 
                                          } 
                                        } 
                                      }
  end

  def self.lookup_lat_lng(lat, lng)
      es_client.search(index: Zone.index_name, 
        body: { 
          query: { 
            percolate: { 
              field: "query", 
              document_type: "zone",
              document: { 
                coordinates: { 
                  type: "point", coordinates: [lat,lng] 
                } 
              } 
            } 
          } 
        }
      )
  end
end

0 个答案:

没有答案