We've all seen them on various websites. Crappy search utilities. They are a constant reminder that search is not something you should take lightly when building a website or application. Search is not just Google's game anymore. When a Java library called Lucene was introduced into the Apache ecosystem, and then Solr was built on top of that, open source developers began to wield some serious power when it came to customizing search features.
In this article you'll be introduced to Apache Solr and a wealth of applications that have been built with it. The content is divided as follows:
- Setup Solr
- Drupal integration
- Hathi Trust
- Auto Suggestions
- Spatial search applications
- Clustering Support with Carrot2
- Near Real Time Search
- Loggly = Full text search in logs
- Solandra = Solr + Cassandra
- Category Browsing via Facets
- Jetwick - Open Twitter Search
- Plaxo - Online Address Management
- Replace FAST or Google Search
- Search Application Prototyping
- Solr as a Whitelist
Apache Solr is an open source search server. It is based on the full text search engine called Apache Lucene. So basically Solr is an HTTP wrapper around an inverted index provided by Lucene. An inverted index could be seen as a list of words where each word-entry links to the documents it is contained in. That way getting all documents for the search query "dzone" is a simple 'get' operation.
One advantage of Solr in enterprise projects is that you don't need any Java code, although Java itself has to be installed. If you are unsure when to use Solr and when Lucene, these answers could help. If you need to build your Solr index from websites, you should take a look into the open source crawler called Apache Nutch before creating your own solution.
To be convinced that Solr is actually used in a lot of enterprise projects, take a look at this amazing list of public projects powered by Solr. If you encounter problems then the mailing list or stackoverflow will help you. To make the introduction complete I would like to mention my personal link list and the resources page which lists books, articles and more interesting material.
2. Setup Solr
As the very first step, you should follow the official tutorial which covers the basic aspects of any search use case:
- Indexing - Get the data of any form into Solr. Examples: JSON, XML, CSV and SQL-database. This step creates the inverted index - i.e. it links every term to its documents.
- Querying - Ask Solr to return the most relevant documents for the users' query
To follow the official tutorial you'll have to download Java and the latest version of Solr here. More information about installation is available at the official description.
Next you'll have to decide which web server you choose for Solr. In the official tutorial, Jetty is used, but you can also use Tomcat. When you choose Tomcat be sure you are setting the UTF-8 encoding in the server.xml. I would also research the different versions of Solr, which can be quite confusing for beginners:
- The current stable version is 1.4.1. Use this if you need a stable search and don't need one of the latest features.
- The next stable version of Solr will be 3.x
- The versions 1.5 and 2.x will be skipped in order to reach the same versioning as Lucene.
- Version 4.x is the latest development branch. Solr 4.x handles advanced features like language detection via Tika, spatial search, results grouping (group by field / collapsing), a new "user-facing" query parser (edismax handler), near real time indexing, huge fuzzy search performance improvements, SQL JOIN-a like feature and more.
If you've followed the official tutorial you have pushed some XML files into the Solr index. This process is called indexing or feeding. There are a lot more possibilities to get data into Solr:
- Using the Data Import Handler (DIH) is a really powerful language neutral option. It allows you to read from a SQL database, from CSV, XML files, RSS feeds, Emails, etc. without any Java knowledge. DIH handles full-imports and delta-imports. This is necessary when only a small amount of documents were added, updated or deleted.
- The HTTP interface is used from the post tool, which you have already used in the official tutorial to index XML files.
- Client libraries in different languages also exist. (e.g. for Java (SolrJ) or Python).
Before indexing you'll have to decide which data fields should be searchable and how the fields should get indexed. For example, when you have a field with HTML in it, then you can strip irrelevant characters, tokenize the text into 'searchable terms', lower case the terms and finally stem the terms. In contrast, if you would have a field with text in it that should not be interpreted (e.g. URLs) you shouldn't tokenize it and use the default field type string. Please refer to the official documentation about field and field type definitions in the schema.xml file.
When designing an index keep in mind the advice from Mauricio: "The document is what you will search for." For example, if you have tweets and you want to search for similar users, you'll need to setup a user index - created from the tweets. Then every document is a user. If you want to search for tweets, then setup a tweet index; then every document is a tweet. Of course, you can setup both indices with the multi index options of Solr.
Please also note that there is a project called Solr Cell which lets you extract the relevant information out of several different document types with the help of Tika.
For debugging it is very convenient to use the HTTP interface with a browser to query Solr and get back XML. Use Firefox and the XML will be displayed nicely:
You can also use the velocity contribution, a cross-browser tool, which will be covered in more detail in the section about 'Search Application Prototyping'. To query the index you can use the dismax handler or standard query handler. You can filter and sort the results:
You can also do a lot more; one other concept is boosting. In Solr you can boost while indexing and while querying. To prefer the terms in the title write:
When using the dismax request handler write:
Now I will list some interesting use cases for Solr - in no particular order. To see how powerful and flexible this open source search server is.
3.1. Drupal Integration
The Drupal integration can be seen as generic use case to integrate Solr into PHP projects. For the PHP integration you have the choice to either use the HTTP interface for querying and retrieving XML or JSON. Or to use the PHP solr client library. Here is a screenshot of a typical faceted search in drupal:
For more information about faceted search look into the wiki of Solr.
More PHP projects which integrates Solr:
- Open Source Typo3- Solr Module
- Magento Enterprise - Solr Module. The open source integration is out dated.
- Oxid - Solr Module. No open source integration available.
3.2. Hathi Trust
The hathi trust project is a nice example that proves Solr's ability to search big digital libraries. To quote directly from the article: "... the index for our one million book index is over 200 gigabytes ... so we expect to end up with a two terabyte index for 10 million books"
Other examples for libraries:
3.3. Auto Suggestions
To push it to the extreme you can use a Lucene index entirely in RAM. This approach is used in a large music shop in Germany.
Live examples for auto suggestions:
3.4. Spatial search applications
When mentioning spatial search, people have geographical based applications in mind. With Solr, this ordinary use case is attainable . Some examples for this are:
Spatial search can be useful in many different ways: for bioinformatics, fingerprints search, facial search, etc. (getting the fingerprint of a document is important for duplicate detection). The simplest approach is implemented in Jetwick to reduce duplicate tweets, but this yields a performance of O(n) where n is the number of queried terms. This is okay for 10 or less terms, but it can get even better at O(1)! The idea is to use a special hash set to get all similar documents. This technique is called local sensitive hashing. Read this nice paper about 'Near Similarity Search and Plagiarism Analysis' for more information.
The index for that feature contains 18M documents and has a size of ~12GB. For this case Gabriel had to tune Solr: "I have two requirements that differ a bit from most sites with respect to Solr:
- I generally only show one result, with sometimes a couple below if you click on them. Therefore, it was really important that the first result is what people expected.
- False positives are really bad in 0-click, so I needed a way to not show anything if a match wasn't too relevant.
I got around these by a) tweaking dismax and schema and b) adding my own relevancy filter on top that would re-order and not show anything in various situations." All the rest is done with tuned open source products. To quote Gabriel again: "The main results are a hybrid of a lot of things, including external APIs, e.g. Bing, WolframAlpha, Yahoo, my own indexes and negative indexes (spam removal), etc. There are a bunch of different types of data I'm working with." Check out the other cool features such as privacy or bang searches.
3.6. Clustering Support with Carrot2
Carrot2 is one of the "contributed plugins" of Solr. With Carrot2 you can support clustering: "Clustering is the assignment of a set of observations into subsets (called clusters) so that observations in the same cluster are similar in some sense." See some research papers regarding clustering here. Here is one visual example when applying clustering on the search "Pannous" - our company:
3.7. Near Real Time Search
Solr isn't real time yet, but you can tune Solr to the point where it becomes near real time, which means that the time ('real time latency') that a document takes to be searchable after it gets indexed is less than 60 seconds even if you need to update frequently. To make this work, you can setup two indices. One write-only index "W" for the indexer and one read-only index "R" for your application. Index R refers to the same data directory of W, which has to be defined in the solrconfig.xml of R via:
To make sure your users and the R index see the indexed documents of W, you have to trigger an empty commit every 60 seconds:
wget -q http://localhost:port/solr/update?stream.body=%3Ccommit/%3E -O /dev/null
Everytime such a commit is triggered a new searcher without any cache entries is created. This can harm performance for visitors hitting the empty cache directly after this commit, but you can fill the cache with static searches with the help of the newSearcher entry in your solrconfig.xml. Additionally, the autowarmCount property needs to be tuned, which fills the cache with a newSearcher from old entries.
Also, take a look at the article 'Scaling Lucene and Solr', where experts explain in detail what to do with large indices (=> 'sharding') and what to do for high query volume (=> 'replicating').
3.8. Loggly = Full text search in logs
Feeding log files into Solr and searching them at near real-time shows that Solr can handle massive amounts of data and queries the data quickly. I've setup a simple project where I'm doing similar things, but loggly has done a lot more to make the same task real-time and distributed. You'll need to keep the write index as small as possible otherwise commit time will increase too great. Loggly creates a new Solr index every 5 minutes and includes this when searching using the distributed capabilities of Solr! They are merging the cores to keep the number of indices small, but this is not as simple as it sounds. Watch this video to get some details about their work.
3.9. Solandra = Solr + Cassandra
Solandra combines Solr and the distributed database Cassandra, which was created by Facebook for its inbox search and then open sourced. At the moment Solandra is not intended for production use. There are still some bugs and the distributed limitations of Solr apply to Solandra too. Tthe developers are working very hard to make Solandra better.
Jetwick can now run via Solandra just by changing the solrconfig.xml. Solandra also has the advantages of being real-time (no optimize, no commit!) and distributed without any major setup involved. The same is true for Solr Cloud.
3.10. Category Browsing via Facets
Solr provides facets, which make it easy to show the user some useful filter options like those shown in the "Drupal integration" example. Like I described earlier, it is even possible to browse through a deep category tree. The main advantage here is that the categories depend on the query. This way the user can further filter the search results with this category tree provided by you. Here is an example where this feature is implemented for one of the biggest second hand stores in Germany. A click on 'Schauspieler' shows its sub-items:
3.11. Jetwick - Open Twitter Search
You may have noticed that Twitter is using Lucene under the hood. Twitter has a very extreme use case: over 1,000 tweets per second, over 12,000 queries per second, but the real-time latency is under 10 seconds! However, the relevancy at that volume is often not that good in my opinion. Twitter search often contains a lot of duplicates and noise.
Reducing this was one reason I created Jetwick in my spare time. I'm mentioning Jetwick here because it makes extreme use of facets which provides all the filters to the user. Facets are used for the RSS-alike feature (saved searches), the various filters like language and retweet-count on the left, and to get trending terms and links on the right:
To make Jetwick more scalable I'll need to decide which of the following distribution options to choose:
- Use Solr Cloud with Zookeeper
- Use Solandra
- Move from Solr to elasticsearch which is also based on Apache Lucene
Other examples with a lot of facets:
- CNET Reviews - Product reviews. Electronics reviews, computer reviews & more.
- Shopper.com - Compare prices and shop for computers, cell phones, digital cameras & more.
- Zappos - Shoes and Clothing.
- manta.com - Find companies. Connect with customers.
3.12. Plaxo - Online Address Management
Plaxo.com, which is now owned by Comcast, hosts web addresses for more than 40 million people and offers smart search through the addresses - with the help of Solr. Plaxo is trying to get the latest 'social' information of your contacts through blog posts, tweets, etc. Plaxo also tries to reduce duplicates.
3.13. Replace FAST or Google Search
Several users report that they have migrated from a commercial search solution like FAST or Google Search Appliance (GSA) to Solr (or Lucene). The reasons for that migration are different: FAST drops linux support and Google can make integration problems. The main reason for me is that Solr isn't a black box—you can tweak the source code, maintain old versions and fix your bugs more quickly!
3.14. Search Application Prototyping
With the help of the already integrated velocity plugin and the data import handler it is possible to create an application prototype for your search within a few hours. The next version of Solr makes the use of velocity easier. The GUI is available via http://localhost:port/solr/browse
3.15. Solr as a Whitelist
Imagine you are the new google and you have a lot of different types of data to display e.g. 'news', 'video', 'music', 'maps', 'shopping' and much more. Some of those types can only be retrieved from some legacy systems and you only want to show the most appropriated types based on your business logic. E.g. a query which contains 'new york' should result in the selection of results from 'maps', but 'new yorker' should prefer results from the 'shopping' type.
With Solr you can set up such a whitelist-index that will help to decide which type is more important for the search query. For example if you get more or more relevant results for the 'shopping' type then you should prefer results from this type. Without the whitelist-index - i.e. having all data in separate indices or systems, would make it nearly impossible to compare the relevancy.
The whitelist-index can be used as illustrated in the next steps. 1. query the whitelist-index, 2. decide which data types to display, 3. query the sub-systems and 4. display results from the selected types only.
Solr is also useful for scientific applications, such as a DNA search systems. I believe Solr can also be used for completely different alphabets so that you can query nucleotide sequences - instead of words - to get the matching genes and determine which organism the sequence occurs in, something similar to Blast.
Another idea you could harness would be to build a very personalized search. Every user can drag and drop their websites of choice and query them afterwards. For example, often I only need stackoverflow, some wikis and some mailing lists with the expected results, but normal web search engines (Google, Bing, etc.) give me results that are too cluttered.
My final idea for a future Solr-based app could be a Lucene/Solr implementation of desktop search. Solr's facets would be especially handy to quickly filter different sources (files, folders, bookmarks, man pages, ...). It would be a great way to wade through those extra messy desktops.
The next time you think about a problem, think about Solr! Even if you don't know Java and even if you know nothing about search: Solr should be in your toolbox. Solr doesn't only offer professional full text search, it could also add valuable features to your application. Some of them I covered in this article, but I'm sure there are still some exciting possibilities waiting for you!