Class: Khoj::Client

Inherits:
Object
  • Object
show all
Includes:
Index
Defined in:
lib/khoj/client.rb

Constant Summary collapse

DEFAULT_DOC_TYPE =
'default'
DEFAULT_DOC_FIELD =
'text'
DEFAULT_SEARCH_LIMIT =
10
DEFAULT_ORDER =
'asc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Index

#create_index, #delete_index, #index?, #index_stats

Constructor Details

#initialize(index) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
# File 'lib/khoj/client.rb', line 16

def initialize(index)
  @index = index
  @_index = "#{Configuration.api_key}-#{index}"
  @conn = Configuration.connection
end

Instance Attribute Details

#_indexObject (readonly)

Returns the value of attribute _index.



11
12
13
# File 'lib/khoj/client.rb', line 11

def _index
  @_index
end

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/khoj/client.rb', line 10

def index
  @index
end

Instance Method Details

#add(resource_id, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/khoj/client.rb', line 27

def add(resource_id, options = {})
  resource_name, id = resource_type(resource_id)
  resource = if options.class == String
               return if options.strip.empty?
               { DEFAULT_DOC_FIELD => options}
             else
               return if options.empty?
               options
             end

  req_options = {:body => resource.to_json, :format => 'json'}
  response = @conn.post("/#{_index}/#{resource_name}/#{id}", req_options)

  case response.code
  when 200
    true
  when 201
    true
  else
    raise KhojException.new(response.parsed_response)
  end
end

#delete(resource_id) ⇒ Object



50
51
52
53
54
55
# File 'lib/khoj/client.rb', line 50

def delete(resource_id)
  id, resource_name =  resource_id.to_s.split(':').reverse
  resource_type =  resource_name == nil ? [id] : [resource_name || DEFAULT_DOC_TYPE, id]
  response = @conn.delete("/#{_index}/#{resource_type(resource_id).join('/')}")
  response.code == 200
end

#get(resource_id, options = {}) ⇒ Object



22
23
24
25
# File 'lib/khoj/client.rb', line 22

def get(resource_id, options = {})
  response = @conn.get("/#{_index}/#{resource_type(resource_id).join('/')}")
  response.parsed_response
end

#search(query, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/khoj/client.rb', line 57

def search(query, options = {})
  search_uri = options[:type] ? "#{options[:type]}/_search?pretty=true" : '_search?pretty=true'
  
  # check that if search string contains AND, OR or NOT in query
  # if it is then we will execute String query of Query DSL
  # else we will execute normal search query
  if query.scan(/\sAND|NOT|OR+\s/).empty?
    
    options[:field] ||= DEFAULT_DOC_FIELD
    q = {:query => 
      {:term => { options[:field] => query}}
    }
    
    #q[:query][:fields] ||= ['_id' , '_type']
    q[:size] ||= (options[:size] ? (options[:size] > 10 ? DEFAULT_SEARCH_LIMIT : options[:size]) : DEFAULT_SEARCH_LIMIT) if options[:size]
    q[:query] = q[:query].merge(:script_fields => { "#{options[:fetch]}" => { :script => "_source.#{options[:fetch]}"}}) if options[:fetch]
  else
    
    # TODO : implement functionality for fetch action if specified by user to fetch fields from result set
    q = get_string_query(query, options)
  end
  
  if category_filter = options[:category_filter]
    q[:facets] = {}
    q[:filter] = {:term => {}}
    category_filter.keys.each do |key|
      q[:facets][key.to_s] = {:terms => {:field => key.to_s}, :facet_filter => {:term => {key => category_filter[key]}}}
      q[:filter][:term].merge!({key => category_filter[key]}) 
    end
  end

  #Check if sorting function is specified in the query.
  #index.search 'test', :function => {:cordinates => "00,00", :order => 'desc'}
  # "sort" => {:geo_location => {:cordinates => ["xx,yy"]}, :fields => {:price => 'xxx', :tags => 'xxx'}} 
  if sort = options[:sort]
    q["sort"] = []
    sort.keys.each do |key|
      if  key == :geo_location
        q["sort"] << { "_geo_distance" =>  { "location" => "#{sort[:geo_location][:cordinates]}", "order" => "#{sort[:geo_location][:order] || DEFAULT_ORDER}", "unit" => "km" }}
      else
        q["sort"] << sort[:fields]
      end
    end
  end
  
  response = @conn.get("/#{_index}/#{search_uri}", :body => q.to_json)

  case response.code
  when 200
    response.parsed_response
  else
    nil
  end
end