<commit_before># Copyright 2014 Square Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

class SearchController < ApplicationController
  # The number of records to return by default.
  PER_PAGE = 50

  before_filter :authenticate_user!

  def translations
    respond_to do |format|
      format.html # translations.html.rb

      format.json do
        offset        = params[:offset].to_i
        limit         = params.fetch(:limit, PER_PAGE)
        project_id    = params[:project_id].to_i
        query_filter  = params[:query]
        field         = params[:field]
        translator_id = params[:translator_id].to_i
        if params[:target_locales].present?
          target_locales = params[:target_locales].split(',').map { |l| Locale.from_rfc5646(l.strip) }
          return head(:unprocessable_entity) unless target_locales.all?
        end
        @results = Translation.search(load: {include: {key: :project}}) do
          if target_locales
            if target_locales.size > 1
              locale_filters = target_locales.map do |locale|
                {term: {rfc5646_locale: locale.rfc5646}}
              end
              filter :or, *locale_filters
            else
              filter :term, rfc5646_locale: target_locales.map(&:rfc5646)[0]
            end
          end
          size limit
          from offset

          if project_id and project_id > 0
            filter :term, project_id: project_id
          end
          if translator_id and translator_id > 0
            filter :term, translator_id: translator_id
          end

          if query_filter.present?
            case field
              when 'searchable_source_copy' then
                query { match 'source_copy', query_filter, operator: 'and' }
              else
                query { match 'copy', query_filter, operator: 'and' }
            end
          end
        end
        render json: decorate_translations(@results).to_json
      end
    end
  end

  def keys
    respond_to do |format|
      format.html # keys.html.erb
      format.json do
        return head(:unprocessable_entity) if params[:project_id].to_i <= 0

        if params[:metadata] # return metadata about the search only
          @project = Project.find(params[:project_id])
          render json: {
                           locales: ([@project.base_locale] + @project.targeted_locales.sort_by(&:rfc5646)).uniq
                       }.to_json
        else
          query_filter = params[:filter]
          status       = params[:status]
          offset       = params[:offset].to_i
          id           = params[:project_id]
          limit        = params.fetch(:limit, PER_PAGE)
          not_elastic  = params[:not_elastic_search] 

          if query_filter.present?
            @results = Key.search(load: {include: [:translations, :project]}) do
              if not_elastic
                filter :term, original_key_exact: query_filter                  
              else 
                query { string "original_key:\"#{query_filter}\"" }
              end 
              filter :term, project_id: id

              unless status.blank?
                filter :term, ready: status
              end 
              
              from offset
              size limit
              sort { by :original_key, 'asc' }
            end
          else
            @results = []
          end
          render json: decorate_keys(@results).to_json
        end
      end
    end
  end

  def commits
    respond_to do |format|<commit_msg>Improves key search as well<commit_after># Copyright 2014 Square Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

class SearchController < ApplicationController
  # The number of records to return by default.
  PER_PAGE = 50

  before_filter :authenticate_user!

  def translations
    respond_to do |format|
      format.html # translations.html.rb

      format.json do
        offset        = params[:offset].to_i
        limit         = params.fetch(:limit, PER_PAGE)
        project_id    = params[:project_id].to_i
        query_filter  = params[:query]
        field         = params[:field]
        translator_id = params[:translator_id].to_i
        if params[:target_locales].present?
          target_locales = params[:target_locales].split(',').map { |l| Locale.from_rfc5646(l.strip) }
          return head(:unprocessable_entity) unless target_locales.all?
        end
        @results = Translation.search(load: {include: {key: :project}}) do
          if target_locales
            if target_locales.size > 1
              locale_filters = target_locales.map do |locale|
                {term: {rfc5646_locale: locale.rfc5646}}
              end
              filter :or, *locale_filters
            else
              filter :term, rfc5646_locale: target_locales.map(&:rfc5646)[0]
            end
          end
          size limit
          from offset

          if project_id and project_id > 0
            filter :term, project_id: project_id
          end
          if translator_id and translator_id > 0
            filter :term, translator_id: translator_id
          end

          if query_filter.present?
            case field
              when 'searchable_source_copy' then
                query { match 'source_copy', query_filter, operator: 'and' }
              else
                query { match 'copy', query_filter, operator: 'and' }
            end
          else
            sort { by :id, 'desc' }
          end
        end
        render json: decorate_translations(@results).to_json
      end
    end
  end

  def keys
    respond_to do |format|
      format.html # keys.html.erb
      format.json do
        return head(:unprocessable_entity) if params[:project_id].to_i <= 0

        if params[:metadata] # return metadata about the search only
          @project = Project.find(params[:project_id])
          render json: {
                           locales: ([@project.base_locale] + @project.targeted_locales.sort_by(&:rfc5646)).uniq
                       }.to_json
        else
          query_filter = params[:filter]
          status       = params[:status]
          offset       = params[:offset].to_i
          id           = params[:project_id]
          limit        = params.fetch(:limit, PER_PAGE)
          not_elastic  = params[:not_elastic_search] 

          @results = Key.search(load: {include: [:translations, :project]}) do
            if query_filter.present?
              if not_elastic
                filter :term, original_key_exact: query_filter
              else
                query { match 'original_key', query_filter, operator: 'and' }
              end
            else
              sort { by :original_key, 'asc' }
            end
            filter :term, project_id: id

            unless status.blank?
              filter :term, ready: status
            end

            from offset
            size limit
          end
          render json: decorate_keys(@results).to_json
        end
      end
    end
  end

  def commits
    respond_to do |format|
{'fraction_non_alphanumeric': 0.0778023598820059, 'fraction_numerical': 0.009587020648967551, 'mean_word_length': 1.8087676907145323, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1670256', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2128, 'n_words': 632}