<p>I want to implement search functionality for a website (assume it is similar to SO). I don't want to use Google search of stuff like that.</p>

<p>My question is:</p>

<p>How do I implement this?</p>

<p>There are two methods I am aware of:</p>

<ol>
<li>Search all the databases in the application when the user gives his query.</li>
<li>Index all the data I have and store it somewhere else and query from there (like what Google does).</li>
</ol>

<p>Can anyone tell me which way to go? What are the pros and cons?</p>

<p>Better, are there any better ways to do this?</p>

[{'answer_id': 34318, 'author': 'paan', 'author_id': 2976, 'author_profile': 'https://Stackoverflow.com/users/2976', 'pm_score': 6, 'selected': True, 'text': '<p>Use lucene,<br>\n<a href="http://lucene.apache.org/java/docs/" rel="noreferrer">http://lucene.apache.org/java/docs/</a></p>\n\n<blockquote>\n  <p>Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.</p>\n</blockquote>\n\n<p>It is available in java and .net. It is also in available in php in the form of a zend framework module.</p>\n\n<p>Lucene does what you wanted(indexing of the searched items), you have to keep track of a lucene index but it is much better than doing a database search in terms of performance. BTW, SO search is powered by lucene. :D</p>\n'}, {'answer_id': 34321, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>You might want to have a look at <a href="http://xapian.org/" rel="nofollow noreferrer">xapian</a> and the <a href="http://xapian.org/docs/omega/overview.html" rel="nofollow noreferrer">omega</a> front end. It\'s essentially a toolkit on which you can build search functionality.</p>\n'}, {'answer_id': 34324, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 1, 'selected': False, 'text': "<p>The best way to approach this will depend on how you construct your pages.</p>\n\n<p>If they're frequently composed from a lot of different records (as I imagine stack overflow pages are), the indexing approach is likely to give better results unless you put a lot of work into effectively reconstructing the pages on the database side.</p>\n\n<p>The disadvantage you have with the indexing approach is the turn around time. There are workarounds (like the Google's sitemap stuff), but they're also complex to get right.</p>\n\n<p>If you go with database path, also be aware that modern search engine systems function much better if they have link data to process, so finding a system which can understand links between 'pages' in the database will have a positive effect.</p>\n"}, {'answer_id': 34415, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 0, 'selected': False, 'text': '<p>This is somewhat orthogonal to your question, but I highly recommend the idea of a RESTful search.  That is, to perform a search that has never been performed, the website POSTs a query to /searches/.  To re-run a search, the website GETs /searches/{some id}</p>\n\n<p>There are some good documents to be found regarding this, for example <a href="http://woss.name/2007/07/22/convention-for-restful-search-in-rails/" rel="nofollow noreferrer">here</a>.</p>\n\n<p>(That said, I like indexing where possible, though it is an optimization, and thus can be premature.)</p>\n'}, {'answer_id': 34803, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': -1, 'selected': False, 'text': '<p>If you application uses the Java EE stack and you are using <a href="http://hibernate.org" rel="nofollow noreferrer">Hibernate</a> you can use the <a href="http://www.compass-project.org" rel="nofollow noreferrer">Compass Framework</a> maintain a searchable index of your database. The Compass Framework uses <a href="http://lucene.apache.org" rel="nofollow noreferrer">Lucene</a> under the hood.</p>\n\n<p>The only catch is that you cannot replicate your search index. So you need to use a clustered database to hold the index tables or use the newer grid based index storage mechanisms that have been added to the Compass Framework 2.x.</p>\n'}, {'answer_id': 34812, 'author': 'Johannes', 'author_id': 925, 'author_profile': 'https://Stackoverflow.com/users/925', 'pm_score': 1, 'selected': False, 'text': "<p>If you are on Microsoft plattform you could use the Indexing service. This integrates very easliy with IIS websites. </p>\n\n<p>It has all the basic features like full text search, ranking, exlcude and include certain files types and you can add your own meta information as well via meta tags in the html pages.</p>\n\n<p>Do a google and you'll find tons!</p>\n"}, {'answer_id': 34837, 'author': 'Peter Hoffmann', 'author_id': 720, 'author_profile': 'https://Stackoverflow.com/users/720', 'pm_score': 5, 'selected': False, 'text': '<p>It depends on how comprehensive your web site is and how much you want to do yourself.</p>\n\n<p>If you are running a a <strong>small website</strong> without further possibilities to add a custom search, let google do the work (maybe add a <a href="https://www.google.com/webmasters/tools/docs/en/protocol.html" rel="noreferrer">sitemap</a>) and use the <a href="http://www.google.com/coop/cse/" rel="noreferrer">google custom search</a>.</p>\n\n<p>If you run a <strong>medium site</strong> with an sql engine use the search features of your sql engine.</p>\n\n<p>If you run some <strong>heavier software stack</strong> like J2EE or .Net use <a href="http://lucene.apache.org/java/docs/" rel="noreferrer">Lucene</a>, a great, powerful search engine or its  .Net clone <a href="http://incubator.apache.org/lucene.net/" rel="noreferrer">lucene.Net</a></p>\n\n<p>If you want to <strong>abstract your search from your application</strong> and be able to query it in a language neutral way with XML/HTTP and JSON APIs, have a look at <a href="http://lucene.apache.org/solr/" rel="noreferrer">solr</a>. Solr runs lucene in the background, but adds a nice web interface to it.</p>\n'}]
['https://Stackoverflow.com/questions/34314', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']