Class: ChinoRuby::Search

Inherits:
ChinoBaseAPI show all
Defined in:
lib/chino_ruby/classes.rb

Instance Method Summary collapse

Methods inherited from ChinoBaseAPI

#delete_resource, #get_resource, #initialize, #parse_response, #patch_resource, #post_resource, #post_resource_with_string_result, #put_resource, #return_uri

Methods inherited from CheckValues

#check_boolean, #check_int, #check_json, #check_string

Constructor Details

This class inherits a constructor from ChinoRuby::ChinoBaseAPI

Instance Method Details

#complex_search_documents(schema_id, result_type, sort, query, limit = nil, offset = nil) ⇒ Object

The new Search API accept a query hash, documented here: console.test.chino.io/docs/v1#header-search-query query hash need to be converted to json e.g. to build this JSON query {

"and": [
    {
        "field": "user_id",
        "type": "eq",
        "value": "aaaaaaa"
    },
    {
        "or": [
            {
                "field": "first_name",
                "type": "like",
                "value": "*Mario*"
            },
            {
                "field": "last_name",
                "type": "like",
                "value": "*Mario*"
            }
        ]
    }
]

}

You can use code like this:

query = { and: [] } query << ChinoFilterOption.new(‘user_id’, ‘eq’, “aaaaaaa”)

or_conditions = { or: [] } or_conditions << ChinoFilterOption.new(‘first_name’, ‘like’, “Mario”) or_conditions << ChinoFilterOption.new(‘last_name’, ‘like’, “Mario”) query << or_conditions



1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
# File 'lib/chino_ruby/classes.rb', line 1395

def complex_search_documents(schema_id, result_type, sort, query, limit=nil, offset=nil)
  check_string(schema_id)
  check_string(result_type)
  check_json(sort)
  check_json(query)
  data = { result_type: result_type, query: query, sort: sort}.to_json

  docs = GetDocumentsResponse.new
  docs.from_json(post_resource("/search/documents/#{schema_id}", data, limit || ChinoRuby::QUERY_DEFAULT_LIMIT, offset || 0).to_json)

  case result_type
  when 'ONLY_ID'
    docs.IDs = docs.IDs.map { |id| id }

  when 'FULL_CONTENT', 'NO_CONTENT'
    docs.documents = docs.documents.map do |d|
      doc = Document.new
      doc.from_json(d.to_json)
    end
  end

  docs
end

#search_documents(schema_id, result_type, filter_type, sort, filter, limit = nil, offset = nil) ⇒ Object



1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
# File 'lib/chino_ruby/classes.rb', line 1334

def search_documents(schema_id, result_type, filter_type, sort, filter, limit=nil, offset=nil)
  check_string(schema_id)
  check_string(result_type)
  check_string(filter_type)
  check_json(sort)
  check_json(filter)
  data = {"result_type": result_type, "filter_type": filter_type, "filter": filter, "sort": sort}.to_json
  docs = GetDocumentsResponse.new
  if limit==nil && offset==nil
    docs.from_json(post_resource("/search/documents/#{schema_id}", data, ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
  else
    docs.from_json(post_resource("/search/documents/#{schema_id}", data, limit, offset).to_json)
  end
  ds = docs.documents
  docs.documents = []
  ds.each do |d|
    doc = Document.new
    doc.from_json(d.to_json)
    docs.documents.push(doc)
  end
  docs
end

#search_users(user_schema_id, result_type, filter_type, sort, filter, limit = nil, offset = nil) ⇒ Object



1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'lib/chino_ruby/classes.rb', line 1419

def search_users(user_schema_id, result_type, filter_type, sort, filter, limit=nil, offset=nil)
  check_string(user_schema_id)
  check_string(result_type)
  check_string(filter_type)
  check_json(sort)
  check_json(filter)
  data = {"result_type": result_type, "filter_type": filter_type, "filter": filter, "sort": sort}.to_json
  users = GetUsersResponse.new
  if limit==nil && offset==nil
    users.from_json(post_resource("/search/users/#{user_schema_id}", data, ChinoRuby::QUERY_DEFAULT_LIMIT, 0).to_json)
  else
    users.from_json(post_resource("/search/users/#{user_schema_id}", data, limit, offset).to_json)
  end
  us = users.users
  users.users = []
  us.each do |u|
    user = User.new
    user.from_json(u.to_json)
    users.users.push(user)
  end
  users
end