<commit_before>module Georgia
  class PageSearch

    attr_reader :params, :definition, :query, :sort_column, :sort_direction

    def initialize params
      @params = params
      @query = params.fetch(:query, nil)
      @sort_column = params.fetch(:sort, :updated_at)
      @sort_direction = params.fetch(:direction, 'desc')
      @definition = default_definition
      process
    end

    def process
      if query.present?
        include_filtered_query
        add_fulltext_query
        # add_suggestion
        # add_status_facet_filter if status.present?
      # elsif status.present?
        # include_filtered_query
        # add_status_facet_filter
      else
        add_match_all
      end
      # add_sorting if sort_column.present?
    end

    private

    def add_fulltext_query
      add_to_query_definition({multi_match: { query: query, fields: ['title^5', 'text'] }})
    end

    def add_match_all
      @definition[:query] = { match_all: {} }
      # @definition[:sort] = { sort_column => sort_direction }
    end

    def add_sorting
      @definition[:sort] = { sort_column => sort_direction }
      @definition[:track_scores] = true
    end

    def add_suggestion
      @definition[:suggest] = {
        text: query,
        suggest_title: {
          term: {
            field: 'title.tokenized',
            suggest_mode: 'always'
          }
        },
        suggest_body: {
          term: {
            field: 'text.tokenized',
            suggest_mode: 'always'
          }
        }
      }
    end

    def add_status_facet_filter
      add_to_query_definition({ term: {status: status} })
      @definition[:facets][:status][:facet_filter] = {term: {status: status}}
    end

    def add_to_query_definition conditions
      @definition[:query][:filtered][:query][:bool][:must] << conditions
    end

    def default_definition
      {
        query: { }
      }
    end

    def include_filtered_query
      @definition[:query] = {filtered: {query: {bool: {must: []}}}}
    end

  end
end<commit_msg>Common SearchDefinition parent for search definition models<commit_after>module Georgia
  class PageSearch < SearchDefinition

  end
end
{'fraction_non_alphanumeric': 0.09880514705882353, 'fraction_numerical': 0.00045955882352941176, 'mean_word_length': 2.406885758998435, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7133740', 'n_tokens_mistral': 654, 'n_tokens_neox': 630, 'n_words': 136}