Class: Luo::Marqo

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/luo/marqo.rb

Constant Summary collapse

SEARCH_PARAMS =
Dry::Schema.Params do
  required(:q).filled(:string)
  optional(:limit).maybe(:integer)
  optional(:offset).maybe(:integer)
  optional(:filter).maybe(:string)
  optional(:searchableAttributes).maybe(:array)
  optional(:showHighlights).maybe(:bool)
  optional(:searchMethod).value(included_in?: %w[TENSOR LEXICAL])
  optional(:attributesToRetrieve).maybe(:array)
  optional(:reRanker).maybe(:string)
  optional(:boost).maybe(:hash)
  optional(:image_download_headers).maybe(:hash)
  optional(:context).maybe(:hash)
  optional(:scoreModifiers).maybe(:hash)
  optional(:modelAuth).maybe(:hash)
end

Instance Method Summary collapse

Methods included from Configurable

#config, included

Instance Method Details

#add_documents(hash, non_tensor_fields: nil) ⇒ Object

添加文档到向量数据库



36
37
38
39
40
41
# File 'lib/luo/marqo.rb', line 36

def add_documents(hash, non_tensor_fields: nil)
  client.post("/indexes/#{@index_name}/documents") do |req|
    req.params[:non_tensor_fields] = non_tensor_fields unless non_tensor_fields.nil?
    req.body = hash
  end
end

#delete(id_or_ids) ⇒ Object



53
54
55
56
# File 'lib/luo/marqo.rb', line 53

def delete(id_or_ids)
  id_or_ids = [id_or_ids] if id_or_ids.is_a?(String)
  client.post("/indexes/#{@index_name}/documents/delete-batch", id_or_ids)
end

#index(index_name) ⇒ Object



29
30
31
32
# File 'lib/luo/marqo.rb', line 29

def index(index_name)
  @index_name = index_name
  self
end

#refreshObject



43
44
45
# File 'lib/luo/marqo.rb', line 43

def refresh
  client.post("/indexes/#{@index_name}/refresh")
end

#removeObject



58
59
60
# File 'lib/luo/marqo.rb', line 58

def remove
  client.delete("/indexes/#{@index_name}")
end

#search(params) ⇒ Object



47
48
49
50
51
# File 'lib/luo/marqo.rb', line 47

def search(params)
  params = SEARCH_PARAMS.call(params)
  return params.errors unless params.success?
  client.post("/indexes/#{@index_name}/search", params.to_h)
end