<commit_before>require "elasticsearch/escaping"

module Elasticsearch
  class SearchQueryBuilder
    include Elasticsearch::Escaping

    QUERY_ANALYZER = "query_default"

    def initialize(query, options={})
      @query = query
      @options = options
    end

    def query_hash
      must_conditions = [
        {
          query_string: {
            fields: match_fields.map { |name, boost|
              boost == 1 ? name : "#{name}^#{boost}"
            },
            query: escape(@query),
            analyzer: QUERY_ANALYZER
          }
        }
      ]
      if @options[:minimum_should_match]
        must_conditions[0][:query_string][:minimum_should_match] = @options[:minimum_should_match]
      end
      if @options[:organisation]
        must_conditions << {
          term: {
            organisations: @options[:organisation]
          }
        }
      end
      {
        from: 0, size: 50,
        query: {
          custom_filters_score: {
            query: {
              bool: {
                must: must_conditions,
                should: shingle_boosts
              }<commit_msg>Allow limit to be passed to query builder<commit_after>require "elasticsearch/escaping"

module Elasticsearch
  class SearchQueryBuilder
    include Elasticsearch::Escaping

    QUERY_ANALYZER = "query_default"

    def initialize(query, options={})
      @query = query
      @options = default_options.merge(options)
    end

    def default_options
      {
        limit: 50
      }
    end

    def query_hash
      must_conditions = [
        {
          query_string: {
            fields: match_fields.map { |name, boost|
              boost == 1 ? name : "#{name}^#{boost}"
            },
            query: escape(@query),
            analyzer: QUERY_ANALYZER
          }
        }
      ]
      if @options[:minimum_should_match]
        must_conditions[0][:query_string][:minimum_should_match] = @options[:minimum_should_match]
      end
      if @options[:organisation]
        must_conditions << {
          term: {
            organisations: @options[:organisation]
          }
        }
      end
      {
        from: 0, size: @options[:limit],
        query: {
          custom_filters_score: {
            query: {
              bool: {
                must: must_conditions,
                should: shingle_boosts
              }
{'fraction_non_alphanumeric': 0.09390862944162437, 'fraction_numerical': 0.004230118443316413, 'mean_word_length': 1.6844494892167992, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3941636', 'n_tokens_mistral': 677, 'n_tokens_neox': 648, 'n_words': 128}