166 points · by okket · 10y ago · 84 comments
If you're using a secondary data store like Elasticsearch presumably you're combining it with a traditional relational database like Postgres or MySQL. And in most use cases, you're going to require additional constraints on top of full text, like restricting to a specific customer or topic, etc. This means data duplication. Any experienced developer should be breaking out in hives at this point because this means (1) keeping two data stores in sync; (2) adding another service to the deployment process; (3) adding another service to development environments; (4) adding another service to debug when problems crop up; (5) large scale schema changes and migrations are complicated because of two data stores; (6) operational overhead from maintaining Elasticsearch like separate backup scripts and deployment scripts and sharding config (across production, qa, and dev); (7) queries are potentially complicated because you may have to aggregate information from two data stores (pagination and delayed updates makes this even more hellish); (8) if your company is anal about devs accessing production data then ES just introduces another layer of requesting permissions and every time the schema changes you have to ask again.
And maybe the advantages of Elasticsearch far outweigh these negatives, but this is a conversation you're going to have with your devs because the cost of using ES is not zero. There are tradeoffs, and maybe just using Postgres full text search is adequate for your use case given sufficient additional constraints.
It's not a serious problem as long as you designate one data source as primary and every other data source as derived. That way, if you ever get data corruption issues, you can re-create the derived data and blow away the old copy. You also don't face complex data-sync issues, because all data-transfer is unidirectional. You write into Postgres or your other source-of-truth, and then you write from Postgres to ElasticSearch. If there's a difference between them, the Postgres version is authoritative, so copy its data over ElasticSearch.
Most of your other points are common to adding any new major feature, and you should always weigh the user benefits against the maintenance costs of a large new dependency. Presumably, if you're looking at ElasticSearch, it's because full-text search in your RDBMS is not good enough (which won't always be the case, but has been my experience and apparently the experience of several other people here).
It's much easier to scale a ephemeral search service than a transactional database. Likely even one search node will support a higher query throughput.
sudo apt-get install elastic-search
pip install django-haystack
add haystack to installed_apps, add the elastic search backend and set the end point.
use a haystack index class that is prebuilt to hook into all model change signals.
done. No weird configurations, everything was vanilla.
Maybe I am missing a step but it took me less than an hour to get everything going and it hasn't had to have any maintenance.
When you are adding ngram support and all the indexes and views in postgres to replicate the behavior, ES looks to be less complicated. At the very least I don't see a reduction in complexity doing the postgres way just that you have 1 less dependency to worry about.
https://www.elastic.co/guide/en/elasticsearch/guide/2.x/gett...
Unless you need basic search and you desperately want to keep everything in the same database I would split it out. Architecturally it makes more sense to split it out just to allow for different scaling needs.
1. https://www.elastic.co/guide/en/elasticsearch/reference/curr...
2. https://www.elastic.co/guide/en/elasticsearch/guide/current/...
https://www.pgcon.org/2016/schedule/attachments/436_pgcon-20...
As far as I am concerned, it is far superior to the goodies that mentioned in this article.
p. 1: sales pitch
p. 2: sales pitch
p. 3: "The only requirement for installing Elasticsearch is a recent version of Java." Ugh.
pp. 4-10: A new JSON API to learn. This is just the part about getting your stuff into Elasticsearch and, at last, doing a query that is no better than "select * from table where last_name like '%Smith%';
p. 11: "More complicated searches," an apt title. Here is the Elasticsearch query that's equivalent to "select * from table where last_name like '%smith%' and age > 30":
GET /megacorp/employee/_search
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}
It only gets more complicated from there. There is talk about ranking, but it was still a black box even by the end of the Getting Started guide, and I was going to want to tweak it. It just seemed more obvious how to do so with Postgres (https://www.postgresql.org/docs/9.6/static/textsearch-contro...).How is Postgres merely "good enough"? How are other products better? What is their draw?
Is it ease of set-up? For me Elasticsearch was harder to install than Postgres and made me learn a bunch of new syntax, beyond the SQL I have to know anyway to write web applications in the first place. Postgres handles stemming, stopwords, ranking, snippets, and highlighting.
Is it better ranking? Postgres can weigh the search words' frequency, their proximity to each other, the length of the document, the number of unique words in the document, and the "mean harmonic distance between extents." I was able to further tweak the results by using other columns and joining other tables.
Is it faster search results? Postgres searches tens of thousands of pages in a split second.
Can they be really considered 'documents' or are they actually database fields?
Can you explain how you keep your Elasticsearch index consistent with your main database, in face of system failure (for example a network partition or a machine hard reboot)? This is usually the non-trivial part.
Using Postgres for search just from a simplicity point of view can save many engineering hours especially if you don't have an expert in Solr/Elasticsearch available. This is even more true these days with SolrCloud needing a Zookeeper ensemble as well.
https://www.chrisstucchio.com/blog/2013/postgres_external_tr...
For one, listen/notify in Postgres wakes up all listeners on each notify. You will want a single stream of indexing updates, yet you will also want it to be fault-tolerant; if one indexer isn't running, another must take over its work.
Listen/notify also doesn't work well if you want to distinguish between batch updates (large imports, for example) and realtime ones.
Thirdly, if you miss out on events, you will want the syncing to catch up from where it was last.
The better design is to maintain state about indexing. If each table has a sequential ID and an "updated_at" column, you can exploit those to build deltas. Another option is to maintain a "changes" table that simply tracks the ID of each modified row, with a sequence number per change. This can be super useful for other purposes (auditing, debugging, support), and makes it easy to index from an earlier point in time.
Such a system will also let you more easily recover from hard failures in ES where entire shards (and their replicas) are lost (I have had it happen with 1.x, should be much less rare in 2.x).
Keeping one data store (ES) in sync with another (Postgres, or anything else) is an interesting problem in general.
For a large number of sites search could be improved greatly by simple setting up Sphinx with an SQL query for slurping up data from you backend database.
Why is that necessary? One of the things which makes maintaining a search index easier is that typically your search datastore has much weaker consistency and durability requirements than your primary one.
I was mostly thinking of Sphinx as a solution for smallish site, that just need the search box to be working. Even though is works just as well for site like Craigslist (but I don't think they use the SQL data source)
For basic utility stuff or simple backend apps it's pretty useful. For anything serious or consumer facing it's crap.
https://developers.google.com/custom-search/
I'm an ex-Googler who's now working fairly extensively with ElasticSearch. While I've been impressed at how easy it is to get started, all the published academic ranking algorithms are utter crap compared to what Google has. Okapi BM25 is mentioned as the state-of-the-art up-thread; it was developed in the 80s, long before Google was born, and most of the advances in search since the 90s have happened behind closed corporate doors inside Google, Yahoo, and Bing.
What is it not better at though? Pretty much everything else. GIS, Cross joins, parent child indexing, etc. Postgres can do it all too with a little digging.
The thing neither of them has? Semantic search. If I ever hit that scale again at another company, the jump won't be from SQL to Lucene, but from SQL to Google.
In my experience, on commercial projects, using 9.4+ postgres GIN tag/keyword search index is very fast over data on the order of 30 to 100 million records, providing you have a host with SSD disks.
I call this size data "MID" data, not "BIG" data, and it seems applicable to a lot of startups before they get to a million customers, and for a lot of real world customer data use cases.
Also, a lot of startups need to handle gps geo-coordinates .. having used them, I have to say that the primitives that come with postGIS are pretty superb, and easily integrated.
Outside startup-land, there are a lot of companies using horrible data systems, dying to get to their data quickly, who could be much better served with postgres via a web front end. ( Lowering the cost/time to build that is something I'm working on )
So if :
* you want fast tag search
* your data has locations or nearby-x
* you'll have < 1bn rows for some time
* your data is worth money
then imo, postgres+postGIS+GIN+SSD is pretty compelling.That covers a lot of startup and business surface area.
I've run consumer facing products with real-time autosuggest using postgresql's search. Calling it crap is hyperbole.
So it's more about being able to find what people are looking for, not the number of users you have. And to be fair, almost all search solution currently available will scale beyond what most of us need in terms for number of users or the size of our datasets.
Given enough sprints, the search engine will eventually be run by an external service. +1 for the Google Custom Search suggestion. If SEO matters to the problem domain, might as well leverage those carefully indexed documents.
https://news.ycombinator.com/item?id=8714477 (~2 years ago, 58 comments)
https://news.ycombinator.com/item?id=8381748 (~2 years ago, 95 comments)
https://news.ycombinator.com/item?id=12605156
And this new index type for PostgreSQL:
BM25 is simply TFIDF with a couple of tunable parameters. These parameters will help with precisely the issues which force people to disable TF (saturating the value early, ...). From what I can see, Elasticsearch is mainly used as an analytics/logging engine and not search. Many NLP heavy organizations are using Solr (or straight Lucene).
You'd think it wouldn't be that hard seeing as SQLite has lots of working code, and it's reasonably similar to pg.