Commit 8fb8f2338309e4b38924a3399585fe570e624004

Authored by Andrew
1 parent c5910164

Added build_query method [skip ci]

Showing 1 changed file with 33 additions and 29 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -401,14 +401,6 @@ module Searchkick
401 401 # post filters
402 402 set_post_filters(payload, post_filters) if post_filters.any?
403 403  
404   - if filters.any? || must_not.any? || should.any?
405   - bool = {must: query}
406   - bool[:filter] = filters if filters.any? # where
407   - bool[:must_not] = must_not if must_not.any? # exclude
408   - bool[:should] = should if should.any? # conversions
409   - query = {bool: bool}
410   - end
411   -
412 404 custom_filters = []
413 405 multiply_filters = []
414 406  
... ... @@ -416,27 +408,7 @@ module Searchkick
416 408 set_boost_where(custom_filters)
417 409 set_boost_by_distance(custom_filters) if options[:boost_by_distance]
418 410  
419   - if custom_filters.any?
420   - query = {
421   - function_score: {
422   - functions: custom_filters,
423   - query: query,
424   - score_mode: "sum"
425   - }
426   - }
427   - end
428   -
429   - if multiply_filters.any?
430   - query = {
431   - function_score: {
432   - functions: multiply_filters,
433   - query: query,
434   - score_mode: "multiply"
435   - }
436   - }
437   - end
438   -
439   - payload[:query] = query
  411 + payload[:query] = build_query(query, filters, should, must_not, custom_filters, multiply_filters)
440 412  
441 413 payload[:explain] = options[:explain] if options[:explain]
442 414 payload[:profile] = options[:profile] if options[:profile]
... ... @@ -516,6 +488,38 @@ module Searchkick
516 488 [boost_fields, fields]
517 489 end
518 490  
  491 + def build_query(query, filters, should, must_not, custom_filters, multiply_filters)
  492 + if filters.any? || must_not.any? || should.any?
  493 + bool = {must: query}
  494 + bool[:filter] = filters if filters.any? # where
  495 + bool[:must_not] = must_not if must_not.any? # exclude
  496 + bool[:should] = should if should.any? # conversions
  497 + query = {bool: bool}
  498 + end
  499 +
  500 + if custom_filters.any?
  501 + query = {
  502 + function_score: {
  503 + functions: custom_filters,
  504 + query: query,
  505 + score_mode: "sum"
  506 + }
  507 + }
  508 + end
  509 +
  510 + if multiply_filters.any?
  511 + query = {
  512 + function_score: {
  513 + functions: multiply_filters,
  514 + query: query,
  515 + score_mode: "multiply"
  516 + }
  517 + }
  518 + end
  519 +
  520 + query
  521 + end
  522 +
519 523 def set_conversions
520 524 conversions_fields = Array(options[:conversions] || searchkick_options[:conversions]).map(&:to_s)
521 525 if conversions_fields.present? && options[:conversions] != false
... ...