【问题标题】:Nested filter with query in elasticsearchelasticsearch中带有查询的嵌套过滤器
【发布时间】:2014-06-16 15:31:21
【问题描述】:

我在使用嵌套过滤器的查询时遇到问题。这是我所拥有的:

  def search_with_filters(reservation_ids, query)
    search query: {
      filtered: {
        query: {
          query_string: {
            query: query
          }
        },
        filter: {
          nested: {
            path: 'reservation',
            query: {
              bool: {
                must: {
                  terms: {
                    'reservation.id' => reservation_ids
                  }
                }
              }
            }
          }
        }
      }
    }
  end

我已经检查了 elasticsearch documentation 并且据我所知它应该可以工作。我还查看了Igor Motov's gist(elasticsearch 贡献者),他在其中解释了解决方案,但它也对我不起作用。其他文章信息量较少,因此我不会在这里引用它们。

这是我的模型的映射:

pry(main)> EmployeeReservation.mappings
=> #<Elasticsearch::Model::Indexing::Mappings:0x0000010fb120a0
 @mapping=
  {:user=>{:type=>"object", :properties=>{:name=>{:type=>"string"}}},
   :reservation=>
    {:nested=>true,
     :type=>"object",
     :properties=>
      {:time_start=>{:type=>"string"},
       :service_location_lane=>
        {:type=>"object", :properties=>{:name=>{:type=>"string"}}}}}},
 @options={},
 @type="employee_reservation">

这是请求的结果查询:

pry(main)> EmployeeReservation.search_with_filters([1,2], '*Yury*').records
=> #<Elasticsearch::Model::Response::Records:0x00000103ba24b8
 @klass=
  [PROXY] EmployeeReservation(id: integer, reservation_id: integer, amount: decimal, created_at: datetime, updated_at: datetime, user_id: integer, rate: integer, duration: integer),
 @response=
  #<Elasticsearch::Model::Response::Response:0x00000103ba2508
   @klass=
    [PROXY] EmployeeReservation(id: integer, reservation_id: integer, amount: decimal, created_at: datetime, updated_at: datetime, user_id: integer, rate: integer, duration: integer),
   @records=#<Elasticsearch::Model::Response::Records:0x00000103ba24b8 ...>,
   @search=
    #<Elasticsearch::Model::Searching::SearchRequest:0x00000103ba27b0
     @definition=
      {:index=>"employee_reservations",
       :type=>"employee_reservation",
       :body=>
        {:query=>
          {:filtered=>
            {:query=>{:query_string=>{:query=>"*Yury*"}},
             :filter=>
              {:nested=>
                {:path=>"reservation",
                 :query=>
                  {:bool=>{:must=>{:terms=>{"reservation.id"=>[1, 2]}}}}}}}}}},
     @klass=
      [PROXY] EmployeeReservation(id: integer, reservation_id: integer, amount: decimal, created_at: datetime, updated_at: datetime, user_id: integer, rate: integer, duration: integer)>>>

这是错误:

EmployeeReservation Search (6.1ms) {index: "employee_reservations", type: "employee_reservation", body: {query: {filtered: {query: {query_string: {query: "*Yury*"}}, filter: {nested: {path: "reservation", query: {bool: {must: {terms: {"reservation.id"=>[1, 2]}}}}}}}}}}
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[km2oNhrpTc-oXLPo-x1B-g][employee_reservations][0]: SearchParseException[[employee_reservations][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"filtered\":{\"query\":{\"query_string\":{\"query\":\"*Yury*\"}},\"filter\":{\"nested\":{\"path\":\"reservation\",\"query\":{\"bool\":{\"must\":{\"terms\":{\"reservation.id\":[1,2]}}}}}}}}}]]]; nested: QueryParsingException[[employee_reservations] [nested] nested object under path [reservation] is not of nested type]; }]","status":400}
from /Users/FUT/.rvm/gems/ruby-2.0.0-p353@car_service/gems/elasticsearch-transport-1.0.1/lib/elasticsearch/transport/transport/base.rb:132:in `__raise_transport_error'

你能帮忙吗?

编辑 1:

我使用弹性搜索 1.1.0 版

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch full-text-search


    【解决方案1】:

    看起来,您的映射将reservation 定义为object 类型,nested 设置为true

    EmployeeReservation.mappings
       […]
       :reservation=>
        {:nested=>true,
         :type=>"object",
         […]
    

    您真正需要的是设置type =&gt; "nested",因为没有nested 属性。 确保在设置映射之前转储您的索引,然后再试一次。

    请参阅nested mapping docs 了解更多信息。

    【讨论】:

    • 谢谢!现在工作得很好。
    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多