2016-06-08 3 views
1

내가 (무게 일부 필드에 필요레일 및 Elasticsearch - 필터, 무게, 및 통계

class MyModel # `Employee` used in concrete examples 
    field :field1 # company_name used in concrete examples 
    field :field2 # job_name used in concrete examples 
    scope: filter1 # non_retired 
    scope: filter2 
end 
  • 내가 non_retired
  • 에 의해 명명 검색 예 : 쿼리, 필터/필터 할 필요가 다음과 같은 모델을 고려 예를 들어, 내가 이미 코드가 작동이 (필자는 company_names을 집계, 예를 들어 결과 (예. 단지 처음 10 개 페이지를 매긴 결과)를 기반으로 통계를 얻을 필요가 일부 필드에 3 배 더 중요성)
  • 을 제공 ,하지만 "총"결과 ID를 얻는 데 문제가 있습니다.

모든 것을 함께 정리하는 데 어려움이 있습니다. Rails elasticsearch - named scope search에서 나는 Elasticsearch에 직접 ID를 공급해야한다는 것을 이해했습니다. { "ROOT_CAUSE": [{ "타입": "query_parsing_exception", "원인"

def search_by_id(query, type, ids, options = {}) 
    self.weighted_search(query, options.deep_merge({ 
    query: { 
     multi_match: { 
     filter: { 
      ids: { 
      values: ids 
      }.tap do |filter| 
      filter[:type] = type if type 
     end 
     } 
     } 
    } 
    })) 
end 

def weighted_search(query, options = {}) 
    self.__elasticsearch__.search(
    { 
     query: { 
     multi_match: { 
      query: query, 
      fields: [ 
      "company_name^3", 
      "job_name^2", 
      ], 
      # strategy: 'leap_frog_filter_first' 
      # PROBLEM :cannot use this strategy on multi_match ? 
     } 
     } 
    }.deep_merge(options) 
) 
end 

[400] { "오류"는 이하의 설명과 함께 BadRequest 에러를 발생한다 : "[경기] 쿼리는 지원하지 않습니다 나는이 오류를 이해하지 못한다 [$ OID]

... 내가 IDS에 의해 필터링 할 수 없습니다 ??

그런 다음이 작업이 있다고 가정하고 ElasticSearch와 일치하는 ID의 하위 집합을 어떻게 추출합니까? 내가 (예. 회사 이름을 집계 통계를 수행 할 수 있도록

search = MyModel.search('query') 
search.total_results # => 81 
search.records.count # => 10 

는하지만, 이미 일부 코드 작업이 모두 81 개 ID를 얻기 위해 필요하지만, 지금은 첫 번째 10 개 개의 결과가 집계 얻을 ...)

답변

0

좋아, 여기 내 현재의 솔루션은 내가 그들의 IDS는 DB를 많이 안타 있기 때문에 기록을 필터링)

  • 만 사용하여 MongoDB라는 이름의 범위를 좋아하고 따려고하지 않는 것이 (이다

    ,210
  • def search_by_id(query, type, ids, options = {}) 
        self.weighted_search(query, options.deep_merge({ 
        query: { 
         filtered: { 
         filter: { 
          ids: { 
          values: ids.map(&:to_s) 
          }.tap do |filter| 
          filter[:type] = type if type 
         end 
         } 
         } 
        } 
        })) 
    end 
    
    def weighted_search(query, options = {}) 
        self.__elasticsearch__.search(
        { 
         query: { 
         filtered: { 
          query: { 
          multi_match: { 
           query: query, 
           fields: [ 
           "company_name^3", 
           "job_name^2", 
           "xxx" 
           ], 
           type: "most_fields" 
          } 
          } 
         } 
         } 
    }.deep_merge(options) 
    ) 
    end 
    
  • 제공하는 ID의 목록과 MongoDB를 데이터베이스에 (등 집계) 추가 정보 추출을 수행 ES에 결과 ID를 전송하고, 특정 필드에 부스트와 득점 검색을 수행 Elasticsearch에 의해

    @response = @searching.search_by_id(@query, @searchable_type, ids).per(10000) # setting a high per number so ES returns all IDs 
    @all_records = @response.records.records 
    
    # Further processing 
    @all_records.map_reduce(...)