Reputation: 215
I would like to be able to use data with searchkick that is not via ActiveRecord / Mongoid. In my specific use case, I would like to sync data that is mapped to an API instead of a database.
For this specific use case, using an ActiveRecord supported db does not work because I have business logic that is encapsulated at the API layer for another service that is not written to the db but calculated at request time. So I'm currently in a place where we need to duplicate business logic in the searchkick layer or precompute all business logic and save it to the db.
So the best solution I see currently is to implement the ActiveRecord interface and have it fetch from my API but I was wondering if anyone had a better idea since that interface is relatively large.
Upvotes: 2
Views: 239
Reputation: 131
Yes you can use some of Searchkick's classes to index and query non-DB data. For example these methods in your class or module allow you to create an index with bulk data and search it:
def self.index_name
"my_index_name"
end
def self.search(term, ...)
query = Searchkick::Query.new(nil, term, ...)
query.options[:index_name] = index_name
query.execute
end
def self.reindex
index = Searchkick::Index.new(index_name)
if index.exists?
index.delete
end
index.create(Searchkick::IndexOptions.new(index).index_options)
Searchkick.client.bulk(
index: index_name,
body: generate_documents,
refresh: true,
)
end
def self.generate_documents
documents = [
{
create: {
data: {
field_1: 1,
field_2: "Something worth searching for in here",
}
}
}
]
documents
end
With your queries you must remember to pass the load: false
option. For example:
MyClass.search(
"something in there?",
load: false,
fields: [:field_2],
where: {field_1: 1},
limit: 1,
misspellings: {edit_distance: 2},
)
For your use case it sounds like you will need to add documents to an existing index from time to time. I don't think the Searchkick::Index
class has anything to offer here and so you will need to write a method that descends to the client level and do whatever you want to do there:
Upvotes: 0
Reputation: 1210
You might want to checkout one of the following.
https://github.com/flexirest/flexirest
https://github.com/rails/activeresource
Upvotes: 0