<commit_before>#
# Copyright 2011 Red Hat, 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.
#

module Infinispan

  class Search

    def initialize(cache, deserializer)
      @cache          = cache
      @deserializer   = deserializer
      @search_manager = cache.search_manager
    end

    def search( query )
      cache_query = search_manager.get_query( build_query( query ), query.model.java_class )
      cache_query.list.collect { |record| deserialize(record) }
    end

    private
    def build_query( query )
      puts ">>>>>>>>>>>>>>>>>>> Building query start"
      builder = search_manager.build_query_builder_for_class( query.model.java_class ).get
      query = query.conditions.nil? ? builder.all.create_query : handle_condition( builder, query.conditions.first ) 
      puts "LUCENE QUERY: #{query.to_s}"
      puts ">>>>>>>>>>>>>>>>>>> Building query end"
      query
    end

    def handle_condition( builder, condition )
      puts "CONDITION: #{condition.inspect}"
      puts "CONDITION CLASS: #{condition.class}"
      puts "CONDITION OPERANDS: #{condition.operands.inspect}" if condition.respond_to? :operands
      #puts "CONDITION VALUE: #{condition.value}"
      #puts "CONDITION SUBJECT: #{condition.subject.name}"
      if condition.class == DataMapper::Query::Conditions::NotOperation
        handle_not_operation( builder, condition )
      elsif condition.class == DataMapper::Query::Conditions::EqualToComparison
        handle_equal_to( builder, condition ) 
      elsif condition.class == DataMapper::Query::Conditions::InclusionComparison
        handle_inclusion( builder, condition )
      elsif condition.class == DataMapper::Query::Conditions::RegexpComparison
        handle_regex( builder, condition )
      else
        builder.all.create_query
      end
    end

    def handle_regex( builder, condition )
      field = condition.subject.name
      # TODO Figure out how hibernate search/lucene deal with regexp
      value = condition.value.nil? ? "?*" : "*" + condition.value.source.gsub('/','') + "*"
      builder.keyword.wildcard.on_field(field).matching(value).create_query
    end

    def handle_inclusion( builder, condition )
      match = condition.value.collect { |v| v }.join(' ')
      builder.keyword.on_field( condition.subject.name ). matching( match ).create_query
    end

    def handle_equal_to( builder, condition )
      field = condition.subject.name
      value = condition.value.nil? ? "?*" : condition.value.to_s
      if !value.nil? && (value.include?( '?' ) || value.include?( '*' ))
        builder.keyword.wildcard.on_field(field).matching(value).create_query
      else
        builder.keyword.on_field(field).matching(value).create_query
      end
    end

    def handle_not_operation( builder, operation )
      condition = operation.operands.first
      if (condition.class == DataMapper::Query::Conditions::EqualToComparison && condition.value.nil?) 
        # not nil means everything
        everything = DataMapper::Query::Conditions::EqualToComparison.new( condition.subject, '*' )
        handle_condition( builder, everything )
      elsif (condition.class == DataMapper::Query::Conditions::InclusionComparison && condition.value == [])
        builder.all.create_query
      else
        builder.bool.must( handle_condition( builder, condition ) ).not.create_query
      end
    end

    def cache
      @cache
    end

    def search_manager
      @search_manager
    end

    def deserialize(value)
      @deserializer.call(value)
    end

  end
end


<commit_msg>Add ranges, arrays and boolean OR to dm-infinispan-adapter searches.<commit_after>#
# Copyright 2011 Red Hat, 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.
#

module Infinispan

  class Search

    def initialize(cache, deserializer)
      @cache          = cache
      @deserializer   = deserializer
      @search_manager = cache.search_manager
    end

    def search( query )
      cache_query = search_manager.get_query( build_query( query ), query.model.java_class )
      cache_query.list.collect { |record| deserialize(record) }
    end

    private
    def build_query( query )
      puts ">>>>>>>>>>>>>>>>>>> Building query start"
      builder = search_manager.build_query_builder_for_class( query.model.java_class ).get
      query = query.conditions.nil? ? builder.all.create_query : handle_condition( builder, query.conditions.first ) 
      puts "LUCENE QUERY: #{query.to_s}"
      puts ">>>>>>>>>>>>>>>>>>> Building query end"
      query
    end

    def handle_condition( builder, condition )
      puts "CONDITION: #{condition.inspect} <<<>>> #{condition}"
      puts "CONDITION CLASS: #{condition.class}"
      puts "CONDITION OPERANDS: #{condition.operands.inspect}" if condition.respond_to? :operands
      #puts "CONDITION VALUE: #{condition.value}"
      #puts "CONDITION SUBJECT: #{condition.subject.name}"
      if condition.class == DataMapper::Query::Conditions::OrOperation
        terms = condition.operands.each do |op|
          puts "OR OPERAND: #{op.inspect}"
          #builder.bool.should( handle_condition builder, op )
          builder.bool.should( handle_condition( builder, op ) )
        end
        builder.all.create_query
      elsif condition.class == DataMapper::Query::Conditions::NotOperation
        handle_not_operation( builder, condition )
      elsif condition.class == DataMapper::Query::Conditions::EqualToComparison
        handle_equal_to( builder, condition ) 
      elsif condition.class == DataMapper::Query::Conditions::InclusionComparison
        handle_inclusion( builder, condition )
      elsif condition.class == DataMapper::Query::Conditions::RegexpComparison
        handle_regex( builder, condition )
      else
        builder.all.create_query
      end
    end

    def handle_regex( builder, condition )
      field = condition.subject.name
      # TODO Figure out how hibernate search/lucene deal with regexp
      value = condition.value.nil? ? "?*" : "*" + condition.value.source.gsub('/','') + "*"
      builder.keyword.wildcard.on_field(field).matching(value).create_query
    end

    def handle_inclusion( builder, condition )
      puts "RANGE: #{condition.value.class} #{condition.value}"
      if condition.value.is_a? Range
        rng = builder.range.on_field(condition.subject.name).from(condition.value.begin).to(condition.value.end)
        condition.value.exclude_end? ? rng.exclude_limit.create_query : rng.create_query 
      else # an Array
        match = condition.value.collect { |v| v }.join(' ')
        if match.empty?
          # we should find nothing
          builder.bool.must( builder.all.create_query ).not.create_query
        else
          builder.keyword.on_field( condition.subject.name ).matching( match ).create_query 
        end
      end
    end

    def handle_equal_to( builder, condition )
      field = condition.subject.name
      value = condition.value.nil? ? "?*" : condition.value.to_s
      if !value.nil? && (value.include?( '?' ) || value.include?( '*' ))
        builder.keyword.wildcard.on_field(field).matching(value).create_query
      else
        builder.keyword.on_field(field).matching(value).create_query
      end
    end

    def handle_not_operation( builder, operation )
      condition = operation.operands.first
      if (condition.class == DataMapper::Query::Conditions::EqualToComparison && condition.value.nil?) 
        # not nil means everything
        everything = DataMapper::Query::Conditions::EqualToComparison.new( condition.subject, '*' )
        handle_condition( builder, everything )
      else
        builder.bool.must( handle_condition( builder, condition ) ).not.create_query
      end
    end

    def cache
      @cache
    end

    def search_manager
      @search_manager
    end

    def deserialize(value)
      @deserializer.call(value)
    end

  end
end



{'fraction_non_alphanumeric': 0.1094488188976378, 'fraction_numerical': 0.0017997750281214847, 'mean_word_length': 3.1800658204043253, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7544742', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2444, 'n_words': 636}