0

我正在尝试使用 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
    }
   }

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

4

1 回答 1

1

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

在这种情况下,总是有一个也是唯一的第一个问题:映射看起来如何真实

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

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


当索引映射与模型中定义的映射不同时,会出现关于显示警告的轮胎 问题。

于 2013-01-11T16:59:32.103 回答