2

我对弹性搜索和轮胎很陌生。

我需要搜索嵌套在格式中的成本中的金额,该格式嵌套在出版物中。这是映射:

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

我不知道出了什么问题,有人可以帮助我吗?

某些格式可能没有价格。

谢谢你的帮助

4

0 回答 0