簡體   English   中英

Elasticsearch中帶有查詢的嵌套過濾器

[英]Nested filter with query in elasticsearch

我有嵌套過濾器查詢的問題。 這是我所擁有的:

  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 文檔 ,據我了解它應該可以工作。 我還查看了Igor Motov的要點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:

我使用的是Elasticsearch 1.1.0版

從外觀上看,您的映射將reservation定義為類型object ,其nested設置為true

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

您真正需要的是設置type => "nested" ,因為沒有nested屬性。 在設置映射之前,請確保轉儲索引並再次嘗試。

請參考嵌套的映射文檔以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM