搜索嵌套嵌套时的Rails轮胎问题

时间:2013-09-09 17:33:32

标签: elasticsearch nested tire

我对elascticsearch和Tire很新。

我需要搜索以嵌套在发布中的格式嵌套的成本中的金额。这是映射:

mapping do
  indexes :id, type: 'integer'
  indexes :publisher_id, type: 'integer'
  indexes :states
  indexes :title
  indexes :created_at, type: 'date'
  indexes :updated_at, type: 'date'
  indexes :formats, type: 'nested' do
    indexes :pid, type: 'integer'
    indexes :key_type
    indexes :value, type: 'string', analyzer: 'whitespace'
    indexes :state
    indexes :protection
    indexes :nature
    indexes :start_sale_at, type: 'date'
    indexes :issued_on, type: 'date'
    indexes :format_aliases do
      indexes :value, analyzer: 'whitespace'
    end
    indexes :costs, type: 'nested' do
      indexes :id, type: 'integer'
      indexes :country
      indexes :format
      indexes :amount, type: 'integer'
    end
  end
end

但是当我执行我的查询时,我收到了这条消息:

{ 
  "error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure;
            shardFailures {[lTCvIZNERy-Q1M4u-fyf1w][publications][4]: SearchParseException[[publications][4]: from[-1],size[-1]: 
            Parse Failure [Failed to parse source [
              { 
                \"query\":{
                  \"filtered\":{
                    \"query\":{
                      \"bool\":{
                        \"must_not\":[
                          { 
                            \"term\": {
                              \"state\": {
                                \"term\":\"destroyed\"
                              }
                            }
                          }
                        ],
                        \"must\": [
                          { 
                            \"nested\": {
                              \"query\": {
                                \"bool\": {
                                  \"must\": [
                                    { 
                                      \"term\": {
                                        \"formats.nature\": {
                                          \"term\":\"pdf\"
                                        }
                                      }
                                    },
                                    { 
                                      \"nested\": {
                                        \"query\": {
                                          \"bool\": {
                                            \"must\": [
                                              { 
                                                \"term\": {
                                                  \"formats.costs.country\": {
                                                    \"term\":\"can\"
                                                  }
                                                }
                                              }, 
                                              { 
                                                \"term\": {
                                                  \"formats.costs.amount\": {
                                                    \"term\":\"499\"
                                                  }
                                                }
                                              }
                                            ]
                                          }
                                        },
                                        \"path\":\"costs\"
                                      }
                                    }
                                  ]
                                }
                              },
                              \"path\":\"formats\"
                            }
                          }
                        ]
                      }
                    },
                    \"filter\": {
                      \"and\": [
                        { 
                          \"nested\": {
                            \"path\":\"formats\",
                            \"query\": {
                              \"filtered\": {
                                \"query\": {
                                  \"match_all\":{}
                                },
                                \"filter\": {
                                  \"and\": [
                                    {
                                      \"exists\": {
                                        \"field\":\"costs\"
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                },
                \"size\":10
              }
            ]
          ]
        ];
        nested: QueryParsingException[[publications] [nested] failed to find nested object under path [costs]]; 
  }
}

这就是我实际上喜欢的搜索功能:

nested_price_filter = Tire::Search::Query.new do
  filtered do
    query { all }
    filter :exists, {:field => 'costs'}
  end
end

tire.search(load: true) do
  query do
    filtered do
      query do
        boolean do
          must { string params[:search][:value].downcase, {default_operator: "AND", analyzer: 'whitespace', fields: ['title', 'formats.value', 'formats.format_aliases.value', 'contributors.first_name', 'contributors.last_name']} } if params[:search][:value].present?
          must_not { term :state, 'destroyed'}
          must {term :state, params[:state][:value] } if(params[:state][:value] != 'all')

          must do
            nested path: 'formats' do
              query do
                boolean do
                  must {term 'formats.nature', params[:price][:nature][:value]} if params[:price][:nature][:value] != 'both'
                  must do
                    nested path: 'costs' do
                      query do
                        boolean do
                          must { term 'formats.costs.country', params[:price][:country][:value]}
                          amount = params[:price][:amount][:value].sub(',','').sub('.','')
                          if params[:price][:operator][:value] == '='
                            must {term 'formats.costs.amount', amount}
                          elsif params[:price][:operator][:value] == '>'
                            must {range 'formats.costs.amount', {gte: amount}}
                          else
                            must {range 'formats.costs.amount', {lte: amount}}
                          end
                        end
                      end
                    end
                  end
                end
              end
            end
          end if params.has_key?(:price) && params[:price].has_key?(:amount) && params[:price][:amount][:value] != ''


        end
      end
      filter :nested, {path: 'formats'}.merge({query: nested_price_filter.to_hash}) if(params.has_key?(:price) && params[:price].has_key?(:amount) && params[:price][:amount][:value] != '')
    end
  end
end

我无法弄清楚出了什么问题,有人能帮帮我吗?

有些格式可能没有价格。

感谢您的帮助

0 个答案:

没有答案