【问题标题】:Elasticsearch/tire Nested Queries with persistant objects具有持久对象的 Elasticsearch/tire 嵌套查询
【发布时间】:2013-01-11 14:36:24
【问题描述】:

我正在尝试使用 Tire 对持久模型执行嵌套查询。模型(事物)具有标签,我正在寻找所有带有特定标签的事物

class Thing
  include Tire::Model::Callbacks
  include Tire::Model::Persistence

  index_name { "#{Rails.env}-thing" }

  property :title, :type => :string
  property :tags, :default => [], :analyzer => 'keyword', :class => [Tag], :type => :nested
end

嵌套查询看起来像

class Thing 
   def self.find_all_by_tag(tag_name, args)
      self.search(args) do
         query do
            nested path: 'tags' do
               query do
                  boolean do
                     must { match 'tags.name', tag_name }
                 end
               end
            end
         end
      end 
   end 
 end

当我执行查询时,我得到一个“不是嵌套类型”的错误

Parse Failure [Failed to parse source [{\"query\":{\"nested\":{\"query\":{\"bool\":{\"must\":[{\"match\":{\"tags.name\":{\"query\":\"TestTag\"}}}]}},\"path\":\"tags\"}},\"size\":10,\"from\":0,\"version\":true}]]]; nested: QueryParsingException[[test-thing] [nested] nested object under path [tags] is not of nested type]; }]","status":500}

查看轮胎的来源,似乎映射是从传递给“属性”方法的选项创建的,所以我认为我不需要在类中单独的“映射”块。谁能看到我做错了什么?

更新

按照下面 Karmi 的回答,我重新创建了索引并验证了映射是否正确:

thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: nested
      }
    }
    title: {
      type: string
    }
   }

但是,当我向 Thing 添加新标签时

thing = Thing.new
thing.title = "Title"
thing.tags << {:name => 'Tag'}
thing.save

映射恢复为“动态”类型并且“嵌套”丢失。

thing: {
  properties: {
    tags: {
      properties: {
        name: {
          type: string
        }
        type: "dynamic"
      }
    }
    title: {
      type: string
    }
   }

查询失败并出现与以前相同的错误。添加新标签时如何保留嵌套类型?

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch tire


    【解决方案1】:

    是的,确实,property 声明中的映射配置在 Persistence 集成中传递。

    在这种情况下,总是有一个也是唯一的第一个问题:真实的映射是什么样子的

    所以,使用例如。 Thing.index.mapping 方法或 Elasticsearch 的 REST API:curl localhost:9200/things/_mapping 看看。

    很有可能,您的索引是根据您使用的 JSON 使用动态映射创建的,并且您稍后更改了映射。在这种情况下,索引创建逻辑被跳过,映射不是你所期望的。


    Tireissue 打开了关于在索引映射与模型中定义的映射不同时显示警告的信息。

    【讨论】:

    • 非常感谢您的帮助,以及优秀的图书馆。添加新标签时,我仍然在保留嵌套文档类型时遇到问题。我已经用细节更新了这个问题,你能解释一下吗?
    • 我得到了这个工作,谢谢。我必须在每个测试的设置中调用 Thing.create_elasticsearch_index 以确保正确创建了映射。
    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    相关资源
    最近更新 更多