标签: elasticsearch-rails

如何使用ElasticSearch-Rails查询dsl返回相关关系

我是ElasticSearch的新手,但需要使用它来返回产品列表.请不要包含旧答案的答案或链接,这些答案会引用已弃用的轮胎宝石.

的Gemfile

ruby '2.2.0'
gem 'rails', '4.0.3'
gem 'elasticsearch-model', '~> 0.1.6'
gem 'elasticsearch-rails', '~> 0.1.6'
Run Code Online (Sandbox Code Playgroud)

我有几个关系模型.我在下面列出了关系.

模型和关系

product.rb 包括Searchable

  belongs_to :family
  belongs_to :collection
  has_many :benefits_products
  has_many :benefits, :through => :benefits_products

  def as_indexed_json(options={})
    as_json(
        include: {:benefits => { :only => [ :id, :name ] },
                  :categories => { :only => [ :id, :name ] } }
    )
  end
Run Code Online (Sandbox Code Playgroud)

collection.rb

  include Searchable

  has_many :products

  def as_indexed_json(options={})
    as_json(
      include: [:products]
    )
  end
Run Code Online (Sandbox Code Playgroud)

family.rb

  include Searchable

  has_many :products

  def as_indexed_json(options={})
    as_json(
      include: [:products]
    ) …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails relationship elasticsearch ruby-on-rails-4 elasticsearch-rails

22
推荐指数
1
解决办法
2558
查看次数

升级到Rails 4.2.5.1后出现VersionConflictEngineException

在将Rails更新到4.2.5.1并更新用户文档后,我不时会收到此异常

Elasticsearch :: Transport :: Transport :: Errors :: Conflict:[409] {"error":"VersionConflictEngineException [[users] [0] [user] [1]:版本冲突,当前[7],提供[6] ]", "状态":409}

但是,我没有在更新请求中传递任何显式版本.我想两个不同的进程正在尝试同时更新同一个文档,但除了Rails版本之外它没有任何改变.

我正在使用elasticsearch-model(0.1.8)elasticsearch-rails(0.1.8)elasticsearch(1.0.15)elasticsearch-api(1.0.15)和elasticsearch-transport(1.0.15)

elasticsearch版本是Version:1.2.2,Build:9902f08/2014-07-09T12:02:32Z,JVM:1.7.0_65

ruby-on-rails elasticsearch elasticsearch-model elasticsearch-rails

7
推荐指数
0
解决办法
797
查看次数

Elasticsearch Rails as_indexed_json与映射

我正在使用Elasticsearch Rails gem,并且在模型中使用了两件事:

def as_indexed_json

end
Run Code Online (Sandbox Code Playgroud)

settings index: { number_of_shards: 1 } do
  mapping dynamic: 'false' do
    indexes :id
    indexes :customer do                                                                                                                                                                                                                          
      indexes :first_name
    end
  end
end 
Run Code Online (Sandbox Code Playgroud)

我已经阅读了文档,但不了解它们各自的目的。我要弄清楚的是这些用于搜索索引数据还是用于创建索引数据?

ruby ruby-on-rails elasticsearch elasticsearch-rails

6
推荐指数
1
解决办法
1504
查看次数

将 Elasticsearch 结果范围限定为特定 ID

我有一个关于 Elasticsearch DSL 的问题。

我想进行全文搜索,但将可搜索记录的范围限定为特定的数据库 ID 数组。

在 SQL 世界中,它的功能相当于WHERE id IN(1, 2, 3, 4).

我一直在研究,但我发现 Elasticsearch 查询 DSL 文档有点神秘并且缺乏有用的示例。任何人都可以指出我正确的方向吗?

elasticsearch elasticsearch-rails elasticsearch-dsl

5
推荐指数
1
解决办法
3486
查看次数

在Elasticsearch Rails中为jsonb字段的属性添加索引

我是 elasticsearch 的初学者,我想为我拥有的 jsonb 字段内的字段添加索引。这不是嵌套关系。

我的表有效负载包含字段id(整数)、user_id(整数)、data(jsonb)。

示例 jsonb 值如下:

{"name" => "Test User", "values" => {"age" => 24, "gender" => "male"}, "married": false}
Run Code Online (Sandbox Code Playgroud)

我想在“数据”(jsonb 列)的“值”部分内添加“性别”字段的索引。

数据库是postgres。

我添加了索引配置如下:

  mappings do
    indexes :id,      type: 'integer'
    indexes :user_id, type: 'integer'

    indexes :data do
      indexes :gender
    end
  end
Run Code Online (Sandbox Code Playgroud)

这是正确的吗?

我得到了查询的准确结果,

{"query": {
    "term": {
        "user_id": 1
    }
}}
Run Code Online (Sandbox Code Playgroud)

但不适用于此查询

{"query": {
    "term": {
        "gender": "male"
    }
}}
Run Code Online (Sandbox Code Playgroud)

提前致谢 !!!

postgresql ruby-on-rails elasticsearch elasticsearch-model elasticsearch-rails

5
推荐指数
1
解决办法
1635
查看次数

Elasticsearch-rails中处于同一级别的多个聚合

我正在尝试使用elasticsearch-rails和elasticsearch-model gem在ElasticSearch的同一级别上执行多个聚合。

在我生成的查询哈希中,有以下内容-

    def query_hash(params, current_person = nil, manager_id = nil)
      aggs = {}
      aggs[:home_country_id] = {terms: {field: "home_country_id"}}
      aggs[:home_region_id] = {terms: {field: "home_region_id"}}
      {
        sort: [ { created_at: { order: "desc" } }, id: { order: "desc" } ],
        aggs: aggs
      }
    end
Run Code Online (Sandbox Code Playgroud)

我存储在对象es_response中的响应。

当我搜索两个聚合时,我只能在响应中找到最后一个。

es_response.response [“ aggregrations”]仅具有最新聚合对象的响应home_region_id

尽管有很多关于嵌套聚合的信息,但是我在《 ES参考》上找不到太多关于在同一级别上构造多个聚合的文档。

我怎样才能解决这个问题?

我的ES版本是5.1

ruby-on-rails elasticsearch elasticsearch-rails

5
推荐指数
0
解决办法
327
查看次数

运行测试时 ElasticSearch Rails resource_already_exists_exception

我正在尝试运行我的一个测试,该测试进行搜索,试图断言搜索结果中包含记录,但与此同时,我收到一个Elasticsearch::Transport::Transport::Errors::BadRequest错误:

SearchTest#test_simple_test_returns_product:
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] 

{
  "error":{
    "root_cause":[
      {
        "type":"resource_already_exists_exception",
        "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
        "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
        "index":"app_application_test_products"
      }
    ],
    "type":"resource_already_exists_exception",
    "reason":"index [app_application_test_products/FTt1YC6eQrCw2XwJuqjmDw] already exists",
    "index_uuid":"FTt1YC6eQrCw2XwJuqjmDw",
    "index":"app_application_test_products"
  },
  "status":400
}
Run Code Online (Sandbox Code Playgroud)

当我在开发中执行搜索时,它按预期工作,但在测试中抛出此类错误,在测试中我添加了导入和索引刷新,仅此而已:

class SearchTest < ActiveSupport::TestCase
  setup do
    Product.import force: true
    Product.__elasticsearch__.refresh_index!
  end

  test "simple test returns product" do
    product = products(:one)
    I18n.locale = product.market.lang
    search = Search.new(
      category: product.category.custom_slug,
      page: 1,
      market_id: product.market_id,
      status: "active",
      seed: Date.today.to_time.to_i
    )
    assert_includes search.results.records, products(:one)
    assert_includes search.results.records, products(:two)
    assert_not_includes search.results.records, products(:three)
  end
end
Run Code Online (Sandbox Code Playgroud)

任何帮助以及改进代码的提示都将受到赞赏。

我在用着: …

ruby ruby-on-rails minitest elasticsearch elasticsearch-rails

5
推荐指数
1
解决办法
1万
查看次数

如何通过elasticsearch rails中的ID获取文档

我在elasticsearch文档中看到,您可以通过其ID获取文档.elasticsearch rails中有没有相应的东西?我正在通过API提供"as_indexed_json"并且这是一个有点昂贵的查询,我想在我的API中直接从弹性搜索中返回JSON.

elasticsearch elasticsearch-rails

4
推荐指数
2
解决办法
2015
查看次数

嵌套关联 - elasticsearch-rails还是耐嚼?

我有一个带有ActiveRecord模型的Rails应用程序,它们之间有关联.

为了简单起见,假设我的数据库架构完全相同 - https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb#L95

我想搜索名为"John"的文章作者.

Article.search('john')按预期在article.authors的指定字段first_name和last_name中搜索.

我想更具体一点,只能通过first_name通过article.authors搜索文章.

Article.search(authors: {first_name: 'john'}) 不起作用.

做上述事情的正确方法是什么?

同样使用Elastic HQ,在文章索引中有字段作者.这是否意味着elasticsearch-rails索引是正确的并且嵌套作者? 弹性hq文章映射

ruby-on-rails elasticsearch elasticsearch-rails

4
推荐指数
1
解决办法
1501
查看次数

Rails __elasticsearch__.create_index!“根映射定义具有不支持的参数(mapper_parsing_exception)”

我在使用elasticsearch-rails时遇到问题,当我使用时Business.__elasticsearch__.create_index!出现错误:

{“error”:{“root_cause”:[{“type”:“mapper_parsing_exception”,“reason”:“根映射定义具有不受支持的参数:[业务:{dynamic=true,properties={id={type=integer}” }}]"}],"type":"mapper_parsing_exception","re​​ason":"无法解析映射 [_doc]: 根映射定义具有不受支持的参数: [business : {dynamic=true,properties={id={type =integer}}}]","caused_by":{"type":"mapper_parsing_exception","re​​ason":"根映射定义具有不受支持的参数:[业务:{dynamic=true,properties={id={type=integer }}}]“}},”状态“:400}

该请求的背后是:

PUT http://localhost:9200/development_businesses [状态:400,请求:0.081s,查询:N/A] {"settings":{"index":{"number_of_shards":1}},"mappings":{ "business":{"dynamic":"true","properties":{"id":{"type":"integer"}}}}}

我的模型代码:

`
after_save :reindex_model
Elasticsearch::Model.client = Elasticsearch::Client.new url: ENV['BONSAI_URL'], log: true
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name [Rails.env, model_name.collection.gsub('///', '-')].join('_')
document_type self.name.downcase
`
Run Code Online (Sandbox Code Playgroud)

我已经定义了我的映射:

`
settings index: { number_of_shards: 1 } do
    mappings dynamic: 'true' do
        indexes :id, type: 'integer'
    end
end
`
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails elasticsearch elasticsearch-model elasticsearch-rails

4
推荐指数
1
解决办法
1475
查看次数