Class: Khoj::Client
Constant Summary collapse
- DEFAULT_DOC_TYPE =
'default'
- DEFAULT_DOC_FIELD =
'text'
- DEFAULT_SEARCH_LIMIT =
10
- DEFAULT_ORDER =
'asc'
Instance Attribute Summary collapse
-
#_index ⇒ Object
readonly
Returns the value of attribute _index.
-
#index ⇒ Object
Returns the value of attribute index.
Instance Method Summary collapse
- #add(resource_id, options = {}) ⇒ Object
- #delete(resource_id) ⇒ Object
- #get(resource_id, options = {}) ⇒ Object
-
#initialize(index) ⇒ Client
constructor
A new instance of Client.
- #search(query, options = {}) ⇒ Object
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
#_index ⇒ Object (readonly)
Returns the value of attribute _index.
11 12 13 |
# File 'lib/khoj/client.rb', line 11 def _index @_index end |
#index ⇒ Object
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, = {}) resource_name, id = resource_type(resource_id) resource = if .class == String return if .strip.empty? { DEFAULT_DOC_FIELD => } else return if .empty? end = {:body => resource.to_json, :format => 'json'} response = @conn.post("/#{_index}/#{resource_name}/#{id}", ) 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, = {}) 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, = {}) search_uri = [:type] ? "#{[: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? [:field] ||= DEFAULT_DOC_FIELD q = {:query => {:term => { [:field] => query}} } #q[:query][:fields] ||= ['_id' , '_type'] q[:size] ||= ([:size] ? ([:size] > 10 ? DEFAULT_SEARCH_LIMIT : [:size]) : DEFAULT_SEARCH_LIMIT) if [:size] q[:query] = q[:query].merge(:script_fields => { "#{[:fetch]}" => { :script => "_source.#{[:fetch]}"}}) if [:fetch] else # TODO : implement functionality for fetch action if specified by user to fetch fields from result set q = get_string_query(query, ) end if category_filter = [: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 = [: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 |