Rails World 2026: Active Search (Donal McBreen) Video: https://www.youtube.com/watch?v=OlzIyVzmMgI Talk page: https://www.rubyevents.org/talks/active-search Unedited machine transcript, generated with Whisper. It contains recognition errors (names, technical terms). Check the video before quoting. ---- Thank you. It can get quite exhausting talking to AIs all day and talking about AIs when you're not talking to them, so this is going to be an AI-free talk, so we can have a little interlude. My name is Donald McBreen. I'm a principal programmer at 37signals, and today we're going to talk about Active Search, a new framework that is searched for Rails. This is a full-text search framework with pluggable stores, so it's like a lot of Rails frameworks like, say, Active Record or Active Job, where you have a single API and then multiple adapters that you can plug into that. There are two separate types of store that we build this with, databases, which do have full-text features, and dedicated search engines. So a little bit small there, but if we look at what the engines that are currently supported, we've got SQLite, Postgres, and MySQL are the databases, and then lots of different search engines on top of that as well. So a lot of different signals. We use Active Search with Elastic Search in Base Camp, and then we also have Fizzy, which is a free Kanban app that we have that comes in two flavors. There's the kind of SAS version that you can sign up for, but you can also run it yourself, and we use Active Search in that to switch between MySQL and SQLite, and Fizzy is open source, so if you want to go and look at it, you can see Active Search in use there. Okay, so we're going to go and we're going to go and we're going to go and we're going to go and we're going to go and we're going to go and we're going to go and we're going to go. So we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search works and we're going to go and see how Active Search in our articles table, which are the title and the body, and then we've got some fields that we're going to want to filter our results on. So that's kind of typically how searches work. full text where you are looking for various words in it, and then the filters on top. And we're mainly going to focus on the articles in this talk. Okay, so stepping through how it works. First of all, so Active Search is a gem. You need to install the gem. But once you've done that, you can then install it into your application. You run this Rails Active Search install task. And initially for the demo, we're going to use Elastic Search. As the adapter, the slight differences with the database adapters, we'll get to that a bit later. But we'll start with Elastic Search, but it could be any of the dedicated search engines. And when you install it, it's going to create two new files, config.search.yaml and config.search.rb. And the YAML file looks like this. We're just going to set per environment what our adapter is, and then some adapter-specific config. So the port of the Elastic Search endpoint, we'd set that in here as well. But we're defaulting to localhost and port 9200, which is the kind of defaults for Elastic Search for this demo. And then if we look in search.rb, it's basically empty. You've got a comment here showing what you might want to put in there, so a little sneak peek ahead of getting to that. But to start with, there's nothing in there. This is the file where we're going to define the search indexes that we're going to use, and we're going to use. And to start with, we're going to use. And to start with, we haven't got any. So first thing to do is let's create an index. So we've got our article model. We want to create an index for that. So we're going to call this rake generator and generate the files you need. So you can see the different fields. We've got title and body. They don't have a type, so they're the kind of default. So you can see the different fields. So you can see the different fields. So you can see the different fields. And so we're going to see the different fields. So we're going to see the different fields. And so we're going to see the different fields. So we're going to see the different fields. So we're going to see the different fields. And so we're going to see the different fields. So we're going to see the different fields. And so we're going to see the different fields. So we're going to see what those fields. So we're going to see what those fields. And so we're going to see what those fields look like. So we're going to see what those fields look like. So in config search.rb, we're defining our index. And you can see all those fields that we listed there are going to get put into this definition. And we've got text, and we've also got string. Now, these are kind of active search-specific types. And a text field is one where we're going to want to search the text, whereas a string is kind of just a string that we might want to filter on. And we'll see that later on. And then if we look in our model, we'll see that adding this has search declaration in there. And that's going to do a bunch of stuff. It's going to add some methods into the model class methods and instance methods. And it's also going to add some callbacks that we're going to use to index our article. So the next step is to run this task, which will create our index. And we'll pass index equals articles through to say that's the particular index that we want to create here. And you can see here, just look at the output here showing the fields that it's created and that we're using the Elasticsearch adapter here. So when this runs, we will actually have our index. So we can now have our index. So we can now have a look. So if we call the Elasticsearch HTTP API, we can just pass this through and get the resulting mapping back from Elasticsearch that shows we've created this index. Now, there isn't a kind of migration system in here where if you want to add an extra field onto an index, it can do that for you the way that ActiveRecord does. That would be probably quite complex to make it complex to make it complex to make it complex to make work across all the different search engines. Something you could maybe do, something you could maybe do, but right now, it doesn't do that. It doesn't do that. It's simply, if you've defined an index, you can create it. If you want to do things like adding fields, you'd have to do that manually later on. And then we've got some topical news here in our articles, but we can create a bunch of articles and write those. And when we do that, that's actually going to trigger some callbacks that are going to put those articles. So we're going to put those articles into our index. After we've done that. After we've done that, we can then search. And searching looks like this. So by adding the hasSearch declaration into article, we've added this class method search. And we can go article.searchrails and get the results back from that. And the results is a specific results object, but it's enumerable. So we can have a look at the first one. So what you can see here. What you can see here is that when you search for articles, you get articles back. So it's not returning the representation that's in the search engine. It's getting the results from your search engine, but then grabbing the actual models from the database, hydrating those into a result set. And if we just map through the titles, you'll see we found all the ones with Rails in there. So that's the simple version of how it works. So we've defined this index. So let's have a closer look at what the index itself is. And there's kind of just three steps here. And there's kind of just three steps here. We've got an index and that talks to a store adapter. And the store adapter is this different adapters for each of the engines we support. So in our case, it'll be the Elasticsearch store adapter. And then that adapter talks to Elasticsearch. And when we want to put something into our index, we're going to take the article, pass it to the index, I'll pass it on to the adapter and then into the specific search engine that we're looking at. Okay. And if we want to search, we'll pass it on to the index. Okay. And if we want to search, we'll pass a query in. That'll go all the way through. We'll get to them. The results will get passed all the way back up the chain and back out. So this is how that works with the index. If we want to look here, we can actually go and have a look at the index itself. So on the active search module, you can call this index method. And pass in the index. So for the article model, it's going to default using the same name as the database table would use. So our index is called articles. And we can grab that and interrogate it for various things, the name, the underlying index name. You can have a look at the store itself, which is the adapter we've got to talk to our underlying search engine. So this returns this active search index model and index model. We can talk to our index index model. We can talk to our index. We can talk to our index. We can talk to our index. We can talk to our index. We can talk to our index. We can talk to our index. We can talk to this index directly. So we can add articles in. We can remove articles. We've got remove by ID. If you've already destroyed an article, then you don't have it to remove it. So you can just remove it by the ID. And then you can run searches as well directly against the index. So when we looked before, we were kind of looking at how this integrated into our active record model. And that's kind of the kind of the kind of more standard way that you would do this. You would have your models. You would have your models and the integration would talk to the index for you. So you wouldn't actually need to look at the index and call methods on it. But it is something you can do directly. So you can use active search without active record. You can go and create an index and just create your, you know, add your own data. And it could just be some like, I don't know, just a hash that you had that you wanted to index. So I don't know, just a hash that you had that you had that you had that you had that you had that you had. So you had to look a bit more closely at the indexing part of this. Okay. So we saw earlier, we created some articles. So what's happening when we create a model or update it or destroy it is that behind the scenes, it's kicking off a job for us to run. And the job, so when we added that has search before, it inserted some callback and these callbacks and these callbacks and these callbacks are going to create these jobs for us. Now, the full name, they're like active search, colon, colon, the index job or remove job, but just remove that from here for brevity. And then those jobs can run behind the scenes. Now, if you want to do your indexing inline, you can write has search async true, sorry, async false, and that will disable creating jobs. And instead, just when you update your model, it'll at the same time do the indexing in-process. So here's kind of the steps that we go through when you create or save an article. So here's kind of the steps that we go through when you create or save an article. So we've got some article. We save it. The article will get passed to our re-index job. When the job runs, it's going to pass the article again onto our index. And that's going to say, add this article into our index. Underneath, the index itself is going to convert our article into a document. So that the different stores understand. And then the store will do whatever it needs to do to that document to add our record into the underlying search engine. So in the case of Elasticsearch, that's a blob of JSON. If we were using SQLite, that would be a query, like an insert query or an up search or something like that. And those are kind of the steps we go through when saving an article. So the document. So the document. So the document we can see here. So if we grab our index, we can call this document for method and see what the document looks like for an article. This isn't something you would normally need to do. It's kind of just parts of the internals, but we can just look at it now to see what's in that document. And it's pretty simple. It's just got an ID and then a bunch of data. And you can see the data. It just matches up with the fields that we have added to our index. And by default. And by default. And by default. And by default, and by default. And by default, it's just going to assume that there's a method on the article that will pass these. So we'll just call article.title to grab the title and article.body and so on and grab those values and just generate a hash with those values. And this thing gets passed on to our store, which will pass it into the underlying engine. Now, you might not want to, you know, that might not work because maybe you're under like the method names are different on your article or you need to grab some data from somewhere else from some other related model or something like that. So you can set a custom serializer to replace that sort of default behavior. So you can pass it in like this. Here we're using a separate method. You could just have an inline proc as well. And then what we do is we are going to call this method instead when we want to generate the document. And so we see here, we've decided we want to truncate the body to avoid bloating or index and say we don't actually have a status field or infer our status from the publish that date. Okay, so that's indexing. We have these callbacks. They create jobs. The jobs run past the data to the index and then it ends up in the underlying store. So the next part is searching. So how do we search this? Well, we're searching with this chainable API. So it's kind of kind of an active record relation style API. Not exactly the same, but similar idea. And we've got all these kind of similar idea. And we've got all these kind of methods on it. So we've got filter methods. You can sort of force a sort order. By default, it would sort by relevance. But there's a bunch of methods in this API that we're not going to have time to go through here. But you can take a look at those later. And if we just have a look at what each of these methods is returning, you'll see the article dot search rails returns this query object. Then when we filter, it returns another query object and so on. So that's kind of the chaining behavior where each level of the chain returns an object containing all the information from before with whatever you've just added to it. So we step through, query, query, query, query, query. And then we call the results method, which returns the results object that we sort of saw briefly before. And since that's enumerable, we could convert that to an array. And we see we've got an array of articles. So this is kind of how searching works. Just one method that it's worth looking at there is the filter method, because this is probably the most common thing that you'd want to do other than searching. So you can do various things. You can filter by fields, by multiple fields at once, multiple values. And you can do things with ranges and open-ended and closed ranges in order to do things like that. So this is the results. So that's the results. So the results. So the results. So the results. So the results. The results. So why do we have that? Why don't we just do article dot search rails dot each and then just go through all the results? Well, the reason is that a search is not just like an array of your results. There's more to it. There's metadata about the search. There's a total number of results you found. So what was it. There's a total number of results. So there's a total number of results. So it's a total number of results. It's a total number of results. So where that's where that metadata is going to sit. So things like if there was a relevant score, the actual underlying fields that came back from the search engine. Another example might be if you'd asked your engine to highlight the results, to mark out the words that actually matched your search. That's where you go and find them in here. Okay. So back at the start, we had our article model. So we had our article model. We had our article model. And for this index. And for this index. And for this index, we're just going to have a title and a title and a body field that we can search. So there's no extra filters on here. So there's no extra filters on here. But we want to be able to put our articles in here. And we want to put our comments in here. And you can see here that if we create this index, it's going to create the record type and record ID fields into our index. So we can use those to link back to the original models and also to filter by them if needed. Okay. Okay. And so next, if we go back and look at how this index. Okay. And so next, if we go back and look at how this index, this polymorphic index is used. So we already had our has search in our article, but we've added a second has search here. So article now is going into two different indexes. It's going into our dedicated article index, but it's also going to go into this records index. And the default name for the index is articles because it's just generated from the model. So the first one doesn't need to specify the name of the index, but the second one doesn't. So that we can do that. So that we can do that. So that we can do that. So that we can do that. So that we can tie it up to that index. We just create it. So when you create an article or update one, the callbacks that run are going to generate two jobs in this case, one job to put it in our articles index and one to put it in a records index. And then if we look underneath, we do a similar thing in our comments. In this case, we're passing in a custom serializer because our comments don't actually have their own title. So we generate one from the article from the article from the article from the article from the article from the article itself and put that in here. Now, for this indexing to work in real life, you need to make sure that you have things in place so that when you updated the article, it triggered a re-index of the comments at the same time, which you can do, but I'm not going to cover that here. And then when we've got this polymorphic index, we can search it. And there's two different ways of searching it. The first one is here, where we can grab the index directly. So we can call the active search index method, which gives us the underlying index. And then we can call search on it. And you can see underneath that when we do that, we're going to get back a mixture of comments and articles because that's what's in our index. Now, if instead we call the search method directly on the model, we're doing that here. Now, in this case, it's going to default to our articles index when we search. So we need to tell it, no, actually go and search the records index. So we're just getting articles back. So we're just getting articles back. So we're just getting articles back. So it's attached a hidden filter that we can't see here. But when we search via this way in a polymorphic index, it's going to be the equivalent of if on the first query we'd added dot filter record type article. So we only get articles back. And when we search via the search method for the article, we can basically pretend there's just articles in our index. Cool. Okay. So initially, we showed the first example we're using Elasticsearch. So now just look a little bit at how it works with the database adapters because it's just a slight bit more to this. Okay. So we're going to run our installer again, but this time we're going to install with the SQLite adapter. So you can see here it's a pretty basic configuration in search.yaml. All it's saying is we're going to use the SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, SQLite adapter, and this is going to, under the hood, it's going to use the configuration in our database.yaml file and use that to hook everything up. So yeah, by default, it's just going to use whatever the default database was there. And this time, if we have a look what happens when we generate our article index, so this is the same thing we ran before to generate it with Elasticsearch, we're going to append our index definition into configsearch.yaml.yaml.yaml.yaml.yaml.yaml and the model we're indexing is with a document appended to it, and it just subclasses application record. And this means that you've got the place to do any custom routing, any replicas. If you want to put your documents in a separate database, you can just use the standard active record mechanisms for handling multiple databases. And if we look at the migration, we're going to go back to that, again, so this is specifically for SQLite, which we're using. We're going to create the article documents table, and in there, we're adding these fields, the article ID, the status, and the views, and publish that. And we also, at the bottom here, we're creating virtual table, which is just SQLite's mechanism for full-text search. So we're creating this article documents_fts virtual table, and in there, we're putting the title and body, and that's just SQLite's mechanism for doing full-text file for doing full-text searching for doing full-text searching. So we've created the separate documents table. So it does mean we're going to be denormalizing the data, which is probably already in the database somewhere else into this table. By default, we have to do that because we don't know where the data is coming from. There could be a custom serializer. It could be pulled from anywhere. There's no way we can run things backwards and filter it from wherever it came from. So we need to denormalize it in there. So that's why we have this separate table. And just to look through the different adapters that are supported. They all have their own mechanisms for doing full-text search. So we saw the virtual table with SQLite. Postgres adds the columns directly into the table, but then it also adds these vector columns with an index on them. MySQL is kind of the simplest version. It just adds some and creates a full-text index on top of those. So sorry, sorry. Sorry, it's a bit small to read there, but that's kind of the full set of stuff I wanted to look through here. There are some other features that you can go and look at. There isn't really time to go through here. Sort of on the query, there's all these various different methods. So we've got reject and filter any. They're kind of the negative and the or version of filtering. I think the most interesting ones here are toNativeQuery and Native. So if you want to look at that query. If you do that on ElasticSearch. If you do that on ElasticSearch, you get back the hash that you're going to generate the JSON from. If you do it in SQLite, you're going to generate the JSON from. If you do it in SQLite, you'll actually get back an ActiveRecord relation. So you can do crazy things like just join to that or just plug it into ActiveRecord as you want. And then the native field is a kind of an escape hatch. If there's something you want to do with your search engine that ActiveSearch doesn't support, you can call the native method and use that to alter the underlying thing. So maybe you've got some custom ranking mechanism that you want to plug in and you can do that. And then at the bottom under the stores, we've got capabilities. So search engines, they're not as homogenous as databases with ActiveRecord. Most things are supported by all the databases. But with search engines. But with search engines, there's some features. Some engines will support and other ones won't. So capabilities is a way for the underlying store to define what things it supports. So a good example there is highlighting. So highlighting where you say, I want you to mark in the search text the words that you actually found. That works on all the engines except for MySQL. So MySQL you can't do highlighting with. So if you want highlighting, you have to do it yourself on the results that you have to do it yourself on the results that you get back. And so those capabilities, they're kind of just a way to allow you to do that. Cool. Right. So just to go back, here's the kind of super simple version of how ActiveSearch works. This has search into your model, and then you call search and get your results. And that's kind of how it works. Okay. So to finish up. So to finish up, here's a link to the gem. There's a link to the gem at the moment. There's a link to the gem at the moment. Tiny, tiny link. But if you look really close, you might be able to see it. So it's github.com/basecamp/rails-active_search. And as I said also, if you look at the Fizzy app, which is under Basecamp/fizzy in GitHub, you can see this in action. And yeah, it's still kind of sort of alpha-ish software. And the API. The API has not set its own at all. So just be aware of that if you are planning to try it out. And yeah, be interested in any feedback you have. So thank you all very much. Cheers.